标签:
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 1640 Accepted Submission(s): 514
#include <iostream> #include <cstdio> #include <string.h> #include <queue> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; const int INF = 999999999; const int N = 405; const int M = N*N; int a[N][N]; struct Edge{ int v,w,next; }edge[5*M]; int head[M]; int tot,n; void addEdge(int u,int v,int w,int &k){ edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u] = k++; } void init(){ memset(head,-1,sizeof(head)); tot = 0; } bool vis[M]; int low[M]; int spfa(int s,int t){ for(int i=0;i<=t;i++){ low[i] = INF; vis[i] = false; } low[s] = 0; queue<int> q; q.push(s); while(!q.empty()){ int u = q.front(); // printf("%d\n",u); q.pop(); vis[u] = false; for(int k = head[u];k!=-1;k = edge[k].next){ int v = edge[k].v,w=edge[k].w; // printf("%d %d\n",v,w); if(low[v]>low[u]+w){ low[v] = low[u]+w; if(!vis[v]){ vis[v] = true; q.push(v); } } } } return low[t]; } int main(){ int tcase; scanf("%d",&tcase); while(tcase--){ init(); scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); } } n-=1; int s = 0,t = n*n+1; /**构造对偶图*/ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ int now = (i-1)*n+j; int next1 = (i-1)*n+j+1; int next2 = (i-1)*n+j+n; if(j!=n) { addEdge(now,next1,a[i][j+1],tot); addEdge(next1,now,a[i][j+1],tot); } if(i!=n){ addEdge(now,next2,a[i+1][j],tot); addEdge(next2,now,a[i+1][j],tot); } if(j==1){ addEdge(s,now,a[i][j],tot); addEdge(now,s,a[i][j],tot); } if(i==n){ addEdge(s,now,a[i+1][j],tot); addEdge(now,s,a[i+1][j],tot); } if(i==1){ addEdge(t,now,a[i][j],tot); addEdge(now,t,a[i][j],tot); } if(j==n){ addEdge(t,now,a[i][j+1],tot); addEdge(now,t,a[i][j+1],tot); } } } printf("%d\n",spfa(s,t)); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5693494.html