Caocao ' s Bridges
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3898 Accepted Submission (s): 1225
problem DescriptionCaocao was defeated by Zhuge Liang and Zhou Yu in the Battle of Chibi. But he wouldn ' t give up. Caocao ' s army still is 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 tro Op. 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 is seperated From the other islands. But Zhou Yu had only one bomb which is 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 is guards on bridges. The soldier number of the bombing team couldn ' t is less than the guard number of a bridge, or the mission would fail. Least how many soldiers Zhou Yu has to sent to CompletE The island seperating mission.
InputThere is no more than test cases.
In each test case:
The first line contains-integers, n and m, meaning that there is N islands and M bridges. All the islands is numbered from 1 to N. (2 <= N <=, 0 < M <= N2)
Next m lines describes M bridges. Each line contains three integers u,v and W, meaning this there is a bridge connecting island U and Island V, and there ar E W guards on the bridge. (u≠v and 0 <= W <= 10,000)
The input ends with N = 0 and M = 0.
OutputFor the all test case, print the minimum soldier number Zhou Yu had to send to complete the mission. If Zhou Yu couldn ' t succeed any, print-1 instead.
Sample Input3 31 2 72 3 43 1 43 21 2 72 3 40 0
Sample Output-14
SourceACM/ICPC Asia Regional Hangzhou Online
Test instructions: There are n-Islands and M-Bridge, each bridge has a W soldiers guarding, now to send not less than the number of soldiers to defend the bridge to the bridge, can only blow a bridge, so that the N-island is not connected, ask at least how many people to send.
Analysis: Only need to use the Tarjan algorithm to find the lowest weight in the graph of the bridge on the line. But the problem is God's pit.
The first pit: If the diagram is not connected, do not send people to blow the bridge, direct output 0
Second pit: There may be heavy edges, that is, there are multiple bridges between the two islands (pit t_t)
Third Pit: If there are no soldiers on the bridge, then at least one person will be sent to blow up the bridge.
1 //2016.9.162#include <iostream>3#include <cstdio>4#include <algorithm>5#include <cstring>6 #defineN 10057 8 using namespacestd;9 Ten Const intINF =0x3f3f3f3f; One intEdge[n][n], Vis[n], num[n], low[n], N, M, Index, ans, root;//the VIS represents a connected collection. A - voidTarjan (intCurintFA) - { theindex++; -Num[cur] = Low[cur] =Index; -Vis[cur] =1; - for(inti =1; I <= N; i++) + { - if(edge[cur][i]!=inf) + { A if(Num[i] = =0) at { - Tarjan (i, cur); -Low[cur] =min (low[cur], low[i]); - if(low[i]>Num[cur]) - if(ans>Edge[cur][i]) -Ans =Edge[cur][i]; in}Else if(I! =FA) - { toLow[cur] =min (low[cur], num[i]); + } - } the } * return; $ }Panax Notoginseng - intMain () the { + intu, V, W; A while(SCANF ("%d%d", &n, &m)! =EOF) the { + if(!n &&!m) Break; -Memset (Edge, INF,sizeof(Edge));//the value of the graph is inf to indicate disconnection $memset (NUM,0,sizeof(num)); $memset (Low,0,sizeof(Low)); -memset (Vis,0,sizeof(Vis)); -Index =0, root =1, ans =inf; the for(inti =0; I < m; i++) - {Wuyiscanf"%d%d%d", &u, &v, &W); the if(Edge[u][v] = =inf) - { WuEDGE[U][V] =W; -Edge[v][u] =W; About}Else//if the heavy edge, must not be cut edge, but also is not broken, set the weight value to inf half $ { -EDGE[U][V] = inf/2; -Edge[v][u] = inf/2; - } A } +Tarjan (1, root); the BOOLFG =true;//determine if the diagram is connected - for(inti =1; I <= N; i++) $ if(!Vis[i]) the { theprintf"0\n"); theFG =false; the Break; - } in if(!FG)Continue; the if(ans = = INF | | ans = = inf/2) printf ("-1\n"); the Else if(ans = =0) printf ("1\n");//If there is no Sanatan, send at least one person . About Elseprintf"%d\n", ans); the } the the return 0; +}
HDU4738 (cut Edge)