Run out of the shortest-circuit diagram and then run the maximum flow of the junction.
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include < algorithm>using namespace std; #define M 100001#define INF 2147483647#define N 501typedef Long Long ll;namespace net{int V[m<<1],next[m<<1],first[n<<1],en,cap[m<<1];queue<int>q;void AddEdge (int U,int V, int Cap) {v[en]=v; cap[en]=cap; next[en]=first[u]; first[u]=en++; V[en]=u; NEXT[EN]=FIRST[V]; first[v]=en++; }int s,t,n;int d[n<<1],cur[n<<1];int BFs () {memset (d,-1,sizeof (int) * (n+1)); d[s]=0; Q.push (S); while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=first[u];i!=-1;i=next[i]) if (cap[i]&&d[v[i]]==-1) {d[v[i]]=d[u]+1; Q.push (V[i]); }} return d[t]!=-1; }int DFS (int u,int a) {if (u==t| | (!a)) return A; int f,flow=0; for (int &i=cur[u];i!=-1;i=next[i]) if (d[v[i]]==d[u]+1&& (F=dfs (V[i],min (a,cap[i)))) {Cap[i]-=f ; Cap[i^1]+=f; flow+=f; A-=f; if (!a) break; } if (! Flow) D[u]=-1; return Flow; }ll Maxflow () {ll flow=0;int tmp; while (BFS ()) {memcpy (cur,first,sizeof (int) * (n+1)); while (Tmp=dfs (S,inf)) flow+= (LL) TMP; } return Flow; }};namespace Sp{int N,m;queue<int>q;int v[m<<1],w[m<<1],next[m<<1],first[n],en;void Addedge (int u,int v,int W) {v[++en]=v;w[en]=w;next[en]=first[u];first[u]=en; }ll d[n];bool inq[n];void SPFA (int S) {for (int i=1;i<=n;++i) D[i]=inf; d[s]=0; Q.push (S); Inq[s]=1; while (!q.empty ()) {int U=q.front (); for (int i=first[u];i;i=next[i]) if (d[u]+ (LL) w[i]<d[v[i]) {d[v[i]]=d[u]+ (LL) w[i]; if (!inq[v[i]]) {inq[v[i]]=1; Q.push (V[i]); }} q.pop (); inq[u]=0; }}};int Map (int x,bool op) {if (x==net::s| | X==net::t) return X;return ((!OP)? ( (x<<1)-2):((x<<1)-1);} int main () {int x,y,z;scanf ("%d%d", &sp::n,&sp::m);(int i=1;i<=sp::m;++i) {scanf ("%d%d%d", &x,&y,&z); Sp::addedge (x, y, z); Sp::addedge (Y,X,Z); }SP::SPFA (1); Net::s=1;net::t=2*sp::n-2;net::n=2*sp::n-2;memset (net::first,-1,sizeof (int) * (Net::n+1)); for (int i= 1;i<=sp::en;i+=2) {if (sp::d [sp::v[i+1]]+ (LL) sp::w[i]==sp::d [sp::v[i]]) Net::addedge (Map (sp::v[i+1],1), map (sp: : v[i],0), INF); if (SP::d [sp::v[i]]+ (LL) sp::w[i]==sp::d [sp::v[i+1]]) Net::addedge (Map (sp::v[i],1), Map (sp::v[i+1],0), INF); }SCANF ("%d", &x), for (int i=2;i<sp::n;++i) {scanf ("%d", &x); Net::addedge (Map (i,0), Map (i,1), x); }SCANF ("%d", &x); Cout<<net::maxflow () <<endl;return 0;}
Shortest path, maximum flow bzoj3931 [CQOI2015] Network throughput