HDU 4738 Caocao ' s Bridges Tarjan seeking bridge

Source: Internet
Author: User

Caocao ' s Bridges

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

Test instructions

In the Yangtze River Cao has set up some points, the point between some sides attached. If the undirected graph formed by these points becomes a connected graph, then the devil is invincible. Liu Bei, in order to prevent the invincible, he intends to destroy the bridge connecting the points of Cao. But Zhuge Liang took all the bombs away, leaving only one for Liu Bei. So Liu Bei can only blow a bridge.

The topic gives n,m. Represents an N-point, M-Bridge.

Each row of the next m-line gives a a,b,c, indicating a bridge between point A and point B, and he sends a C-man to guard the bridge.

Now ask Liu Bei to send at least how many people to blow up the bridge.

If it is not possible to make the point of Cao a plurality of connected graphs, then output-1.

The following:Set hundred family's long hhhh

1. The graph is a side double connected graph, do not delete one side to get two connected blocks, this case output-1.

2. The figure is connected but not the double unicom, using Tarjan to find the bridge in the weight of the smallest, there is a giant pit , if the bridge the minimum weight of 0, then according to test instructions, to output 1 instead of 0 (see the question can be understood).

3. The graph is not connected, there is no need to delete the edge, that is, direct output 0.

In the side (u,v), if Low[v]>dfn[u], then (U,V) is cut edge, but we do not judge the actual processing, because some of the figures may have heavy edges, so bad processing. We record each side of the label (a non-edge split into two of the same to the side of the label), record each point of the father to its side of the label, if the Edge (U,v) is the father side of V, you can not use Dfn[u] update low[v]. This way, if all the subnodes of V are traversed, dfn[u]<low[v] is found, indicating that the edge (U,V) is the cutting edge.

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<queue>#include<vector>#include<stack>#include<string>#include<map>#include<cmath>#defineCLR (A, B) memset ((a), b,sizeof ((a)))#definePB (x) push_back (x)#defineINF 0XFFFFFFFusing namespacestd;Const intmaxn=1010;Const intmaxm=maxn*maxn*2;intDFN[MAXN],LOW[MAXN],HEAD[MAXN];intNum,tot;intans;structnode{intV,c,next,id;} E[MAXM];intn,m;voidTarjan (intUintFA) {Dfn[u]=low[u]=++num;  for(intI=head[u];~i;i=E[i].next) {        intv=e[i].v; intId=e[i].id; if(Fa==id)Continue; if(!Dfn[v])            {Tarjan (v,id); Low[u]=min (low[u],low[v]); if(dfn[u]<Low[v]) {ans=min (ans,e[i].c); }        }        Else{Low[u]=min (low[u],dfn[v]); }    }}voidAddintUintVintCintID) {E[TOT].C=C; E[TOT].V=v; E[tot].id=ID; E[tot].next=Head[u]; Head[u]=tot++;}intMain () { while(cin>>n>>m,n| |m) {        intU,v,c; Tot=0; memset (Head,-1,sizeof(head));  for(intI=1; i<=m;++i) {scanf ("%d%d%d",&u,&v,&c);            Add (u,v,c,i);        Add (v,u,c,i); } memset (DFN,0,sizeof(DFN)); Num=0; intok=0; Ans=INF;  for(intI=1; i<=n;++i)if(!Dfn[i]) {OK++; Tarjan (i,0); }        if(ans==0) ans=1; if(Ans==inf) ans=-1; if(ok>1) ans=0; //for (int i=1;i<=n;++i) cout<<dfn[i]<< "" <<low[i]<<endl;cout<<ans<<Endl; }    return 0;}

Recommend

HDU 4738 Caocao ' s Bridges Tarjan seeking bridge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.