HDU 2435 There is a war
There is a war
This problem will be judged on HDU. Original ID: 2435
64-bit integer IO format: %I64d Java class name: Main
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 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
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 ;
}
HDU 2435 There is a war的更多相关文章
- HDU 2435 There is a war (网络流-最小割)
There is a war Problem Description There is a sea. There are N islands in the sea. ...
- HDU 2435 There is a war Dinic 最小割
题意是有n座城市,n号城市不想让1号城市可达n号,每条道路有一条毁坏的代价,1号还可以修一条不能毁坏的道路,求n号城市所需的最小代价最大是多少. 毁坏的最小代价就直接求一遍最大流,就是最小割了.而可以 ...
- hdu 2435 dinic算法模板+最小割性质
#include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...
- hdu 2435dinic算法模板+最小割性质
hdu2435最大流最小割 2014-03-22 我来说两句 来源:hdu2435最大流最小割 收藏 我要投稿 2435 There is a war 题意: 给你一个有向图,其中可以有一条边是无敌的 ...
- hdu2435最大流最小割
2435 There is a war 题意: 给你一个有向图,其中可以有一条边是无敌的,这条边可以是图中的边,也可以是自己任意加上去的图中没有的边,这条无敌的边不可以摧毁,让1和n无法 ...
- hdu 4005 The war
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4005 In the war, the intelligence about the enemy is ...
- War Chess (hdu 3345)
http://acm.hdu.edu.cn/showproblem.php?pid=3345 Problem Description War chess is hh's favorite game:I ...
- HDU 4005 The war(双连通好题)
HDU 4005 The war pid=4005" target="_blank" style="">题目链接 题意:给一个连通的无向图.每条 ...
- HDU 4005 The war Tarjan+dp
The war Problem Description In the war, the intelligence about the enemy is very important. Now, o ...
随机推荐
- ajax学习和总结
Jquery AJAX http://www.cnblogs.com/jayleke/archive/2012/08/10/2633174.html http://www.php100.com/htm ...
- ubuntu键盘映射
在sublime下开发习惯把CapsLock和Shift间交换,windows下有很多软件可以修改键盘映射,在ubuntu下可以是哦用xmodmap命令,使用方法如下: 在自己用户的home目录下新建 ...
- 关于IE兼容的问题
以下内容,均来自不同的网站,非本人原创,只是收集一下放在一起! =============================== [一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 ...
- IE浏览器兼容background-size
background-size是CSS3新增的属性,IE8以下不支持,通过滤镜实现background-size效果 background-size:contain; // 缩小图片来适应元素的尺寸( ...
- Visual Studio 2005 移植 (札记之一)【zhuan】
Visual Studio 2005 移植 - WINVER,warning C4996, error LINK1104 一.WINVER Compile result: WINVER not d ...
- 不同版本的 Tomcat 设置用户名密码 的方法
Tomcat : tomcat根目录\conf\tomcat-users.xml,找到 <tomcat-users> 标签,在后面添加 <user username="ad ...
- APP启动原理
当我们点击一个应用的时候,系统会自动创建一个相应的activity类实例,然后执行Oncreate方法,接着会执行以下两行代码,解释如下: super.onCreate(savedInstanceSt ...
- bat 符号说明
netstat -an|findstr 139 ipconfig/all findstr IP ipconfig/all |findstr 物理地址 定值选行 ipconf ...
- hihoCode r#1077 : RMQ问题再临-线段树
思路: 两种实现方法: (1)用链表(2)用数组. #include <bits/stdc++.h> using namespace std; int n, q, L, R, op, P, ...
- 洛谷P1036 选数
题目描述 已知 n 个整数 x1,x2,…,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整数分别为 3,7,12, ...