HDU 3667 (minimum cost max Flow + split)

Source: Internet
Author: User

Transportation

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


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

Source Regional Harbin Test instructions: Now there is a person to transport K units from point 1th to Point N, each edge has a factor A, the cost of transporting X units from the side of article I is ai*x*x, there is a capacity limit CI on the side of article I, Ask for the minimum cost of transporting this K unit of goods, if not shipped, output-1. Puzzle: Refer to the < Algorithm competition-training guide from Rujia, because each side of the capacity limit will not exceed 5, and each time we ship is an integer, so you can use the split to represent a CI-capable edge can be transported all possible, assuming ci==5, then split into 5 1-capacity Edge, The cost is 1*ai,3*ai,5*ai,7*ai,9*ai, then all the x*x can be composed of these sides, then set the super source point and 1th point capacity of K,n point and the capacity of the super meeting point is K, This limits the maximum flow to no more than K. Then run again MCMF, judge whether Maxflow is K, is the words, output mincost, not words, output-1.
#include <cstdio>#include<cstring>#include<queue>#include<algorithm>using namespacestd;Const intINF =999999999;Const intN = $;Const intM =100005;structedge{intU,v,cap,cost,next;} EDGE[M];intHead[n],tot,low[n],pre[n];intTotal ;BOOLVis[n];voidAddedge (intUintVintCapintCostint&k) {edge[k].u=u,edge[k].v=v,edge[k].cap = Cap,edge[k].cost = Cost,edge[k].next = Head[u],head[u] = k++; EDGE[K].U=v,edge[k].v=u,edge[k].cap =0, edge[k].cost =-cost,edge[k].next = Head[v],head[v] = k++;}voidinit () {memset (head,-1,sizeof(head)); Tot=0;}BOOLSPFA (intSintTintN) {memset (Vis,false,sizeof(VIS));  for(intI=0; i<=n;i++) {Low[i]=INF; Pre[i]= -1; } Queue<int>Q; Low[s]=0;    Q.push (s);  while(!Q.empty ()) {        intU =Q.front ();        Q.pop (); Vis[u]=false;  for(intk=head[u];k!=-1; k=Edge[k].next) {            intv =edge[k].v; if(edge[k].cap>0&&low[v]>low[u]+edge[k].cost) {Low[v]= Low[u] +Edge[k].cost; PRE[V]= k;///v is the corresponding edge of the end point                if(!Vis[v]) {Vis[v]=true;                Q.push (v); }            }        }    }    if(pre[t]==-1)return false; return true;}intMCMF (intSintTintN) {    intMincost =0, minflow,flow=0;  while(SPFA (s,t,n)) {Minflow=inf+1;  for(inti=pre[t];i!=-1; i=pre[edge[i].u]) Minflow=min (minflow,edge[i].cap); Flow+=Minflow;  for(inti=pre[t];i!=-1; i=PRE[EDGE[I].U]) {Edge[i].cap-=Minflow; Edge[i^1].cap+=Minflow; } mincost+=low[t]*Minflow; } Total=flow; returnMincost;}intn,m,k;BOOLFlag[n][n];intMain () { while(SCANF ("%d%d%d", &n,&m,&k)! =EOF)        {init (); memset (Flag,-1,sizeof(flag)); intsrc =0, des = n+1;  for(intI=1; i<=m;i++){            intU,v,a,c; scanf ("%d%d%d%d",&u,&v,&a,&c);  for(intj=0; j<c;j++) {Addedge (u,v,1,(2*j+1)*A,tot); }} addedge (SRC,1K0, tot); Addedge (N,des,k,0, tot); intMincost = MCMF (src,des,n+2); if(total<k) printf ("-1\n"); Elseprintf"%d\n", Mincost); }}

HDU 3667 (minimum cost max Flow + split)

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.