#include <stdio.h>#include <string.h>#include <vector>#include <queue>#include <algorithm>using namespace STD;Const intn=5024;Const intinf=0x7fffffff;structedge{intFrom,to,cap,flow,cost;}; vector<Edge>Edges vector<int>G[n];intInq[n],p[n],d[n],a[n];voidAddedge (intFromintTo,intCapintCost) {Edge TP; tp.from=from,tp.to=to,tp.cap=cap,tp.flow=0, Tp.cost=cost; Edges.push_back (TP); tp.from=to,tp.to=from,tp.cap=0, tp.flow=0, Tp.cost=-cost; Edges.push_back (TP);intG=edges.size (); G[from].push_back (g2); G[to].push_back (g1);}intBellmanford (intSintTint&flow,int&cost,intN) {intI,j,u; for(i=0; i<=n+1; i++) D[i]=inf;memset(INQ,0,sizeof(INQ)); d[s]=0; inq[s]=1; p[s]=0; A[s]=inf; Queue<int>Q; Q.push (s); while(! Q.empty ()) {U=q.front (); Q.pop (); inq[u]=0; for(i=0; I<g[u].size (); i++) {Edge &e=edges[G[u][i]];if(E.cap>e.flow&&d[e.to]>d[u]+e.cost) {d[e.to]=d[u]+e.cost; P[e.to]=g[u][i]; A[e.to]=min (A[u],e.cap-e.flow);if(!inq[e.to]) {Q.push (e.to); inq[e.to]=1; } } } }if(D[t]==inf)return 0;if((Long Long) cost>= (Long Long) (D[t]*a[t]) {flow+=a[t]; COST-=D[T]*A[T]; }Else{flow+=cost/d[t];return 0; } u=t; while(u!=0) {edges[p[u]].flow+=a[t]; edges[p[u]^1].FLOW-=A[T]; U=edges[p[u]].from; }return 1;}intMincost (intSintTintNintMX) {intflow=0, cost=mx,ans=0; while(Bellmanford (S,t,flow,cost,n)) {//printf ("%d%d\n", flow,cost);};returnFlow;}voidInitintN) { for(intI=0; i<=n+1; i++) g[i].clear (); Edges.clear ();}intMain () {intI,u,v,c1,n,m,_,c2,mx,pi;scanf("%d", &_); while(_--) {scanf("%d%d%d%d", &N,&M,&MX,&PI); Init (n); Addedge (N,0, MX/PI,PI); for(i=0; i<m;i++) {scanf("%d%d%d%d", &U,&V,&C1,&C2); Addedge (U,V,C1,0); Addedge (V,U,C1,0); Addedge (U,V,INF,C2); Addedge (V,U,INF,C2); }printf("%d\n", Mincost (N,1, n,mx)); }return 0;}
hoj2543 Stone IV "minimum cost maximum Flow"