/* <Br/> the question is actually very simple, but the question is too long to be understood. The most depressing thing is to understand it at the beginning, <br/> it's only 2520/8 = 290. As a result, the idea that you understand is totally collapsed, so you are struggling and suffering .. <Br/> I didn't expect this mentally retarded problem to happen. <br/> the question roughly means finding the least time-consuming path from the start point to the end point and outputting it (blips is actually time) <br/> the distance between each route is 2520 (the minimum public multiple from 1 to 9 can be ignored ), the maximum speed between two points is P <br/> the time t = 2520/P for two points. Note that if p = 0, the two points are disconnected, the following figure shows a total of V = N * (m + 1) + M Points <br/> */<br/> # include <iostream> <br/> # include <cstdio> <br/> # include <queue> <br/> # include <cstring> <br/> using namespace STD; </P> <p> const int INF = 0x3fffffff; <br/> const int n = 1005; <br/> int cost [N] [N]; <B R/> int dis [N]; <br/> bool hash [N]; </P> <p> struct node <br/>{< br/> int DIS; <br/> int num; <br/> friend bool operator <(node A, Node B) <br/> {<br/> return. dis> B. DIS; <br/>}< br/>}; </P> <p> void BFS (INT end) <br/>{< br/> memset (hash, false, sizeof (hash); <br/> node P, F; <br/> priority_queue <node> q; <br/> for (INT I = 1; I <= end; I ++) <br/>{< br/> dis [I] = cost [0] [I]; <br/> If (DIS [I]! = Inf) <br/>{< br/> P. num = I; <br/> P. dis = dis [I]; <br/> q. push (p); <br/>}< br/> hash [0] = true; <br/> while (! Q. empty () <br/>{< br/> F = Q. top (); <br/> q. pop (); <br/> If (hash [end]) <br/> break; <br/> hash [F. num] = true; <br/> for (INT I = 1; I <= end; I ++) <br/>{< br/> If (! Hash [I] & dis [I]> F. DIS + cost [F. Num] [I] & cost [F. Num] [I]! = Inf) <br/>{< br/> dis [I] = f. DIS + cost [F. num] [I]; <br/> P. num = I; <br/> P. dis = dis [I]; <br/> q. push (P ); <br/>}</P> <p >}< br/>}</P> <p> inline int direction_w_e (char s []) <br/>{< br/> If (s [0] = '*') <br/> return 2; <br/> If (s [0] = '>') <br/> return 1; <br/> If (s [0] = '<') <br/> return-1; <br/>}</P> <p> inline int direction_n_s (char s []) <br/>{< br/> If (s [0] = '*') <br/> return 2; <br/> If (s [0] = '^') <Br/> return 1; <br/> If (s [0] = 'V') <br/> return-1; <br/>}</P> <p> inline void int () <br/>{< br/> for (INT I = 0; I <N; I ++) <br/> for (Int J = 0; j <n; j ++) <br/> {<br/> if (I = J) <br/> cost [I] [J] = 0; <br/> else <br/> cost [I] [J] = inf; <br/>}</P> <p> int main () <br/>{< br/> int n, m; <br/> while (scanf ("% d", & N, & M )! = EOF) <br/>{< br/> If (n = 0 & M = 0) <br/> break; <br/> int sp, P; <br/> char s [2]; <br/> int dir; <br/> int M = m + 1; <br/> int (); <br/> // The following figure is very troublesome. It is very easy to use an adjacent table .. <Br/> for (INT I = 0; I <= N; I ++) <br/> {<br/> if (I = 0) <br/> {<br/> for (Int J = 1; j <= m; j ++) <br/>{< br/> scanf ("% d % s", & P, S); <br/> If (P = 0) <br/>{< br/> cost [J-1] [J] = cost [J] [J-1] = inf; <br/> continue; <br/>}< br/> sp = 2520/P; <br/> dir = direction_w_e (s); <br/> If (DIR = 2) <br/> cost [J-1] [J] = cost [J] [J-1] = sp; <br/> If (DIR =-1) <br/> cost [J] [J-1] = sp; <br/> If (DIR = 1) <br/> C Ost [J-1] [J] = sp; </P> <p >}< br/> else <br/>{< br/> for (Int J = 0; j <= m; j ++) <br/>{< br/> scanf ("% d % s", & P, S); <br/> If (P = 0) <br/> {<br/> cost [(I-1) * m + J] [M * I + J] = cost [M * I + J] [M * (I-1) + J] = inf; <br/> continue; <br/>}< br/> sp = 2520/P; <br/> dir = direction_n_s (s ); <br/> If (DIR = 2) <br/> cost [(I-1) * m + J] [M * I + J] = cost [M * I + J] [M * (I-1) + J] = sp; <br/> If (DIR =-1) <br/> cost [(I-1) * m + J] [M * I + J] = sp; <br/> If (DIR = 1) <br/> cost [M * I + J] [M * (I-1) + J] = sp; </P> <p >}< br/> for (Int J = 1; j <= m; j ++) <br/>{< br/> scanf ("% d % s", & P, S); <br/> If (P = 0) <br/> {<br/> cost [M * I + J] [M * I + J-1] = cost [M * I + J-1] [M * I + J] = inf; <br/> continue; <br/>}< br/> sp = 2520/P; <br/> dir = direction_w_e (s ); <br/> If (DIR = 2) <br/> cost [M * I + J] [M * I + J-1] = cost [M * I + J-1] [M * I + J] = sp; <br/> If (DIR =-1) <br/> cost [M * I + J] [M * I + J-1] = sp; <br/> If (DIR = 1) <br/> cost [M * I + J-1] [M * I + J] = sp; <br/> If (sp = 0) <br/> cost [M * I + J] [M * I + J-1] = cost [M * I + J-1] [M * I + J] = INF; <br/>}< br/> int end = N * m + m; <br/> BFS (end ); <br/> If (DIS [end]! = Inf) <br/> printf ("% d blips/N", DIS [end]); <br/> else <br/> printf ("Holiday/N "); <br/>}</P> <p>/* <br/> # include <iostream> <br/> # include <cstdio> <br/> # include <algorithm> <br/> # include <memory. h> <br/> # include <cmath> <br/> # include <bitset> <br/> # include <queue> <br/> # include <vector> <br /> using namespace STD; <br/> const int border = (1 <20)-1; const int maxsize = 37; const int maxn = 1105; cons T int inf = 1000000000; # define CLR (x, y) memset (X, Y, sizeof (x) # define add (x) x = (x + 1) & Border) # define in (x) scanf ("% d", & X) # define out (x) printf ("% d/N", X) # define min (M, V) (m) <(v )? (M) :( v) # define max (M, V) (m)> (v )? (M) :( v) # define ABS (x)> 0? (X):-(x) # define Len 2520 # define set_node (NO, a, B) {No. U = A; No. val = B;} typedef struct {int V, next; int val;} edge; typedef struct {int U; int val;} node; bool operator <(const node &, const node & B) {return. val> B. val;} edge [maxn * maxn]; int n, m, start, end, index; int Dist [maxn], net [maxn]; bool visit [maxn]; void add_edge (const Int & U, const Int & V, const Int & SP) {edge [Index]. V = V; edge [Index]. next = net [U]; edge [Index]. val = sp; net [u] = index ++;} void add_input (const Int & U, const Int & V, const Int & sp, const char & C) {If (C = '*') {add_edge (u, v, SP); add_edge (v, U, SP); return ;} if (C = '>' | C = 'V') add_edge (u, v, SP); else add_edge (v, U, SP );} int Init () {Index = 0; CLR (net,-1); CLR (visit, 0); CLR (Dist, 127); Return 0;} int input () {int I, j, U, V, TMP, SP; char ch; for (I = 0; I <n-1; ++ I) {for (j = 0; j <m -1; ++ J) {scanf ("% d % C", & sp, & Ch); If (sp = 0) continue; U = I * m + J; V = u + 1; add_input (u, v, SP, CH);} For (j = 0; j <m; ++ J) {scanf ("% d % C", & sp, & Ch); If (sp = 0) continue; u = I * m + J; V = (I + 1) * m + J; add_input (u, v, SP, CH) ;}} for (j = 0; j <m-1; ++ J) {scanf ("% d % C", & sp, & Ch); U = (n-1) * m + J; V = u + 1; if (sp = 0) continue; add_input (u, v, SP, CH);} return 0;} int dij () {int I, j, U, TMP, Mark, MMI N, V; int n = N * m; priority_queue <node> que; node, t_node; while (! Que. empty () que. pop (); set_node (t_node, 0, 0); que. push (t_node); Dist [0] = 0; CLR (visit, 0); While (! Que. empty () {node = que. top (); que. pop (); U = node. u; If (visit [node. u]) continue; If (u = N-1) return node. val; visit [u] = true; for (I = net [u]; I! =-1; I = edge [I]. next) {v = edge [I]. v; If (visit [v]) continue; TMP = DIST [u] + Len/edge [I]. val; If (Dist [v]> TMP) {Dist [v] = TMP; set_node (t_node, V, TMP); que. push (t_node) ;}}} return-1;} int work () {int I, j, ans; ans = dij (); If (ANS =-1) printf ("Holiday/N"); else printf ("% d blips/N", ANS); Return 0;} int main () {While (scanf ("% d", & N, & M) {If (! N &&! M) break; ++ N, ++ m; Init (); input (); Work ();} return 0;} O (N ^ 2 ): </P> <p> # include <iostream> # include <cstdio> # include <algorithm> # include <memory. h >#include <cmath> # include <bitset> # include <queue> # include <vector> using namespace STD; const int border = (1 <20)-1; const int maxsize = 37; const int maxn = 1105; const int INF = 1000000000; # define CLR (x, y) memset (X, Y, sizeof (x )) # define add (x) x = (x + 1) & Border) # Define in (x) scanf ("% d", & X) # define out (x) printf ("% d/N", x) # define min (m, v) (m) <(v )? (M) :( v) # define max (M, V) (m)> (v )? (M) :( v) # define ABS (x)> 0? (X):-(x) # define Len 2520 typedef struct {int V, next; int val;} edge; typedef struct {int U; int val;} node; bool operator <(const node & A, const node & B) {return. val> B. val;} edge [maxn * maxn]; int n, m, start, end, index; int Dist [maxn], net [maxn]; bool visit [maxn]; void add_edge (const Int & U, const Int & V, const Int & SP) {edge [Index]. V = V; edge [Index]. next = net [u]; edge [Index]. val = sp; net [u] = index ++;} Void add_input (const Int & U, const Int & V, const Int & sp, const char & C) {If (C = '*') {add_edge (u, v, SP); add_edge (v, U, SP); return;} If (C = '>' | C = 'V') add_edge (u, v, SP); else add_edge (v, U, SP);} int Init () {Index = 0; CLR (net,-1); CLR (visit, 0 ); CLR (Dist, 127); Return 0;} int input () {int I, j, U, V, TMP, SP; char ch; for (I = 0; I <n-1; ++ I) {for (j = 0; j <m-1; ++ J) {// CIN> SP> CH; scanf ("% d % C", & sp, & Ch); If (sp = 0) continue; u = I * m + J; V = u + 1; add_input (u, v, SP, CH );} for (j = 0; j <m; ++ J) {// CIN> SP> CH; scanf ("% d % C", & sp, & Ch); If (sp = 0) continue; u = I * m + J; V = (I + 1) * m + J; add_input (u, v, SP, CH) ;}}for (j = 0; j <m-1; ++ J) {// CIN> SP> CH; scanf ("% d % C", & sp, & Ch); U = (n-1) * m + J; V = u + 1; if (sp = 0) continue; add_input (u, v, SP, CH);} return 0;} int dij () {int I, j, TMP, Mark, MMI N, V; int n = N * m; Dist [0] = 0; CLR (visit, 0); for (I = 0; I <n; ++ I) {mmin = inf; For (j = 0; j <n; ++ J) if (! Visit [J] & mmin> Dist [J]) {mmin = DIST [J]; Mark = J;} visit [Mark] = true; for (j = net [Mark]; J! =-1; j = edge [J]. Next) {TMP = Len/edge [J]. Val; V = edge [J]. V; If (! Visit [v] & Dist [v]> TMP + mmin) {Dist [v] = TMP + mmin ;}} if (Dist [n-1]> inf) return-1; return Dist [n-1];} int work () {int I, j, ans; ans = dij (); If (ANS =-1) printf ("Holiday/N"); else printf ("% d blips/N", ANS); Return 0;} int main () {While (scanf ("% d", & N, & M) {If (! N &&! M) break; ++ N, ++ m; Init (); input (); Work ();} return 0 ;}< br/> */
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