71分求助

P2895 [USACO08FEB] Meteor Shower S

HDZmessi @ 2023-10-22 16:36:08

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a[3005][3005],vis[3005][3005],d[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
struct node{int x,y;};
struct NODE{node z;int t;};
queue<NODE> Q;
signed main(){
//  freopen("P2895_5.in","r",stdin);
//  freopen("P2895_5_1.out","w",stdout);
    cin>>n;
    memset(a,-1,sizeof a);
    for(int i=1;i<=n;i++){
        int u,v,w;cin>>u>>v>>w;
        u++,v++;
        a[u][v]=w;
        for(int i=0;i<4;i++){
            int x_a=u+d[i][0],y_a=v+d[i][1];
            if(x_a<1||y_a<1) continue;
            if(a[x_a][y_a]==-1) a[x_a][y_a]=w;
            else a[x_a][y_a]=min(a[x_a][y_a],w);
        }
    }
    Q.push({1,1,0});
    vis[1][1]=1;
    while(!Q.empty()){
        NODE u=Q.front();Q.pop();
    //  cerr<<u.z.x<<" "<<u.z.y<<" "<<u.t<<endl;
        if(a[u.z.x][u.z.y]==-1){
            cout<<u.t;
            return 0;
        }
        for(int i=0;i<4;i++){
            int x_a=u.z.x+d[i][0],y_a=u.z.y+d[i][1];
            if(x_a<1||y_a<1||vis[x_a][y_a]||(a[x_a][y_a]<=u.t+1&&a[x_a][y_a]!=-1)) continue;
            vis[x_a][y_a]=1;
            Q.push({x_a,y_a,u.t+1});
        }
    //  cerr<<ans<<endl;
    } 
    cout<<-1;
    return 0;
}

|