1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| EDGE(N,N); int S,T,cnt=0,n,m; int id[MAXN][MAXN][2]; LL dis[N]; LL solve() { priority_queue<lpr,vector<lpr>,greater<lpr>> q; fp(i,1,cnt)dis[i]=linf; dis[S]=0,q.push({0,S}); while(!q.empty()) { int u=q.top().se; LL d=q.top().fi; q.pop(); if(d!=dis[u])continue; if(u==T)break; gow(u)if(dis[u]+w<dis[v]) dis[v]=dis[u]+w,q.push({dis[v],v}); } return dis[T]; } void con(int u,int v,int w) { addedge(u,v,w),addedge(v,u,w); } int work() { scanf("%d%d",&n,&m); fp(i,1,n-1)fp(j,1,m-1)id[i][j][0]=++cnt,id[i][j][1]=++cnt; S=++cnt,T=++cnt; fp(i,1,n)fp(j,1,m-1) { int w;scanf("%d",&w); if(i==1)con(S,id[i][j][0],w); else if(i==n) con(id[i-1][j][1],T,w); else con(id[i-1][j][1],id[i][j][0],w); } fp(i,1,n-1)fp(j,1,m) { int w;scanf("%d",&w); if(j==1)con(id[i][j][1],T,w); else if(j==m)con(S,id[i][j-1][0],w); else con(id[i][j][1],id[i][j-1][0],w); } fp(i,1,n-1)fp(j,1,m-1) { int w;scanf("%d",&w); con(id[i][j][0],id[i][j][1],w); } return printf("%lld\n",solve()); }
|