P2895 [usaco 08feb] meteor shower
Mark the latest time that can be taken for each vertex, and then BFS will process it.
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <algorithm> 5 # include <cctype> 6 # include <queue> 7 # define re register 8 using namespace STD; 9 void read (Int & X) {10 char c = getchar (); X = 0; 11 while (! Isdigit (c) c = getchar (); 12 while (isdigit (c) x = (x <3) + (x <1) + (C ^ 48), c = getchar (); 13} 14 int min (int A, int B) {return a <B? A: B;} 15 # define n 31016 const int D1 [5] = {,-, 0}; 17 const int D2 [5] = {, 0, -1,0}; 18 struct node {int X, Y, T;} A; 19 queue <node> H; 20 int M, TP = 1, ANS, dan [N] [N], win; bool vis [N] [N]; 21 int main () {22 memset (Dan, 127, sizeof (DAN )); int INF = Dan [0] [0]; 23 read (m); 24 for (Re int I = 1; I <= m; ++ I) {25 read (. x); read (. y); read (. t); 26 ++. x; ++. y; 27 Dan [. x] [. y] = min (Dan [. x] [. y],. t); 28 Dan [. x] [. Y + 1] = min (Dan [. X] [. Y + 1],. t); 29 Dan [. x] [. y-1] = min (Dan [. x] [. y-1],. t); 30 Dan [. X + 1] [. y] = min (Dan [. X + 1] [. y],. t); 31 Dan [. x-1] [. y] = min (Dan [. x-1] [. y],. t); 32} // process the latest time for each vertex to go 33 H. push (node) {1, 1, 0}); vis [1] [1] = 1; 34 if (Dan [1] [1] = inf) win = 1, ans = 0; 35 while (! H. Empty ()&&! Win) {36 node u = H. Front (); H. Pop (); 37 for (Re int I = 0; I <5 &&! Win; ++ I) {38 int R1 = u. X + D1 [I], R2 = u. Y + D2 [I]; 39 if (R1 <1 | R2 <1 | R1> = n | r2> = N) continue; 40 if (vis [R1] [R2] | Dan [R1] [R2] <= u. t + 1) continue; 41 if (Dan [R1] [R2] = inf) win = 1, ANS = u. t + 1; 42 else h. push (node) {R1, R2, U. t + 1}), vis [R1] [R2] = 1; 43} 44} win? Printf ("% d", ANS): printf ("-1"); 45 return 0; 46}
View code
Bzoj1611/p2895 [usaco 08feb] meteor shower