HDU 4738 diagram-free bridge

Source: Internet
Author: User

Caocao ' s Bridges

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1876 Accepted Submission (s): 679


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 each 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

Source2013 ACM/ICPC Asia Regional Hangzhou Online

Attention:

1. There are heavy edges;

2. Go to at least one soldier;

3. The diagram is not connected to the need to go;

1#include <iostream>2#include <algorithm>3#include <cstdio>4#include <cstring>5#include <cstdlib>6#include <vector>7#include <map>8 using namespacestd;9 Const intmaxn=1007;Ten Const intinf=0x3f3f3f3; One structnode A { -     intTo,cost,next; - }; theNode edge[maxn*MAXN]; - intHEAD[MAXN],N,M,NUM,ANS,SUM,PRE[MAXN],CHILD,LOW[MAXN]; - voidAddedge (intUintVintc) - { +edge[num].to=v;edge[num].cost=c;edge[num].next=Head[u]; -head[u]=num++; +edge[num].to=u;edge[num].cost=c;edge[num].next=Head[v]; Ahead[v]=num++; at } - voidTarjan (intUintFA) - { -child++; -low[u]=pre[u]=sum++; -      for(inti=head[u];i!=-1; i=edge[i].next) in     { -         intv=edge[i].to; to         if(i== (fa^1))Continue; +         if(!Pre[v]) { - Tarjan (v,i); thelow[u]=min (low[u],low[v]); *             if(pre[u]<Low[v]) { $ans=min (ans,edge[i].cost);Panax Notoginseng             } -         } the         Elselow[u]=min (low[u],low[v]); +     } A } the intMain () + { -     inta,b,c; $      while(~SCANF ("%d%d",&n,&m)) { $         if(n==0&&m==0) Break; -memset (head,-1,sizeof(head)); -memset (PRE,0,sizeof(pre)); thememset (Low,0,sizeof(Low)); -Ans=inf;child=0; sum=1;Wuyi          for(intI=1; i<=m;i++) the         { -scanf"%d%d%d",&a,&b,&c); Wu Addedge (a,b,c); -         } AboutTarjan (1,-1); $         if(child<n) printf ("0\n"); -         Else if(Ans==inf) printf ("-1\n"); -         Else if(ans==0) printf ("1\n"); -         Elseprintf"%d\n", ans); A     } +     return 0; the}

HDU 4738 diagram-free 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.