#define mp make_pair #define pb push_back using namespace std; typedef long long LL; typedef pair<int,int> PII; inline LL () { LL x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();} return x*f; } const int N=508; int n,x; int s,t; int nume,head[N*N]; struct edge{ int to,nxt,f; }e[N*N*4]; inline void add_edge(int x,int y,int z) { e[++nume]=(edge){y,head[x],z};head[x]=nume; } priority_queue<PII,vector<PII>,greater<PII> > pq; int dis[N*N]; bool vis[N*N]; inline void dijk() { memset(dis,0x3f,sizeof(dis)); dis[s]=0; pq.push(mp(0,s)); while(!pq.empty()){ int x=pq.top().second; pq.pop(); if(vis[x]) continue; vis[x]=1; for(int i=head[x];i;i=e[i].nxt){ if(dis[e[i].to]>dis[x]+e[i].f){ dis[e[i].to]=dis[x]+e[i].f; pq.push(mp(dis[e[i].to],e[i].to)); } } } printf("%d",dis[t]); } int main() { n=read(); s=0;t=n*n+1; for(int i=1;i<=n+1;++i){ for(int j=1;j<=n;++j){ x=read(); if(i==1){ add_edge(s,j,x); } else if(i==n+1){ add_edge(n*(n-1)+j,t,x); } else{ add_edge(n*(i-2)+j,n*(i-1)+j,x); } } } for(int i=1;i<=n;++i){ for(int j=1;j<=n+1;++j){ x=read(); if(j==1){ add_edge(n*(i-1)+1,t,x); } else if(j==n+1){ add_edge(s,n*i,x); } else{ add_edge(n*(i-1)+j,n*(i-1)+j-1,x); } } } for(int i=1;i<=n+1;++i){ for(int j=1;j<=n;++j){ x=read(); if(i==1){ add_edge(j,s,x); } else if(i==n+1){ add_edge(t,n*(n-1)+j,x); } else{ add_edge(n*(i-1)+j,n*(i-2)+j,x); } } } for(int i=1;i<=n;++i){ for(int j=1;j<=n+1;++j){ x=read(); if(j==1){ add_edge(t,n*(i-1)+1,x); } else if(j==n+1){ add_edge(n*i,s,x); } else{ add_edge(n*(i-1)+j-1,n*(i-1)+j,x); } } } dijk(); return 0; }
|
近期评论