Catch the Theves
Time limit:5000/2000 MS (java/others) Memory limit:65768/32768 K (java/others)
Total submission (s): 1640 Accepted Submission (s): 514
Problem Descriptiona Group of thieves is approaching a museum in the country of Zjsxzy,now they be in city A,and the Muse Um is in the city B,where keeps many broken legs of zjsxzy. LUCKILY,GW learned the conspiracy when he was watching stars and told it to Zjsxzy.
Zjsxzy decided to caught these thieves,and He-let the police-do this,the police try-catch them on their the-from A t o B. Although the thieves might travel this-by-more than one group, Zjsxzy ' s excellent police have already gather the Statis Tics 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) has Bidirectionals Road to City (I+1, j) and City (i,j+1), Gurad anyone of them costs Aij.
Now give your map,help zjsxzy to calculate the minimium cost. We assume thieves may travel in any way,and we'll catch all passing thieves on a road if we guard it.
Inputthe first line is a integer t,followed by T test cases.
In each test case,the first line contains a number N (1<n<=400).
The following n Lines,each line was n numbers,the jth number of the ith line is Aij.
The city A was always located on (all) and the city B was always located on (n,n).
of Course,the City (I,J) on the last row or last line won ' t has road to (i,j+1) or (I+1,J).
Output for each case,print a line with a number indicating the minimium cost to arrest all thieves.
Sample Input1310 5 56 6 204 7 9
Sample Output18
HintThe map is like this:
Source: multi-university Training Contest 4-host by SDU test instructions: The number of scenarios from the upper-left corner to the lower-right corner. Solution: A look thought is the smallest cut, but there are 1.6 million points, obviously unrealistic, in fact, Rujia book there is a A similar question, but did not read the method. Then find the solution on the net, find the structure duality diagram and solve the duality graph's shortest path. Amazing http://blog.csdn.net/ahero_happy/article/details/6637214.
#include <iostream>#include<cstdio>#include<string.h>#include<queue>#include<algorithm>#include<math.h>using namespaceStd;typedefLong LongLL;Const intINF =999999999;Const intN =405;Const intM = nN;intA[n][n];structedge{intV,w,next;} edge[5*M];intHead[m];intTot,n;voidAddedge (intUintVintWint&k) {EDGE[K].V= V,EDGE[K].W = W,edge[k].next = Head[u],head[u] = k++;}voidinit () {memset (head,-1,sizeof(head)); Tot=0;}BOOLVis[m];intLow[m];intSPFA (intSintt) { for(intI=0; i<=t;i++) {Low[i]=INF; Vis[i]=false; } Low[s]=0; Queue<int>Q; Q.push (s); while(!Q.empty ()) { intU =Q.front (); //printf ("%d\n", u);Q.pop (); Vis[u]=false; for(intK = head[u];k!=-1; k =Edge[k].next) { intv = edge[k].v,w=EDGE[K].W; //printf ("%d%d\n", v,w); if(low[v]>low[u]+5) {Low[v]= low[u]+W; if(!Vis[v]) {Vis[v]=true; Q.push (v); } } } } returnlow[t];}intMain () {inttcase; scanf ("%d",&tcase); while(tcase--) {init (); scanf ("%d",&N); for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++) {scanf ("%d",&A[i][j]); }} N-=1; ints =0, t = n*n+1; /** Structural duality diagram*/ for(intI=1; i<=n;i++){ for(intj=1; j<=n;j++){ intnow = (I-1) *n+J; intNext1 = (I-1) *n+j+1; intNEXT2 = (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;}
HDU 3870 (Minimum cut-to-short for floor plan)