Test instructions: A weighted graph, the sum of the two path weights of the beginning to the end, and the two paths have no common points (except the starting point, the end point);
Analysis: Split-point method, the U and U ', u-u ' capacity of 1, the cost of 0, so that each point can be used only once, the starting point s-s ' capacity of 2, the end T-t ' capacity of 2 to ensure that the maximum flow will find two path, if the input u-v, the right is C, then increase the edge U '-V, the capacity of 1, the cost
#include <cstdio>#include<iostream>#include<sstream>#include<cmath>#include<cstring>#include<cstdlib>#include<string>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<algorithm>using namespacestd;#definell Long Long#define_cle (M, a) memset (M, A, sizeof (m))#defineRepu (I, A, b) for (int i = A; I < b; i++)#defineREPD (I, A, b) for (int i = b; i >= A; i--)#defineSFI (n) scanf ("%d", &n)#definePFI (n) printf ("%d\n", N)#defineSfi2 (n, m) scanf ("%d%d", &n, &m)#definePfi2 (n, m) printf ("%d%d\n", N, m)#definePfi3 (A, B, c) printf ("%d%d%d\n", A, B, c)#defineMAXN 2010#defineMAXM 10*MAXNConst intINF =0x3f3f3f3f;structNod {intB, NXT; intCap, CST; voidInitintBintNxtintCapintCST) { This->b =b; This->NXT =NXT; This->cap =cap; This->CST =CST; }};structMincost {intE[MAXN];intN; Nod BUF[MAXM*2];intLen; intP[MAXN]; voidInitintN) { This->n =N; memset (E,255,sizeof(E)); Len=0; } voidAddcap (intAintBintCapintCST) {Buf[len].init (b, e[a], cap, CST); E[a]= Len + +; Buf[len].init (A, e[b],0,-CST); E[B] = len + +; } BOOLSPFA (intSourceintsink) { Staticqueue<int>Q; Static intD[MAXN]; memset (d, the,sizeof(d)); Memset (P,255,sizeof(p)); D[source]=0; Q.push (source); intu, v; while(!Q.empty ()) {u=Q.front (); Q.pop (); for(inti = E[u]; I! =-1; i =buf[i].nxt) {v=buf[i].b; if(buf[i].cap>0&& d[u]+buf[i].cst<D[v]) {D[v]= d[u]+BUF[I].CST; P[V]=i; Q.push (v); } } } returnD[sink]! =inf; } intSolveintSourceintsink) { intMincost =0, Maxflow =0;//If you need Maxflow, try to return. while(SPFA (source, sink)) {intNeck =inf; for(intT=p[sink]; T! =-1; t = p[buf[t^1].B])//buf[t^1].b is the parent nodeNeck =min (neck, buf[t].cap); Maxflow+=Neck; for(intt = P[sink]; T! =-1; t = p[buf[t^1].b]) {Buf[t].cap-=Neck; Buf[t^1].cap + =Neck; Mincost+ = BUF[T].CST *Neck; } } returnMincost; }} MC;intMain () {intN, M; while(~sfi2 (n, m)) {mc.init (n); intA, B, C; Repu (i,0, M) {Sfi2 (A, B), SFI (c); Mc.addcap ((--A) + N,--b,1, c); } Repu (I,1N1) Mc.addcap (i, i + N,1,0); Mc.addcap (0N2,0); Mc.addcap (n-1N2-1,2,0); PFI (Mc.solve (0N1)); } return 0;}
View Code
UVA 1658 (minimum cost maximum flow)