This is the problem of minimizing the maximum flow, giving the edge the relationship and spending, and then feeding D and K,d is the data stream to be transported. K indicates that each side can transport the maximum flow, this figure should be constructed without a map, and found that, generally, no graph of the network flow problem is best to use adjacency table to represent this can solve the heavy edge and reverse edge problem, before using the adjacency matrix to represent the error =
We also want to abstract a source point 0, then 0-1 of the cost is 0, the capacity is D, and then the template is good
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<queue>#defineINF 111111111111111111using namespacestd;Const intn=5550;structnode3{intu,v; Long LongW;} E[n];structnode4{intV,u; intNext; intF; intcap; Long LongCost ; }g[50000]; Long Longd[ the];inthead[ the]; intp[ the];BOOLvis[ the];intn,m;intd,k;intnum;voidinit () {memset (head,-1,sizeof(head)); Num=0;} voidAddintUintVintWLong Longc) {g[num].u=u; G[NUM].V=v; G[num].cap=W; G[num].cost=C; G[NUM].F=0; G[num].next=Head[u]; Head[u]=num++; inttt=T; U=v; V=tt; G[NUM].U=u; G[NUM].V=v; G[num].cap=0; G[num].cost=-B; G[NUM].F=0; G[num].next=Head[u]; Head[u]=num++;}voidEk_ () {Queue<int>P; intf=0; Long LongC=0; for(;;) { for(intI=0; i<=n;i++) D[i]= (i==0?0: INF); Memset (P,-1,sizeof(p)); memset (Vis,false,sizeof(VIS)); Q.push (0); while(!Q.empty ()) { intu=Q.front (); Q.pop (); Vis[u]=false; for(intk=head[u];k!=-1; k=G[k].next) { intv=g[k].v; if(g[k].cap>g[k].f&&d[v]>d[u]+g[k].cost) {D[v]=d[u]+G[k].cost; P[V]=K; if(!Vis[v]) {Vis[v]=true; Q.push (v); } } } } if(d[n]==inf) Break; intA=999999999; for(intu=p[n];u!=-1; u=P[G[U].U]) {a=min (a,g[u].cap-g[u].f); } for(intk=p[n];k!=-1; k=P[G[K].U]) {G[K].F+=A; G[k^1].f-=A; } F+=A; C+=a*D[n]; } if(f==D) cout<<c<<Endl; Elsecout<<"impossible.\n";}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {init (); for(intI=1; i<=m;i++) Cin>>E[i].u>>E[i].v>>E[I].W; CIN>>D>>J; Add (0,1D0); for(intI=1; i<=m;i++) {Add (E[I].U,E[I].V,K,E[I].W); Add (E[I].V,E[I].U,K,E[I].W); } ek_ (); } return 0;}
#include <stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<string>#include<vector>#include<queue>#defineINF 111111111111111111using namespacestd;Const intn=5550;structnode3{intu,v; Long LongW;} E[n];structnode4{intV,u; intNext; intF; intcap; Long LongCost ; }g[50000]; Long Longd[ the];inthead[ the]; intp[ the];BOOLvis[ the];intn,m;intd,k;intnum;voidinit () {memset (head,-1,sizeof(head)); Num=0;} voidAddintUintVintWLong Longc) {g[num].u=T; G[NUM].V=v; G[num].cap=W; G[num].cost=C; G[NUM].F=0; G[num].next=Head[u]; Head[u]=num++; inttt=u; U=v; V=tt; G[NUM].U=T; G[NUM].V=v; G[num].cap=0; G[num].cost=-C; G[NUM].F=0; G[num].next=Head[u]; Head[u]=num++;}voidEk_ () {Queue<int>Q; intf=0; Long LongC=0; for(;;) { for(intI=0; i<=n;i++) D[i]= (i==0?0: INF); Memset (P,-1,sizeof(p)); memset (Vis,false,sizeof(VIS)); Q.push (0); while(!Q.empty ()) { intu=Q.front (); Q.pop (); Vis[u]=false; for(intk=head[u];k!=-1; k=G[k].next) { intv=g[k].v; if(g[k].cap>g[k].f&&d[v]>d[u]+g[k].cost) {D[v]=d[u]+G[k].cost; P[V]=K; if(!Vis[v]) {Vis[v]=true; Q.push (v); } } } } if(d[n]==inf) Break; intA=999999999; for(intu=p[n];u!=-1; u=P[G[U].U]) {a=min (a,g[u].cap-g[u].f); } for(intk=p[n];k!=-1; k=P[G[K].U]) {G[K].F+=A; G[k^1].f-=A; } F+=A; C+=a*D[n]; } if(f==D) cout<<c<<Endl; Elsecout<<"impossible.\n";}intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {init (); for(intI=1; i<=m;i++) Cin>>E[i].u>>E[i].v>>E[I].W; CIN>>D>>K; Add (0,1D0); for(intI=1; i<=m;i++) {Add (E[I].U,E[I].V,K,E[I].W); Add (E[I].V,E[I].U,K,E[I].W); } ek_ (); } return 0;}
Uva (10594)