Hdu 3870 min Cut converted to shortest path 2

Source: Internet
Author: User

Catch the Thevesproblem Descriptiona group of Thieves is approaching a museum in the country of Zjsxzy,now they was in CIT Y A,and the museum is in 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).

OutputFor 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:

Thinking: And the previous question http://www.cnblogs.com/EdsonLin/p/5574033.html almost, but this question directly to the right, so logically feel good to understand a little

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<stack>#include<string>#include<queue>#include<vector>#include<algorithm>#include<ctime>using namespacestd;//#define Edsonlin#ifdef Edsonlin#defineDebug (...) fprintf (stderr,__va_args__)#else#defineDebug (...)#endif //EdsonlintypedefLong LongLl;typedefDoubledb;Const intINF =0x3f3f3f;Const intMAXN =410;Const intMaxnn =160000;Const intMAXM =1e6;//const int MAXM = 3e4+100;Const intMOD =1000000007;ConstDB EPS = 1e-3;#definePB push_backstructdij{intn,m; intFirst[maxnn]; structedge{intst,to,next,dist;    }; Vector<edge>e; inttop; intD[MAXNN];//s to the distance of each node    intDONE[MAXNN];//whether it has been permanently marked    intP[MAXNN];//record the previous edge    structheapnode{intSt; intDist; BOOL operator< (Constheapnode& RHS)Const {            returnDist>rhs.dist;    }    }; voidInitintN) {         This->n =N; memset (First,-1,sizeof(first)); Top=0;    E.clear (); }    voidAddedge (intUintVintDist) {        /*e[top].st = u;        E[top].to = v;        E[top].dist = dist;        E[top].next = First[u]; First[u] = top++;*/Edge A; A.St=u; A.to=v; A.dist=Dist; A.next=First[u];        E.PB (a); First[u]= top++; //cout<<first[u]<<endl; //cout<<top<<endl;    }    voidPqdij (ints) {Priority_queueQ;        Heapnode A;  for(intI=0; i<n;i++) D[i]=inf; D[s]=0; memset (Done,0,sizeof(done)); A.dist=0; A.St=s;        Q.push (a);  while(!Q.empty ()) {Heapnode x=Q.top ();            Q.pop (); intU =X.st; if(Done[u])Continue; Done[u]=1;  for(inti=first[u];i!=-1; i=E[i].next) {                if(d[e[i].to]>d[u]+e[i].dist) {D[e[i].to]= D[u] +e[i].dist; P[e[i].to]=i; A.dist=D[e[i].to]; A.St=e[i].to;                Q.push (a); }}}}}solver;intReadint () {intX;SCANF ("%d", &x);returnx;}intMain () {#ifdef Edsonlin//freopen ("1.in", "R", stdin); //freopen ("1.out", "w", stdout);        int_time_ed =clock (); #endif //Edsonlin    intT; scanf ("%d",&T); intMc=0, GRID[MAXN][MAXN];  while(mc++<T) {        intn,st,ed; CIN>>N;  for(intI=1; i<=n;i++){             for(intj=1; j<=n;j++) Grid[i][j]=Readint (); } St=0; ed = (n1) * (n1)+1; Solver.init (Ed+1);  for(intI=1; i<n;i++){             for(intj=1; j<n;j++){                intCur = (I-1) * (n1)+J; if(i==1) {Solver.addedge (cur,ed,grid[i][j]);                Solver.addedge (Ed,cur,grid[i][j]); }                if(i!=n-1) {Solver.addedge (cur,cur+n-1, grid[i+1][j]); Solver.addedge (cur+n-1, cur,grid[i+1][j]); }                if(i==n-1) {Solver.addedge (st,cur,grid[i+1][j]); Solver.addedge (Cur,st,grid[i+1][j]); }                if(j==1) {Solver.addedge (st,cur,grid[i][j]);                Solver.addedge (Cur,st,grid[i][j]); }                if(j!=n-1) {Solver.addedge (cur,cur+1, grid[i][j+1]); Solver.addedge (cur+1, cur,grid[i][j+1]); }                if(j==n-1) {Solver.addedge (cur,ed,grid[i][j+1]); Solver.addedge (Ed,cur,grid[i][j+1]);        }}} Solver.pqdij (ST); printf ("%d\n", solver.d[ed]); } #ifdef Edsonlin Debug ("Time :%d\n",int(Clock ()-_time_ed)); #endif //Edsonlin   //cout << "Hello world!" << Endl;    return 0;}
View Code

Hdu 3870 min Cut converted to shortest path 2

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.