Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65768/32768 K (Java/Others)
Total Submission(s): 1640 Accepted Submission(s): 514
Zjsxzy decided to caught these thieves,and he let the police to do this,the police try to catch them on their way from A to B. Although the thieves might travel this way by more than one group, zjsxzy's excellent police has already gather the statistics that the cost needed on each road to guard it.
Now ,zjsxzy's conutry can be described as a N*N matrix A,Aij indicates the city(i,j) have bidirectionals road to city(i+1,j) and city(i,j+1),gurad anyone of them costs Aij.
Now give you the map,help zjsxzy to calculate the minimium cost.We assume thieves may travel in any way,and we will catch all passing thieves on a road if we guard it.
In each test case,the first line contains a number N(1<N<=400).
The following N lines,each line is N numbers,the jth number of the ith line is Aij.
The city A is always located on (1,1) and the city B is always located on (n,n).
Of course,the city (i,j) at the last row or last line won't have road to (i,j+1) or (i+1,j).
#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; }