"Test Instructions" gives an no-map, starting from 1 to N, two without a common edge of the shortest, so that the sum of the distance is minimal
The weight of each edge is set to cost, the maximum flow is set to 1, and then is the minimum charge flow from the source point to the meeting point flow rate of 2.
Because it is the specified traffic, a new source point and a sink point, the source point to the node 1 a maximum flow of 2, the cost of 0 of the edge, node N to the meeting point of a maximum flow of 2, the cost of 0 of the edge, so that the traffic is regulated.
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include < algorithm> #include <set> #include <map> #include <stack> #include <vector> #include < queue> #include <string> #include <sstream> #define EPS 1e-9#define all (x) X.begin (), X.end () #define INS (x ) Inserter (X,x.begin ()) #define for (I,J,K) for (int i=j;i<=k;i++) #define MAXN 1005#define MAXM 40005#define INF 0x3fffffffusing namespace Std;typedef Long long ll;int i,j,k,n,m,x,y,ans,big,cas,num,w,t,u,v,s,t;bool flag;int head[ Maxn],vis[maxn],dis[maxn],pos[maxn],edge,size;char s[305][305];struct edgenode{int to,next,w,cost;} Edge[MAXM]; void Add_edge (int x,int y,int w,int cost) {edge[edge].to=y; Edge[edge].w=w; Edge[edge].cost=cost; EDGE[EDGE].NEXT=HEAD[X]; Head[x]=edge; edge++; Edge[edge].to=x; edge[edge].w=0; Edge[edge].cost=-cost; Edge[edge].next=head[y]; Head[y]=edge; edge++;} BOOL SPFA (int s, int t) {int u,v,i; Queue <int> q; memset (vis,0,sizeof (VIS)); for (i=0;i<size;i++) Dis[i]=inf; dis[s]=0; Vis[s]=1; Q.push (s); while (!q.empty ()) {U=q.front (); Q.pop (); vis[u]=0; for (I=head[u];i!=-1;i=edge[i].next) {v=edge[i].to; if (Edge[i].w>0&&dis[u]+edge[i].cost<dis[v]) {dis[v]=dis[u]+edge[i].cost; Pos[v]=i; if (!vis[v]) {vis[v]=1; Q.push (v); }}}} return dis[t]!=inf;} int mincostflow (int s,int t) {int i,cost=0,flow=0; while (SPFA (s,t)) {int d=inf; for (i=t;i!=s;i=edge[pos[i]^1].to) {d=min (D,EDGE[POS[I]].W); } for (i=t;i!=s;i=edge[pos[i]^1].to) {edge[pos[i]].w-=d; Edge[pos[i]^1].w+=d; } Flow+=d; Cost+=dis[t]*d; } return cost; Flow is the mostThe large stream value}int main () {memset (head,-1,sizeof (head)); EDGE=0;SCANF ("%d%d", &n,&m); for (i=1;i<=m;i++) {scanf ("%d%d%d", &x,&y,&t); Add_edge (x,y,1,t) ; Add_edge (y,x,1,t);} s=0; T=n+1;size=n+2;add_edge (s,1,2,0), Add_edge (n,t,2,0);p rintf ("%d\n", Mincostflow (s,t)); return 0;}
"Network Stream #" POJ 2135 Farm Tour minimum cost Flow-"Challenge Program Design Competition" Example