At first glance a template question, a careful look or template problem, but three pits. 1, not a connected graph, put 0. 2 Guards are 0 and 1 are placed. 3 Note the heavy edges.
#include <iostream>#include<cstdio>#include<vector>#include<queue>#include<algorithm>#include<stack>#include<cstring>using namespacestd;#defineMAXN 1005intHead[maxn],dfn[maxn],low[maxn],guard[maxn][maxn],tot,n;intvis[maxn],all,index,bri[maxn*MAXN];structedge{intTo,nxt,g;} EDGE[MAXN*maxn*2];queue<int>que;voidBFsintu) {Vis[u]=1; Que.push (U); while(!Que.empty ()) { intnow =Que.front (); Que.pop (); for(inti = Head[now]; I! =-1; i =edge[i].nxt) { intv =edge[i].to; if(!vis[v]) {Que.push (v); Vis[v] =1;} } }}BOOLjudge () {memset (Vis,0,sizeof(VIS)); intsum =0; while(!que.empty ()) Que.pop (); for(inti =1; I <= N; i++) { if(!Vis[i]) {Sum++; BFS (i); } } if(sum==1)return true; return false;}voidAddedge (intUintVintW) {edge[tot].to=v; EDGE[TOT].G=W; EDGE[TOT].NXT=Head[u]; Head[u]= tot++;}voidinit () { all= Index =0; memset (Low,0,sizeof(low)); memset (DFN,0,sizeof(DFN));}voidTarjan (intUintFA) {Low[u]= Dfn[u] = + +index; for(inti = head[u];i! =-1; i =edge[i].nxt) { intv =edge[i].to; if(!Dfn[v]) {Tarjan (v,u); Low[u]=min (low[u],low[v]); if(Low[v] > Dfn[u] && guard[u][v]!=-2&&guard[v][u]!=-2) {Bri[all++] =EDGE[I].G; } } Else if(v! = FA) Low[u] =min (low[u],dfn[v]); }}intMain () {intm,a,b,w; while(~SCANF ("%d%d",&n,&m)) {if(N+m = =0) Break; memset (Head,-1,sizeof(head)); Tot=0; memset (guard,-1,sizeof(guard)); for(inti =0; I < m; i++) {scanf ("%d%d%d",&a,&b,&W); if(guard[a][b]==-1&& guard[b][a]==-1) {Addedge (a,b,w); Addedge (B,A,W); GUARD[A][B]= Guard[b][a] =W; } Else{Guard[a][b]= Guard[b][a] =-2; } } if(!judge ()) {Puts ("0"); Continue; } init (); Tarjan (1,-1); if(All = =0) {puts ("-1"); Continue; } sort (Bri,bri+All ); if(bri[0] ==0) puts ("1"); Elseprintf"%d\n", bri[0]); }}
HDU 4738 Caocao ' s Bridges (cut edge)