http://codevs.cn/problem/1364/
Solving
A look is a short-circuit problem. Set the starting point, end point, press test instructions to add edge. Each floor is annular, the end of the n+1 layer, add edge should be extra careful. A bit of a layered graph. Heap optimization Dijkstra patience to write down. The shortest way to test, but also to test patience.
Code
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace STD;Const intOO =1000000000, nil =0;intNmMap[ $][ $];structpoint{inth, N; Point (inth =0,intn =0): H (h), n (n) {}}; Point u[20000], v[20000], S, T;intE, w[20000], nxt[20000], pnt[ $][ $];intd[ $][ $];BOOLcan[ $][ $], vis[ $][ $];voidAddedge (Point A, point B,intc) {U[++e] = A; V[e] = b; w[e] = c; Nxt[e] = PNT[A.H][A.N]; PNT[A.H][A.N] = e;}voidInit () {intOptmemset(CAN,0,sizeof(can));memset(W,0,sizeof(w));memset(NXT,0,sizeof(NXT));memset(PNT,0,sizeof(PNT)); E =0;scanf("%d%d", &n, &m); for(inti =0; i < N; ++i) { for(intj =0; J < M; ++J) {scanf("%d%d", &opt, &MapI [j]);if(opt = =1) {Can[i][j] =true; } } } for(inti =0; i < N; ++i) { for(intj =0; J < M; ++J) {Addedge (Point (I, J), Point (I, (j +1)% M),MapI [(J +1) (% M]); Addedge (Point (I, (j +1)% M), point (I, J),MapI [j]); opt = J-1;if(Opt <0) {opt = M-1; } Addedge (Point (I, J), point (I, opt),MapI [opt]); Addedge (Point (I, opt), point (I, J),MapI [j]);if(Can[i][j]) {Addedge (Point (I, J), point (i +1, j),Map[i +1][J]); }}} S = Point (N-1, M); T = Point (N, M); for(inti =0; i < M; ++i) {Addedge (S, point (0, i),Map[0][i]); } for(inti =0; i < M; ++i) {Addedge (Point (N, i), T,0); }}structNode: Publicpoint{intD NodeintXH =0,intxn =0,intxd =0) {h = xh; n = xn; d = xd; }BOOL operator< (Constnode& b)Const{returnD > b.d; }};voidWork () {memset(d,0x3f,sizeof(d));memset(Vis,0,sizeof(VIS)); Priority_queue <node> Q; D[S.H][S.N] =0; VIS[S.H][S.N] =1; Q.push (Node (S.h, S.N,0)); while(! Q.empty ()) {node T = q.top (); Q.pop (); for(intj = PNT[T.H][T.N]; J! = Nil; j = Nxt[j]) {if((!VIS[V[J].H][V[J].N]) && D[V[J].H][V[J].N] > T.D + w[j]) {D[V[J].H][V[J].N] = t.d + w[j]; VIS[V[J].H][V[J].N] =true; Q.push (Node (v[j].h, V[J].N, D[V[J].H][V[J].N])); } } }if(D[T.H][T.N] > oo) {puts("-1"); }Else{printf("%d\n", D[T.H][T.N]); }}intMain () {init (); Work ();return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Codevs1364 Treasure Hunt