HDU 3667 split Edge plus minimum charge flow

Source: Internet
Author: User

Transportation

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


Problem Descriptionthere is N cities, and M directed roads connecting them. Now the want to transport K units of the goods from the City 1 to the city N. There is many robbers on the road and so you must be very careful. The more goods your carry, the more dangerous it is. To being more specific, for each road I, there is a coefficient ai. If you want to carry X units of goods along this road, you should pay AI * x2 dollars to hire guards to protect your goods . And what's worse, for each road I, there are an upper bound Ci, which means so you cannot transport more than Ci units of Goods along this road. Please note that you can be carry integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.

Inputthere is several test cases. The first line of all case contains three integers, N, M and K. (1 <= N <=, 1 <= M <=, 0 <= K &lt ; = 100). Then M. Lines followed, each contains four integers (UI, VI, AI, Ci), indicating there are a directed road from the city UI to V I, whose coefficient is AI and upper bound are Ci. (1 <= UI, vi <= N, 0 < ai <=, Ci <= 5)

Outputoutput one, indicating the minimum cost. If It is impossible to transport all the K units of goods, output-1.

Sample Input2 1 21 2 1 22 1 21 2 1 12 2 21 2 1 21 2 2 2

Sample output4-13

The focus of the problem is to remove the edge, although not previously seen the problem of dismantling, but this simple idea I did not think, or should give themselves a big slap
1#include <iostream>2#include <cstdio>3#include <queue>4#include <cstring>5#include <climits>6 #defineMAXP 1107 #defineMaxe 510008 using namespacestd;9 structEdgeTen { One     intS,t,f,c,next; A } Edge[maxe]; - intHEAD[MAXP]; - intPRE[MAXP]; the intDIST[MAXP]; - BOOLISQ[MAXP]; - intn,m,k,s,t,u,v,c,f,ent; - voidAddintSintTintFintc) + { -edge[ent].s=S; +edge[ent].t=T; Aedge[ent].f=F; atEdge[ent].c=C; -edge[ent].next=Head[s]; -head[s]=ent++; -edge[ent].s=T; -edge[ent].t=S; -edge[ent].f=0; inedge[ent].c=-C; -edge[ent].next=Head[t]; tohead[t]=ent++; + } - intMIN (intAintb) the { *     returnA<b?a:b; $ }Panax Notoginseng BOOLSPFA () - { thememset (pre,-1,sizeof(pre));//initialization path is-1 +      for(intI=s; i<=t; i++) A     { theisq[i]=false;//at the beginning of each point is not in the queue +Dist[i]=int_max;//the minimum cost to initialize to each point is Int_max -     } $queue<int>Q; $ Q.push (s); -isq[s]=true;//The source point S has been put into the queue. -dist[s]=0;//distance from source point to Source point is 0 the      while(!q.empty ())//The optimization process ends when the queue is empty, exiting the loop -     {Wuyi         inttemp1=Q.front (); the Q.pop (); -isq[temp1]=false;//the point has exited the queue Wu          for(intI=HEAD[TEMP1]; i!=-1; I=edge[i].next)//from this point, find all the edges that start with that point from the adjacency table and find the points that can be optimized. -         { About             intTemp2=edge[i].t; $             if(edge[i].f&&dist[temp2]>dist[temp1]+edge[i].c) -             { -dist[temp2]=dist[temp1]+edge[i].c; -pre[temp2]=i; A                 if(!ISQ[TEMP2])//If the point is not in the queue, the point is placed in the queue +                 { the Q.push (TEMP2); -isq[temp2]=true; $                 } the             } the         } the     } the     returnpre[t]!=-1;//If the pre[t]==-1 words indicate that no path from S to T is found, that all paths have been found, end loop - } in voidMCMF () the { the     inttot=0; About     intsum=0; the     intmincost=Int_max; the     intminn=Int_max; the      while(SPFA ()) +     { -tot++; themincost=Dist[t];Bayisum+=Mincost; the         if(tot==k) the         { -printf"%d\n", sum); -             return; the         } the          for(inti=pre[t];i!=-1; I=pre[i])//The process of reducing flow in the maximum flow of minimum cost the         { theedge[i].f--; -edge[i^1].f++; theI=Edge[i].s; the         } the     }94printf"-1\n"); the } the intMain () the {98      while(~SCANF ("%d%d%d",&n,&m,&k)) About     { -Ent=0;101memset (head,-1,sizeof(head));102s=1; t=N;103          for(intI=1; i<=m;i++)104         { thescanf"%d%d%d%d",&u,&v,&c,&f);106              for(intj=1; j<=f;j++)107Add (U,v,1, (j*2-1)*c);108         }109         if(m==0|| n==1) the         {111printf"0\n"); the             Continue;113         } the MCMF (); the     } the     return 0;117}
View Code

HDU 3667 split Edge plus minimum charge flow

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.