1. 程式人生 > >bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower

bzoj1611 / P2895 [USACO08FEB]流星雨Meteor Shower

iostream splay clu img src () code ont queue

P2895 [USACO08FEB]流星雨Meteor Shower

給每個點標記一下能夠走的最遲時間,藍後bfs處理一下

技術分享圖片
 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 310 16 const int d1[5]={1,0,-1,0,0}; 17 const int d2[5]={0,1,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(a.x);read(a.y);read(a.t); 26 ++a.x; ++a.y; 27 dan[a.x][a.y]=min(dan[a.x][a.y],a.t);
28 dan[a.x][a.y+1]=min(dan[a.x][a.y+1],a.t); 29 dan[a.x][a.y-1]=min(dan[a.x][a.y-1],a.t); 30 dan[a.x+1][a.y]=min(dan[a.x+1][a.y],a.t); 31 dan[a.x-1][a.y]=min(dan[a.x-1][a.y],a.t); 32 }//處理每個點能走的最遲時間 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 [USACO08FEB]流星雨Meteor Shower