To build a dual graph, the shortest path is the maximum flow
The surface of the connection s,t is the new S, the periphery is the new T,
Directed graph connecting edge direction depending on the situation
#include <cstdio>#include<cstring>#include<queue>#defineLL Long Long#defineINF 1000000000using namespacestd; LL dis[500000]; intb[500000]; intnext[5000000],des[5000000],cnt=0, nd[500000],n; LL len[5000000]; structdata{intnum; LL dis; BOOL operator< (ConstData &a)Const { return(a.dis<dis); } }; Priority_queue<data>Heap; voidAddedge (intUintVintl) {next[++cnt]=nd[u];nd[u]=CNT; DES[CNT]=v;len[cnt]=l; } voidbuild () {intT; memset (ND,-1,sizeof(ND)); for(intI=1; i<=n+1; i++) for(intj=1; j<=n;j++) {scanf ("%d",&t); if(i==1) Addedge ((I-1) *n+j,n*n+1, t);Else if(i==n+1) Addedge (0, (I-2) *n+j,t);ElseAddedge ((i-1) *n+j, (I-2) *n+j,t); } for(intI=1; i<=n;i++) for(intj=0; j<=n;j++) {scanf ("%d",&t); if(j==0) Addedge (0, (I-1) *n+j+1, t);Else if(j==n) Addedge (i*n,n*n+1, t);ElseAddedge ((i-1) *n+j, (I-1) *n+j+1, T); } for(intI=1; i<=n+1; i++) for(intj=1; j<=n;j++) {scanf ("%d",&t); if(i==1) Addedge (n*n+1, (I-1) *n+j,t);Else if(i==n+1) Addedge ((n1) *n+j,0, t);ElseAddedge ((i-2) *n+j, (I-1) *n+j,t); } for(intI=1; i<=n;i++) for(intj=0; j<=n;j++) {scanf ("%d",&t); if(j==0) Addedge ((I-1) *n+j+1,0, t);Else if(j==n) Addedge (n*n+1, (I-1) *n+j,t);ElseAddedge ((i-1) *n+j+1, (I-1) *n+j,t); } } voidDij () {inttcnt; for(intI=0; i<=n*n+1; i++) {Data t;t.num=i;t.dis=Dis[i];heap.push (t);} while(!Heap.empty ()) {Data T=Heap.top (); Heap.pop (); if(B[t.num])Continue; for(intP=nd[t.num];p!=-1;p =Next[p])if(dis[des[p]]>dis[t.num]+Len[p]) {tcnt++; DIS[DES[P]]=dis[t.num]+Len[p]; Data Tmp;tmp.num=des[p];tmp.dis=Dis[des[p]]; Heap.push (TMP); } B[t.num]=1; } } intMain () {scanf ("%d",&N); Build (); A for(intI=1;i<500000; i++) dis[i]=1000000000; memset (b,0,sizeof(b)); dis[0]=0; Dij (); printf ("%lld", dis[n*n+1]); }//bzoj2007
Floor Plan Maximum Flow