"UVALive-5095" Transportation (fee flow)

Source: Internet
Author: User

Description

There is N cities, and M directed roads connecting them. Now you want to transport K units of
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-carry, the more dangerous it is. To was 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∗x
2 Dollars to hire guards
To protect your goods. And what's worse, for each road I, there are an upper bound Ci
, which means
That's cannot transport more than Ci units of goods along this road. Please note that you can only carry
Integral unit of goods along each road.
You should find out the minimum cost to transport all the goods safely.


Input
There is several test cases.
The first line of all case contains three integers, N, M and K. (1≤n≤100, 1≤m≤5000,
0≤K≤100). Then M. Lines followed, each contains four integers (UI
, VI
, AI
, Ci), indicating there is
A directed road from the city UI to VI
, whose coefficient is AI and upper bound is Ci
. (1≤ui
, Vi≤n,
0 < ai≤100, ci≤5)


Output
Output one line for each test case, indicating the minimum cost. If It is impossible to transport all the
K units of goods, output '-1 '.


Sample Input
2 1 2
1 2 1 2
2 1 2
1 2 1 1
2 2 2
1 2 1 2
1 2 2 2


Sample Output
4
-1
3

Test instructions

A state-owned n (n<=100) city, connected by a one-way road of M (m<=5000). You want to ship K (0<=K<=100) unit goods from City 1 to the city N. These roads are not safe and there are many robbers, so you decide to hire bodyguards to protect you. Each road has a risk factor AI (0<ai<=100), if you carry X units of goods through, need to give bodyguards aix^2 yuan money to ensure your safety (this is reasonable, because the more goods with the more unsafe). In addition, there is a capacity limit CI (ci<=5) on each route, indicating that only the goods with CI units can be passed. Note that the goods cannot be disassembled, so the quantity of goods on the body must be an integer when passing through each edge.

Analysis

The problem is to be torn apart. (smaller capacity)

For a cost coefficient of a, an edge with a capacity of 5 is split into 5 capacity of 1, and the cost is 1a,3a,5a,7a,9a side in turn.

Because the request is the minimum cost flow, if the flow of this arc is 1, the walk is certainly 1a, if the flow is 2, walking is certainly 1a,3a this two, if the flow is 3, walking is certainly 1a,3a,5a ... It is not difficult to verify that, regardless of the traffic is between the one, the corresponding traffic selected edge of the sum of the same is exactly the cost of the non-split.

This problem translates into a common minimum cost maximum flow problem. (Break if traffic reaches K)

The cost flow is probably doing this:

Every time you look for the least expensive augmentation, walk to the end of your journey. So the final ans is the minimum cost under the maximum flow premise.

The code is as follows:

1#include <cstdio>2#include <cstdlib>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <queue>7 using namespacestd;8 #defineMAXN 1109 #defineMAXM 5100Ten #defineINF 0XFFFFFFF One  A intn,m,k; -  - structnode the { -     intX,y,f,c,o,next; -}t[maxm*Ten];intLen; - intFIRST[MAXN]; + intFLOW[MAXN],DIS[MAXN],PRE[MAXN]; - BOOLINQ[MAXN]; +  A intMymin (intXintY) {returnX<y?x:y;} at  - voidInsintXintYintFintc) - { -T[++len].x=x;t[len].y=y;t[len].f=f;t[len].c=C; -t[len].next=first[x];first[x]=len;t[len].o=len+1; -t[++len].x=y;t[len].y=x;t[len].f=0; t[len].c=-C; int[len].next=first[y];first[y]=len;t[len].o=len-1; - } to  +queue<int>Q; - BOOLBFS (intF1,intF2) the { *      while(!q.empty ()) Q.pop (); $memset (pre,-1,sizeof(pre));Panax Notoginsengmemset (INQ,0,sizeof(INQ)); -memset (DIS, the,sizeof(DIS)); thepre[f1]=0; flow[f1]=inf;inq[f1]=1; +dis[f1]=0; Q.push (F1); A      while(!q.empty ()) the     { +         intx=Q.front (), y,i; -          for(I=first[x];i;i=t[i].next)if(t[i].f>0) $         { $y=t[i].y; -             if(dis[y]>dis[x]+t[i].c) -             { thepre[y]=i; -dis[y]=dis[x]+t[i].c;Wuyiflow[y]=mymin (t[i].f,flow[x]); the                 if(!inq[y]) {Q.push (y); inq[y]=1;} -             } Wu         } -Q.pop (); inq[x]=0; About     } $     if(pre[f2]==-1)return 0; -     returnFLOW[F2]; - } -  A voidMax_flow (intXinty) + { the     inta,sum=0, h=0; -     BOOLok=0; $      while(a=BFS (x, y)) the     { the         intnow=y;sum+=a*Dis[y]; theh+=A; the          while(now!=x) -         { int[pre[now]].f-=A; thet[t[pre[now]].o].f+=A; thenow=t[pre[now]].x; About         } the         if(h>=k) Break; the     } the     if(h<k) printf ("-1\n"); +     Elseprintf"%d\n", sum); - } the Bayi intMain () the { the      while(SCANF ("%d%d%d", &n,&m,&k)! =EOF) -     { -len=0; thememset (First,0,sizeof(first)); the          for(intI=1; i<=m;i++) the         { the             intx,y,z,kk,now=0; -scanf"%d%d%d%d",&x,&y,&z,&KK); the              for(intI=1; i<=kk;i++) the             { theIns (x, Y,1, z* (i*i-Now ));94now+=i*i-Now ; the             } the         } theMax_flow (1, n);98     } About     return 0; -}
[LA5095]

2016-05-23 13:39:41

"UVALive-5095" Transportation (fee 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.