The original problem is equivalent to breaking off some edges, so that all the original short-circuit all can not be connected to s and T.
The shortest path is first, then the Edge (Dis[u[i]]+w[i]==dis[v[i]) in the shortest path is added to the new picture, running the smallest cut. Obviously.
Note that there is no direction graph.
#include <cstdio> #include <cstring> #include <queue> #include <algorithm>using namespace std;# Define INF 2147483647#define maxn 511#define maxm 505001int n,m,s,t,sta,end,ws[260000],w,c;queue<int>q; namespace dinic{int V[MAXM],CAP[MAXM],EN,FIRST[MAXN],NEXT[MAXM]; int D[MAXN],CUR[MAXN]; void Init_dinic () {memset (first,-1,sizeof (first)); en=0; S=1; T=n;} void Addedge (const int &U,CONST int &v,const int &w) {v[en]=v; cap[en]=w; next[en]=first[u]; first[u]=en++; V[en]=u; NEXT[EN]=FIRST[V]; first[v]=en++;} BOOL BFs () {memset (d,-1,sizeof (d)); Q.push (S); d[s]=0; while (!q.empty ()) {int U=q.front (); Q.pop (); for (int i=first[u];i!=-1;i=next[i]) if (d[v[i]]==-1 && cap[i]) {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 flow=0,f; for (int &i=cur[u];i!=-1;i=next[i]) if (D[u]+1==d[v[i]] && (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; } int Max_flow () {int flow=0,tmp=0; while (BFS ()) {memcpy (Cur,first, (n+5) *sizeof (int)); while (Tmp=dfs (S,inf)) flow+=tmp; } return Flow; }};namespace spfa{int U[260000],next[260000],v[260000],first[501],w[260000],en,dis[501];bool inq[501];void AddEdge (const int &U,CONST int &V,CONST int &w,const int &c) {u[++en]=u; v[en]=v; w[en]=w; Ws[en]=c; Next[en]=first[u]; first[u]=en;} void SPFA (const int &s) {memset (dis,0x7f,sizeof (dis)); dis[s]=0; Inq[s]=1; Q.push (s); while (!q.empty ()) {int U=q.front (); for (int i=first[u];i;i=next[i]) if (Dis[v[i]]>dis[u]+w[i]) {dis[v[i]]=dis[u]+w[i]; if (!inq[v[i]]) Q.push (V[i]), inq[v[i]]=1; } q.pop (); inq[u]=0; }}void Rebuild_graph () {dinic::init_dinic (); for (int i=1;i<= (m<<1); ++i) if (Dis[u[i]]+w[i]==dis[v[i]]) Dinic::addedge (u[i],v[i],ws[i]); }};int Main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;++i) {scanf ("%d%d%d%d", &sta,&end,&w, &C); Spfa::addedge (STA,END,W,C); Spfa::addedge (END,STA,W,C); }SPFA::SPFA (1); Spfa::rebuild_graph ();p rintf ("%d\n%d\n", SPFA::d is[n],dinic::max_flow ()); return 0;}
"Shortest Path" "SPFA" "min Cut" "Dinic" bzoj1266 [AHOI2006] School Route route