HDU 1797 the algorithm should be the largest spanning tree, but I use the maximum flow of

Source: Internet
Author: User
Tags cas

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 22294 Accepted: 5916

Description

Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever mans tells him whether there really is a-a-a-from-the-place his customer have build his giant stee L Crane to the place where it's needed on which all streets can carry the weight.
Fortunately he already have a plan of the city with all streets and bridges and all the allowed weights. Unfortunately he have no idea how to find the the the maximum weight capacity in order to tell him customer how heavy the crane May become. But you surely know.

Problem
You is given the plan of the city, described by the streets (with weight limits) between the crossings, which is numbere D from 1 to N. Your task is to find the maximum weight, can be transported from crossing 1 (Hugo's place) to crossing N (the customer ' s place). You are assume that there are at least one path. All streets can is travelled in both directions.

Input

The first line contains the number of scenarios (city plans). For each city the number N of the street crossings (1 <= n <=) and number M of streets is given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed Weight, which is positive and not larger than 1000000. There'll is at the most one street between each pair of crossings.

Output

The output for every scenario begins with a line containing "scenario #i:", where I am the number of the scenario starting at 1. Then print a, containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.

Sample Input

13 31 2 31 3 42 3 5

Sample Output

Scenario #1:4

Source

Attached code:

1#include <iostream>2#include <queue>3#include <cstring>4#include <cstdio>5#include <climits>6 #defineMaxe 1010*1010*27 #defineMAXP 10108 #defineMax (A, b) a>b?a:b9 #defineMin (A, b) a<b?a:bTen using namespacestd; One structEdge A { -     intS,t,f,next; - } Edge[maxe]; the intHEAD[MAXP]; - intCUR[MAXP]; - intPRE[MAXP]; - intStack[maxe]; + intUSED[MAXP]; - intent; + intMAXN; A intn,m,s,t; at intnum; - voidAddintStartintLastintf) - { -edge[ent].s=start; -edge[ent].t=Last ; -edge[ent].f=F; inedge[ent].next=Head[start]; -head[start]=ent++; toedge[ent].s=Last ; +edge[ent].t=start; -edge[ent].f=0; theedge[ent].next=Head[last]; *head[last]=ent++; $ }Panax Notoginseng BOOLBFsintSintT) - { thememset (pre,-1,sizeof(pre)); +pre[s]=0; Aqueue<int>Q; the Q.push (S); +      while(!q.empty ()) -     { $         inttemp=Q.front (); $ Q.pop (); -          for(intI=HEAD[TEMP]; i!=-1; I=edge[i].next) -         { the             intTemp2=edge[i].t; -             if(pre[temp2]==-1&&edge[i].f>MAXN)Wuyi             { thepre[temp2]=pre[temp]+1; - Q.push (TEMP2); Wu             } -         } About     } $     returnpre[t]!=-1; - } - voidDinic (intStartintLast ) - { A     intflow=0, now; +maxn=0; the      while(BFS (start,last)) -     { $         inttop=0; thememcpy (Cur,head,sizeof(head)); the         intu=start; the          while(1) the         { -             if(U==last)//If the end of the endpoint is found, the intermediate path is processed and the flow is calculated in             { the                 intminn=Int_max; the                  for(intI=0; i<top; i++) About                 { the                     if(minn>edge[stack[i]].f) the                     { theminn=edge[stack[i]].f; +now=i; -                     } the                 }Bayimaxn=Max (Maxn,minn); theedge[stack[now]].f=edge[stack[now]^1].f=0; thetop=Now ; -u=Edge[stack[top]].s; -             } the              for(intI=cur[u]; i!=-1; Cur[u]=i=edge[i].next)//find the side that you can go from the U point the                 if(edge[i].f&&pre[edge[i].t]==pre[u]+1) the                      Break; the             if(cur[u]==-1)//If a viable edge is not found from this point, mark the point and backtrack -             { the                 if(top==0) Break; thepre[u]=-1; theu=edge[stack[--Top]]. s;94             } the             Else//If you find it, continue running the             { thestack[top++]=Cur[u];98u=edge[cur[u]].t; About             } -         }101     }102 }103 intMain ()104 { the     intCAs;106Cin>>CAs;107     intsum=1;108      while(cas--)109     { thememset (head,-1,sizeof(head));111Ent=0; thescanf"%d%d",&n,&m);113s=1; t=N; the         intU,v,flow; the          for(intI=0; i<m;i++) the         {117scanf"%d%d%d",&u,&v,&flow);118 Add (u,v,flow);119 Add (v,u,flow); -         }121printf"Scenario #%d:\n", sum++);122 dinic (s,t);123printf"%d\n\n", MAXN);124     } the     return 0;126}
View Code

HDU 1797 the algorithm should be the largest spanning tree, but I use the maximum flow of

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.