Subject description:
Problem solution time:
Consider first the altitude $ h $ undetermined point should be how much
Obviously they are $ 0 or $ $ $ 1, $ 0 and $ all together into one, all connected to a $ 1 $
Only above sea boundary line to answer contributes to become a minimum cut
But it is clear that the range data are not directly run the network stream
Since this is a plan view, the routine according to occur:
= Minimum cut plan view of the dual graph the smallest ring area into an outermost = S $ $ $ T $ and shortest run
After construction from the upper left to the lower right corner of FIG. Draw a line to the outside into an S $ and $ $ T $.
But be careful on this chart two different directions weights with an edge.
Then be built in the same direction side, i.e. the side corresponding to the new lower rightward side of $ S $ -> $ T $ direction, whereas on the left.
Then you can run the most short-circuited.
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> using namespace std; typedef long long lint; namespace LarjaIX { const int N=511; int n,id[N][N]; struct sumireko{int to,ne,w;}e[N*N*8]; int he[N*N*2],ecnt; void addline(int f,int t,int w) { e[++ecnt].to=t,e[ecnt].w=w; e[ecnt].ne=he[f],he[f]=ecnt; } struct shion { int x;lint d; shion(){} shion(int x,lint d):x(x),d(d){} bool operator < (const shion &a)const{return d>a.d;} }stmp; priority_queue<shion>q; lint dis[N*N*2]; bool vis[N*N*2]; void dijkstra(int sp,int ep) { memset(dis,0x3f,sizeof(dis)); q.push(shion(sp,dis[sp]=0)); while(!q.empty()) { stmp=q.top(),q.pop(); int x=stmp.x; if(vis[x]) continue;vis[x]=1; for(int i=he[x],t=e[i].to;i;i=e[i].ne,t=e[i].to) { if(dis[t]>dis[x]+e[i].w) { dis[t]=dis[x]+e[i].w; q.push(shion(t,dis[t])); } } } printf("%lld\n",dis[ep]); } int wi; int maid() { #ifdef debug freopen("sample.in","r",stdin); freopen("debug.out","w",stdout); #endif scanf("%d",&n); for(int i=1;i<=n;i++)id[i][0]=id[n+1][i]=0,id[0][i]=id[i][n+1]=n*n+1; for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) id[i][j]=(i-1)*n+j; for(int i=0;i<=n;i++)for(int j=1;j<=n;j++) scanf("%d",&wi),addline(id[i+1][j],id[i][j],wi); for(int i=1;i<=n;i++)for(int j=0;j<=n;j++) scanf("%d",&wi),addline(id[i][j],id[i][j+1],wi); for(int i=0;i<=n;i++)for(int j=1;j<=n;j++) scanf("%d",&wi),addline(id[i][j],id[i+1][j],wi); for(int i=1;i<=n;i++)for(int j=0;j<=n;j++) scanf("%d",&wi),addline(id[i][j+1],id[i][j],wi); dijkstra(0,n*n+1); return 0; } } int main(){return LarjaIX::maid();}