hdu4738 Caocao's Bridges
Caocao's Bridges
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 673 Accepted Submission(s): 288
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 <= N 2 )
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 <= N 2 )
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
liuyiding
题目意思很简单 ,就是求最小桥,注意,这题如果不连通输出0连通的话,如果,最小桥是0刚要输出1,因为,必须派一个人啊!大坑题啊!
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; #define N 1005 #define mem(a,b) memset(a,b,sizeof(a)) #define E 1000050 #define inf 0x4f4f4f4f struct node{ int v,next,id,num,flag; }vec[E]; int head[N],edge_m,ans,sta[E],dfn[N],low[N],vis[N],clock_m; void init(){ mem(head,-1);mem(vis,0);mem(dfn,0);mem(low,0); edge_m=0;ans=0;clock_m=0; } int addedge(int s,int e,int id,int w){ for(int i=head[s];i!=-1;i=vec[i].next){ if(vec[i].v==e){ vec[i].flag++; return 1; } } vec[edge_m].flag=0,vec[edge_m].v=e,vec[edge_m].next=head[s],vec[edge_m].id=id,vec[edge_m].num=w;head[s]=edge_m++; } int tarjan(int x,int pre){ dfn[x]=low[x]=++clock_m; vis[x]=1; int j,v; for(j=head[x];j!=-1;j=vec[j].next){ v=vec[j].v; if(v==pre)continue; if(!vis[v]){ tarjan(v,x); low[x]=min(low[x],low[v]); if(dfn[x]<low[v]&&!vec[j].flag){ sta[ans++]=vec[j].num; } } else { low[x]=min(low[x],dfn[v]); } } } int main() { int n,m,i,s,e,temp; while(scanf("%d%d",&n,&m)!=EOF&&n+m){ init(); for(i=0;i<m;i++){ scanf("%d%d%d",&s,&e,&temp); addedge(s,e,i,temp);addedge(e,s,i,temp); } tarjan(1,-1); int scnt=0; for(i=1;i<=n;i++) if(!vis[i]) scnt++; if(scnt>0){ printf("0\n"); continue; } int minx=inf; for(i=0;i<ans;i++){ //printf(" sta%d ",sta[i]); minx=min(minx,sta[i]); } if(minx==0)printf("1\n"); else if(minx==inf)printf("-1\n"); else printf("%d\n",minx); } return 0; }