Language:DefaultMeteor Shower
Time limit:1000 ms |
|
Memory limit:65536 K |
Total submissions:9929 |
|
Accepted:2771 |
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into Earth and destroy anything they hit. anxious for her safety, She vows to find her way to a safe location (one that is never destroyed by a meteor ). she is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way. The reports say thatMMeteors (1 ≤M≤ 50,000) will strike, with meteorIWill striking point (XI,Yi) (0 ≤XI≤ 300; 0 ≤Yi≤ 300) at timeTi(0 ≤Ti≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points. Bessie leaves the origin at time 0 and can travel in the First quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. she cannot be located on a point at any time greater than or equal to the time it is destroyed ). Determine the minimum time it takes Bessie to get to a safe place. Input * Line 1: A single INTEGER:M * Lines 2 ..M+ 1: LineI+ 1 contains three space-separated integers:XI,Yi, AndTi Output * Line 1: the minimum time it takes Bessie to get to a safe place or-1 if it is impossible. Sample Input 40 0 22 1 21 1 20 3 5 Sample output 5 Source Usaco 2008 February silver |
Q: m meteorite hits the earth, and the coordinate and time of M meteorite impact are given. The adjacent four points of each meteorite impact point are also destroyed. Once destroyed, they cannot be left, ask how long it takes for a person at (0, 0) to go to the security point.
Idea: a place may be destroyed multiple times by several meteorite, so we can first find the earliest time for each point to be destroyed, and then BFs.
Code:
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # pragma comment (linker, "/Stack: 102400000,102400000") # define maxn 305 # define maxn 2005 # define mod 1000000009 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6 # define lson RT <1, L, mid # define rson RT <1 | 1, Mid + 1, R # define fre (I, a, B) for (I = A; I <= B; I ++) # define FRL (I, a, B) for (I = A; I <B; I ++) # define MEM (T, v) memset (t), V, sizeof (t )) # define SF (n) scanf ("% d", & N) # define SFF (a, B) scanf ("% d", & A, & B) # define sfff (a, B, c) scanf ("% d", & A, & B, & C) # define pf printf # define dbg PF ("Hi \ n") typedef long ll; using namespace STD; struct meteor {int X, Y; int t ;}; int dir [4] [2] = {,-, 0,-1}; int men Um; bool vis [maxn] [maxn]; int time [maxn] [maxn]; // bool isok (INT x, y), the earliest time bool isok (int x, int y) {If (x> = 0 & x <= maxn & Y> = 0 & Y <= maxn) return true; return false;} int BFS () {int I; If (time [0] [0] = 0) Return-1; // it is impossible to escape if the start point is destroyed from the very beginning, -1 Queue <Meteor> q; MEM (VIS, false); meteor St, now; ST. X = 0; ST. y = 0; ST. T = 0; vis [0] [0] = true; q. push (ST); While (! Q. empty () {ST = Q. front (); q. pop (); If (time [st. x] [st. y] = inf) return St. t; FRL (I, 0, 4) {now. X = ST. X + dir [I] [0]; now. y = ST. Y + dir [I] [1]; now. T = ST. t + 1; if (isok (now. x, now. Y )&&! Vis [now. x] [now. y] & now. T <time [now. x] [now. y]) // when it reaches the current point, it must not be accessed and the point has not been destroyed {vis [now. x] [now. y] = true; q. push (now) ;}} return-1 ;}int main () {int I, j, X, Y, T; while (~ SF (menum) {MEM (time, INF); FRL (I, 0, menum) {sfff (X, Y, t ); if (T <time [x] [Y]) time [x] [Y] = T; FRL (J, 0, 4) {int dx = x + dir [J] [0]; int DY = Y + dir [J] [1]; If (isok (dx, Dy) & time [dx] [dy]> T) time [dx] [dy] = T ;}} PF ("% d \ n", BFs ());} return 0;}/* 40 0 22 1 21 1 20 3 5 */
Meteor Shower (poj 3669 BFS)