HDU 4005 the War

Source: Internet
Author: User

The wartime limit:1000msmemory limit:65768kbthis problem'll be judged onHDU. Original id:4005
64-bit integer IO format: %i64d Java class name: Main  in The war, the intelligence about the enemy is very important. Now, our troop have mastered the situation of the enemy ' s war zones, and known that these war zones can communicate to each Other directly or indirectly through the network. We also know the enemy is going to build a new communication line to strengthen their communication network. Our tasks are to destroy their communication network, so that some of their war zones can ' t communicate. Each of the line have its ' cost of destroy '. If we want to destroy a line, we must spend the ' cost of destroy ' of this line. We want to finish this task using the least cost, but our enemy is very clever. Now, we know the network they has already built, but we know nothing on the new line which our enemy are going to build . In this condition, your task was to find the minimum cost that no matter where we enemy builds the new line, you can destr Oy it using the fixed money. Give the minimum cost. For efficiency, we can only destroy one COMMunication line.  InputThe input contains several cases. For each cases, the first line contains-positive integers n, m (1<=n<=10000, 0<=m<=100000) standing for th E Number of the enemy ' s war zones (numbered from 1 to N), and the number of lines, we enemy has already build. Then M lines follow. For each line there is three positive integer A, B, C (1<=a, B<=n, 1<=c<=100000), meaning between war zone a and war zone B there is a communication line with the ' cost of Destroy ' C.OutputIf the task can be finished output of the minimum cost, or output‐1.Sample Input
3 21 2 12 3 24 31 2 11 3 21 4 3
Sample Output
-13
HintFor the second sample Input:our enemy could build line 2 to 3, 2 to 4, 3 to 4. If They build line 2 to 3, we'll destroy Line 1 to 4 and cost 3. If They build line 2 to 4, we'll destroy Line 1 to 3 and cost 2. If They build line 3 to 4, we'll destroy Line 1 to 2 and cost 1. So, if we want to make sure we can destroy successfully, the minimum cost is 3.SourceThe 36th ACM/ICPC Asia Regional Dalian site--online Contest problem solving: Side Dual connectivity Components +DP
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =10010;4 Const intINF =0x3f3f3f3f;5 structarc{6     intTo,w,next;7Arcintx =0,inty =0,intz =-1){8to =x;9W =y;TenNext =Z; One     } A}e[500000]; - intHd[maxn],hd2[maxn],low[maxn],dfn[maxn],belong[maxn],clk,bcc,tot; - voidAddint*head,intUintVintW) { theE[tot] =arc (V,w,head[u]); -Head[u] = tot++; - } -stack<int>Stk; + voidTarjan (intUintFA) { -Dfn[u] = Low[u] = + +CLK; + stk.push (u); A     BOOLFlag =false; at      for(inti = Hd[u]; ~i; i =E[i].next) { -         if(e[i].to = = FA &&!)flag) { -Flag =true; -             Continue; -         } -         if(!Dfn[e[i].to]) { in Tarjan (e[i].to,u); -Low[u] =min (low[u],low[e[i].to]); to}ElseLow[u] =min (low[u],dfn[e[i].to]); +     } -     if(Low[u] = =Dfn[u]) { the         intv; *++bcc; $          Do{Panax Notoginsengv =stk.top (); - Stk.pop (); theBELONG[V] =bcc; +} while(V! =u); A     } the } +  - intRET =INF,DP[MAXN]; $ voidDfsintUintFA) { $      for(inti = Hd2[u]; ~i; i =E[i].next) { -         if(e[i].to = = FA)Continue; - DFS (e[i].to,u); theDp[e[i].to] =min (DP[E[I].TO],E[I].W); -         if(Dp[u] >Dp[e[i].to]) {WuyiRET =min (ret,dp[u]); theDp[u] =dp[e[i].to]; -}ElseRET =min (ret,dp[e[i].to]); Wu     } - } About voidinit () { $      for(inti =0; i < MAXN; ++i) { -Hd[i] = hd2[i] =-1; -Dfn[i] = Belong[i] =0; -Dp[i] =INF; A     } +tot = BCC = CLK =0; the      while(!stk.empty ()) Stk.pop (); - } $ intMain () { the     intn,m,u,v,w; the      while(~SCANF ("%d%d",&n,&m)) { the init (); the          for(inti =0; I < m; ++i) { -scanf"%d%d%d",&u,&v,&W); in Add (hd,u,v,w); the Add (hd,v,u,w); the         } About          for(inti =1; I <= N; ++i) the             if(!dfn[i]) Tarjan (i,-1); the         if(Bcc = =1){ thePuts"-1"); +             Continue; -         } the         intMINW = Inf,id =0;Bayi          for(inttt = tot, i =0; I < TT; i + =2){ the             if(belong[e[i].to] = = belong[e[i^1].to])Continue; theAdd (hd2,belong[e[i].to],belong[e[i^1].TO],E[I].W); -Add (hd2,belong[e[i^1].TO],BELONG[E[I].TO],E[I].W); -             if(E[I].W < MINW) MINW = E[id =I].W; the         } theRET =INF; theDFS (belong[e[id].to],belong[e[id^1].to]); theDFS (belong[e[id^1].to],belong[e[id].to]); -printf"%d\n", ret = = inf?-1: ret); the     } the     return 0; the}
View Code

HDU 4005 the war

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.