http://acm.hdu.edu.cn/showproblem.php?pid=4738
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5067 Accepted Submission(s): 1589
Problem Description
Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and based on those islands, Caocao's army could easily attack Zhou Yu's troop. Caocao also built bridges connecting islands. If all islands were connected by bridges, Caocao's army could be deployed very conveniently among those islands. Zhou Yu couldn't stand with that, so he wanted to destroy some Caocao's bridges so one or more islands would be seperated from other islands. But Zhou Yu had only one bomb which was left by Zhuge Liang, so he could only destroy one bridge. Zhou Yu must send someone carrying the bomb to destroy the bridge. There might be guards on bridges. The soldier number of the bombing team couldn't be less than the guard number of a bridge, or the mission would fail. Please figure out as least how many soldiers Zhou Yu have to sent to complete the island seperating mission.
Input
There are no more than 12 test cases.
In each test case:
The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )
Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )
The input ends with N = 0 and M = 0.
In each test case:
The first line contains two integers, N and M, meaning that there are N islands and M bridges. All the islands are numbered from 1 to N. ( 2 <= N <= 1000, 0 < M <= N2 )
Next M lines describes M bridges. Each line contains three integers U,V and W, meaning that there is a bridge connecting island U and island V, and there are W guards on that bridge. ( U ≠ V and 0 <= W <= 10,000 )
The input ends with N = 0 and M = 0.
Output
For each test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn't succeed any way, print -1 instead.
Sample Input
3 3
1 2 7
2 3 4
3 1 4
3 2
1 2 7
2 3 4
0 0
Sample Output
-1
4
Source
Recommend
题意:炸去代价最小的一个桥,使图不连通
所以 若原图不连通,则输出0,,若桥上的兵数为0则输出1 ,毕竟还需要人去炸,,若无割边,就输出-1
1 #include <algorithm> 2 #include <cstring> 3 #include <cstdio> 4 5 using namespace std; 6 7 const int INF(0x7fffffff); 8 const int N(1000+15); 9 const int M(N*N); 10 int n,m,u,v,w,ans,num; 11 int head[N],sumedge; 12 struct Edge 13 { 14 int to,next,w; 15 Edge(int to=0,int next=0,int w=0) : 16 to(to),next(next),w(w){} 17 }edge[M<<1]; 18 19 void ins(int from,int to,int w) 20 { 21 edge[++sumedge]=Edge(to,head[from],w); 22 head[from]=sumedge; 23 } 24 25 int dfn[N],low[N],tim; 26 void DFS(int now,int pre) 27 { 28 low[now]=dfn[now]=++tim; 29 int sumtredge=0; 30 for(int i=head[now];i!=-1;i=edge[i].next) 31 if((i^1)!=pre) 32 { 33 int go=edge[i].to; 34 if(!dfn[go]) 35 { 36 sumtredge++; 37 DFS(go,i); 38 if(low[go]>dfn[now]) 39 ans=min(ans,edge[i].w); 40 low[now]=min(low[now],low[go]); 41 } 42 else low[now]=min(low[now],dfn[go]); 43 } 44 } 45 46 int main() 47 { 48 for(;1;) 49 { 50 scanf("%d%d",&n,&m); 51 if(!n&&!m) break; 52 sumedge=-1;num=0;ans=INF;tim=0; 53 memset(head,-1,sizeof(head)); 54 memset(low,0,sizeof(low)); 55 memset(dfn,0,sizeof(dfn)); 56 for(int i=1;i<=m;i++) 57 { 58 scanf("%d%d%d",&u,&v,&w); 59 ins(u,v,w); ins(v,u,w); 60 } 61 for(int i=1;i<=n;i++) 62 if(!dfn[i]) DFS(i,-1),num++; 63 if(num>1) puts("0"); 64 else if(!ans) puts("1"); 65 else if(ans==INF) puts("-1"); 66 else printf("%d ",ans); 67 } 68 return 0; 69 }