The topic probably says that there are N cities, and M has an edge attached to them, each side has two properties one is the AI one is the price that Ci,ai represents the cost of ai*x*x for transporting x units of goods, CI indicates the maximum number of goods that can be transported in the side (<=5). Ask for the minimum cost of transporting K unit cargo from City 1 to City N.
This unit cost is not fixed cost flow is a classic way to build a picture is to split the edge, the problem is split into CI, each unit costs ai*1, ai*3, Ai*5, ai*7. So if only one unit of traffic through this side then the cost is AI, if two units of traffic so the cost is ai*1+ai*3=ai*2*2 ...
Then the problem is solved.
1#include <cstdio>2#include <cstring>3#include <queue>4#include <algorithm>5 using namespacestd;6 #defineMAXN 1117 #defineMAXM 555558 #defineINF (1<<30)9 Ten structedge{ One intV,cap,cost,next; A }EDGE[MAXM]; - intVS,VT,NV,NE,HEAD[MAXN]; - voidAddedge (intUintVintCapintCost ) { theEdge[ne].v=v; Edge[ne].cap=cap; edge[ne].cost=Cost ; -Edge[ne].next=head[u]; head[u]=ne++; -Edge[ne].v=u; edge[ne].cap=0; edge[ne].cost=-Cost ; -EDGE[NE].NEXT=HEAD[V]; head[v]=ne++; + } - + intD[MAXN],PRE[MAXN]; A BOOLINQUE[MAXN]; at BOOLSPFA () { - for(intI=0; i<nv; ++i) { -D[i]=inf; inque[i]=0; - } -d[vs]=0; inque[vs]=1; -queue<int>que; in Que.push (VS); - while(!Que.empty ()) { to intu=Que.front (); Que.pop (); + for(intI=head[u]; i!=-1; I=Edge[i].next) { - intv=edge[i].v; the if(Edge[i].cap && d[v]>d[u]+edge[i].cost) { *d[v]=d[u]+Edge[i].cost; $pre[v]=i;Panax Notoginseng if(!Inque[v]) { -inque[v]=1; the Que.push (v); + } A } the } +inque[u]=0; - } $ returnd[vt]!=INF; $ } - intMxflow; - intMCMF () { themxflow=0; - intres=0;Wuyi while(SPFA ()) { the intflow=inf,cost=0; - for(intU=VT; U!=vs; u=edge[pre[u]^1].v) { Wuflow=min (flow,edge[pre[u]].cap); - } Aboutmxflow+=flow; $ for(intU=VT; U!=vs; u=edge[pre[u]^1].v) { -edge[pre[u]].cap-=flow; -edge[pre[u]^1].cap+=flow; -cost+=Edge[pre[u]].cost; A } +res+=cost*flow; the } - returnRes; $ } the the intMain () { the intn,m,k; the while(~SCANF ("%d%d%d",&n,&m,&k)) { -vs=0; Vt=n; nv=vt+1; Ne=0; inmemset (head,-1,sizeof(head)); theAddedge (VS,1K0); the About intu,v,a,c; the while(m--){ thescanf"%d%d%d%d",&u,&v,&a,&c); the for(intI=1; i<=c; ++i) { +Addedge (U,v,1, A * (2*i-1)); - } the }Bayi intres=MCMF (); the if(Mxflow!=k) puts ("-1"); the Elseprintf"%d\n", res); - } - return 0; the}
UVa1486 Transportation (minimum cost maximum flow)