Test instructions: The animal escapes, runs from the top left to the lower right corner, the escape path is a grid side, now the zoo staff to intercept. Input gives the cost required to intercept each path of the grid, and the problem requires a minimum of success for interception.
Classic minimum cut problem, but the 400*400 point is too big, so you can't do it directly
LRJ gives the method is the animal to go from the top left to the bottom right, all of us consider how to put obstacles on the can, think can know, as long as the left/bottom right/on the continuous interception edge, you can be all feasible path split open. such as (stole a picture of someone else's blog ...) )
This picture is actually the hdu3870 figure, the test instructions is similar, but does not have the hypotenuse, therefore has twice times less point, the construction diagram also is simple many. From left/Bottom to right/on the interception edge can be considered, increase s source point, Ed Endpoint, the most successful interception of the minimum cost is from S to T can be the shortest possible edge, Dij template cover a bit can be
The difficulty in thinking and building a map, the side into the point, the re-construction of the first look at the beginning to see or quite ruin my three views
#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<stack>#include<string>#include<queue>#include<vector>#include<algorithm>#include<ctime>using namespacestd;#defineEdsonlin#ifdef Edsonlin#defineDebug (...) fprintf (stderr,__va_args__)#else#defineDebug (...)#endif //EdsonlintypedefLong LongLl;typedefDoubledb;Const intINF =0x3f3f3f;Const intMAXN =1e3;Const intMaxnn = 2e6+ -;//const int MAXM = 1E6;//const int MAXM = 3e4+100;Const intMOD =1000000007;ConstDB EPS = 1e-3;#definePB push_back#defineUp (i) ((i) <<1)#defineDown (i) ((Up (i))-1)structdij{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_queuep; 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;intHOR[MAXN][MAXN],VEC[MAXN][MAXN],DIA[MAXN][MAXN];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 intMc=0, N,m; while(SCANF ("%d%d", &n,&m) &&N) { for(intI=1; i<=n;i++){ for(intj=1; j<m;j++) Hor[i][j]=Readint (); } for(intI=1; i<n;i++){ for(intj=1; j<=m;j++) Vec[i][j]=Readint (); } for(intI=1; i<n;i++){ for(intj=1; j<m;j++) Dia[i][j]=Readint (); } intst=0, ed = (n1) * (M-1)*2+1; Solver.init (Ed+1); for(intI=1; i<n;i++){ for(intj=1; j<m;j++){ if(i==1) {Solver.addedge (2* (I-1) * (M-1)+Up (j), Ed,hor[i][j]); Solver.addedge (Ed,2* (I-1) * (M-1)+Up (j), Hor[i][j]); } if(i==n-1) {Solver.addedge (St,2* (I-1) * (M-1) +down (j), hor[i+1][j]); Solver.addedge (2* (I-1) * (M-1) +down (j), st,hor[i+1][j]); } if(i!=n-1) {Solver.addedge (2* (I-1) * (M-1) +down (j),2* (i) * (M-1) +up (j), hor[i+1][j]); Solver.addedge (2*i* (M-1) +up (j),2* (I-1) * (M-1) +down (j), hor[i+1][j]); } if(j==1) {Solver.addedge (St,2* (I-1) * (M-1)+Down (j), Vec[i][j]); Solver.addedge (2* (I-1) * (M-1)+Down (j), St,vec[i][j]); } if(j!=m-1) {Solver.addedge (2* (I-1) * (M-1) +down (j),2* (I-1) * (M-1)+Up (j), Dia[i][j]); Solver.addedge (2* (I-1) * (M-1) +up (j),2* (I-1) * (M-1)+Down (j), Dia[i][j]); Solver.addedge (2* (I-1) * (M-1) +up (j),2* (I-1) * (M-1) +down (j+1), vec[i][j+1]); Solver.addedge (2* (I-1) * (M-1) +down (j+1),2* (I-1) * (M-1) +up (j), vec[i][j+1]); } if(j==m-1) {Solver.addedge (2* (I-1) * (M-1) +down (j),2* (I-1) * (M-1)+Up (j), Dia[i][j]); Solver.addedge (2* (I-1) * (M-1) +up (j),2* (I-1) * (M-1)+Down (j), Dia[i][j]); Solver.addedge (2* (I-1) * (M-1) +up (j), ed,vec[i][j+1]); Solver.addedge (Ed,2* (I-1) * (M-1) +up (j), vec[i][j+1]); }}} Solver.pqdij (ST); printf ("Case %d:minimum =%d\n",++mc,solver.d[ed]); } #ifdef Edsonlin Debug ("Time :%d\n",int(Clock ()-_time_ed)); #endif //Edsonlin //cout << "Hello world!" << Endl; return 0;}
View Code
Uvalive 3661 min Cut converted to shortest path