Reference:
maximum stream + expense flow.
first ask the maximum flow can be.
The second question is "minimum cost maximum flow".
by test instructions, this question can be translated into the "residual network" on the previous question, expanding the capacity of some edges so that the maximum flow from the new graph is K.
so easy: for the side of the remaining traffic, the cost of passing through them is 0. The "Increase in traffic" variable is: to the residual network on each side of the building a capacity is ∞ cost is the edge of W. This means that from these sides, the cost of each flow is w, which is in line with test instructions.
finally build a super source point, from the super source to 1 to build a capacity of k, the cost of 0 of the edge, you can make the minimum cost maximum flow algorithm.
#include <cstdio> #include <algorithm> #include <cstring> #include <queue>using namespace std;# Define MAXN 1011#define maxm 25001#define INF 2147483647int s,t,n,m,a[5001],b[5001],w1,goal;int en,u[maxm],w2[5001],v[ Maxm],first[maxn],next[maxm],cap[maxm],cost[maxm];//next Arraybool Inq[maxn];int d[MAXN]/*spfa*/,p[MAXN]/*spfa*/, a[maxn]/* can improve the amount of */;queue<int>q;void init_mcmf () {memset (first,-1,sizeof (first)); en=0; S=1; T=n;} void Addedge (const int &U,CONST int &V,CONST int &w,const int &c) {u[en]=u; v[en]=v; cap[en]=w; cost[en]=c; Next[en]=first[u]; First[u]=en++;u[en]=v; V[en]=u; Cost[en]=-c; NEXT[EN]=FIRST[V]; first[v]=en++;} BOOL SPFA (int &flow,int &cost) {memset (d,0x7f,sizeof (d)); memset (inq,0,sizeof (INQ)); d[s]=0; Inq[s]=1; p[s]=0; A[s]=inf; Q.push (S); while (!q.empty ()) {int U=q.front (); Q.pop (); inq[u]=0; for (int i=first[u];i!=-1;i=next[i]) if (Cap[i] && d[v[i]]>d[u]+cost[i]) { D[v[i]]=d[u]+cost[i]; P[v[i]]=i; A[v[i]]=min (A[u],cap[i]); if (!inq[v[i]]) {Q.push (v[i]); inq[v[i]]=1;} }} if (d[t]>2100000000) return 0; FLOW+=A[T]; COST+=D[T]*A[T]; int u=t; while (u!=s) {cap[p[u]]-=a[t]; cap[p[u]^1]+=a[t]; U=u[p[u]]; } return 1;} int Flow,cost;int mincost () {flow=0,cost=0; while (SPFA (Flow,cost));} int main () {scanf ("%d%d%d", &n,&m,&goal), INIT_MCMF (); for (int i=1;i<=m;++i) {scanf ("%d%d%d%d", &a[ I],&b[i],&w1,&w2[i]); Addedge (a[i],b[i],w1,0); }mincost (); printf ("%d", Flow); for (int i=1;i<=m;++i) Addedge (A[i],b[i],inf,w2[i]); s=0; Addedge (s,1,goal,0); Mincost (); printf ("%d\n", cost); return 0;}
"Maximum Flow" "Cost flow" bzoj1834 [zjoi2010]network Network expansion