There is a war

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2435
64-bit integer IO format: %I64d      Java class name: Main

There is a sea.
There are N islands in the sea.
There are some directional bridges connecting these islands.
There is a country called Country One located in Island 1.
There is another country called Country Another located in Island N.
There is a war against Country Another, which launched by Country One.
There
is a strategy which can help Country Another to defend this war by
destroying the bridges for the purpose of making Island 1 and Island n
disconnected.
There are some different destroying costs of the bridges.
There
is a prophet in Country Another who is clever enough to find the
minimum total destroying costs to achieve the strategy.
There
is an architecture in Country One who is capable enough to rebuild a
bridge to make it unbeatable or build a new invincible directional
bridge between any two countries from the subset of island 2 to island
n-1.
There is not enough time for Country One, so it can only
build one new bridge, or rebuild one existing bridge before the Country
Another starts destroying, or do nothing if happy.
There is a
problem: Country One wants to maximize the minimum total destroying
costs Country Another needed to achieve the strategy by making the best
choice. Then what’s the maximum possible result?

Input

There are multiple cases in this problem.
There is a line with an integer telling you the number of cases at the beginning.
The
are two numbers in the first line of every case, N(4<=N<=100) and
M(0<=M<=n*(n-1)/2), indicating the number of islands and the
number of bridges.
There are M lines following, each one of
which contains three integers a, b and c, with 1<=a, b<=N and
1<=c<=10000, meaning that there is a directional bridge from a to b
with c being the destroying cost.
There are no two lines containing the same a and b.

Output

There is one line with one integer for each test case, telling the maximun possible result.

Sample Input

4
4 0
4 2
1 2 2
3 4 2
4 3
1 2 1
2 3 1
3 4 10
4 3
1 2 5
2 3 2
3 4 3

Sample Output

0
2
1
3

Source

 
解题:暴力枚举 + 最小割
 #include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>;
const int maxn = ;
struct arc {
int to,flow,next;
arc(int x = ,int y = ,int z = -) {
to = x;
flow = y;
next = z;
}
} e[maxn*maxn];
int head[maxn],d[maxn],gap[maxn],tot,S,T;
void add(int u,int v,int flow) {
e[tot] = arc(v,flow,head[u]);
head[u] = tot++;
e[tot] = arc(u,,head[v]);
head[v] = tot++;
}
int dfs(int u,int low) {
if(u == T) return low;
int tmp = ,minH = T - ;
for(int i = head[u]; ~i; i = e[i].next) {
if(e[i].flow) {
if(d[u] == d[e[i].to] + ) {
int a = dfs(e[i].to,min(e[i].flow,low));
e[i].flow -= a;
e[i^].flow += a;
tmp += a;
low -= a;
if(!low) break;
if(d[S] >= T) return tmp;
}
}
if(e[i].flow) minH = min(minH,d[e[i].to]);
}
if(!tmp) {
if(--gap[d[u]] == ) d[S] = T;
++gap[d[u] = minH + ];
}
return tmp;
}
int sap(int ret = ) {
memset(gap,,sizeof gap);
memset(d,,sizeof d);
gap[S] = T;
while(d[S] < T) ret += dfs(S,INF);
return ret;
}
bool vis[maxn];
void dfs(int u) {
vis[u] = true;
for(int i = head[u]; ~i; i = e[i].next)
if(e[i].flow && !vis[e[i].to]) dfs(e[i].to);
}
int a[maxn*maxn],b[maxn*maxn],c[maxn*maxn];
int main() {
int n,m,kase;
scanf("%d",&kase);
while(kase--) {
scanf("%d%d",&n,&m);
memset(head,-,sizeof head);
for(int i = tot = ; i < m; ++i) {
scanf("%d%d%d",a + i,b + i,c + i);
add(a[i],b[i],c[i]);
}
S = ;
T = n;
int ret = sap();
memset(vis,false,sizeof vis);
dfs(S);
for(int i = ; i < n; ++i) {
if(!vis[i]) continue;
for(int j = ; j < n; ++j) {
if(vis[j]) continue;
memset(head,-,sizeof head);
for(int k = tot = ; k < m; ++k)
add(a[k],b[k],c[k]);
add(i,j,INF);
ret = max(ret,sap());
}
}
printf("%d\n",ret);
}
return ;
}

最新文章

  1. 版本控制工具比较-CVS,SVN,GIT
  2. 【转】js 关键字 in 的使用方法
  3. Nodejs&amp;express+mongodb完成简单用户登录(即Nodejs入门)
  4. 一、Python 简介
  5. windbg学习---.browse打开一个新的command 窗口
  6. js实现对json数据的序列化(兼容ie6以上浏览器)
  7. 用Zend Studio12 导入在workspace中的项目
  8. Linq JsRender
  9. Webform——验证控件
  10. menu菜单项和menubutton菜单按钮的结合使用
  11. 强大的Resharp插件(转)
  12. 手把手的教你激活PyCharm --Pycharm激活详细教程(二)(非常详细,非常实用)
  13. svn&#160;checkout&#160;实用小技巧
  14. mysql5.6 sql_mode设置为宽松模式
  15. Entity Framework Code first 可能会导致循环或多个级联路径.
  16. 第 16 章 C 预处理器和 C 库(可变参数:stdarg.h)
  17. mybatis配置文件resultMap标签的使用
  18. .NET中的FileUpload控件的使用-Jquery(一)
  19. 详解tomcat连接数和线程数
  20. openal 基础知识

热门文章

  1. RK3288开发过程中遇到的问题点和解决方法之Devices
  2. SM2-DE
  3. ejb2.0用本地引用提高EJB访问效率
  4. org.springframework.beans.factory.BeanCreationException: Error creating bean with name &#39;requestMappingHandlerMapping&#39; defined in class path resource
  5. Android(java)学习笔记143:Android中View动画之 XML实现 和 代码实现
  6. html备忘录
  7. 中国剩余定理&amp;Lucas定理&amp;按位与——hdu 5446
  8. Java Miniui实现批量上传文件demo 201906221520
  9. hibernate的注解
  10. 多数据源连接Oracle报错,linux熵池耗尽问题