蒟蒻求助63分

P2895 [USACO08FEB] Meteor Shower S

Xuzhenan520 @ 2024-08-25 16:25:03

https://www.luogu.com.cn/record/174682914 具体测试点情况

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
int M,Xi[MAXN],Yi[MAXN],Ti[1005],mp[305][305];
bool f[305][305];
struct N0DE{
    int x,y,t;
};
queue<N0DE> q;
int main(){
    memset(mp,-1,sizeof(mp));
    cin>>M;
    for(int i=1;i<=M;i++){
        cin>>Xi[i]>>Yi[i]>>Ti[i];
        if(mp[Xi[i]][Yi[i]]!=-1)mp[Xi[i]][Yi[i]]=min(mp[Xi[i]][Yi[i]],Ti[i]);
        else mp[Xi[i]][Yi[i]]=Ti[i];
        if(mp[Xi[i]+1][Yi[i]]!=-1)mp[Xi[i]+1][Yi[i]]=min(mp[Xi[i]+1][Yi[i]],Ti[i]);
        else mp[Xi[i]+1][Yi[i]]=Ti[i];
        if(mp[Xi[i]-1][Yi[i]]!=-1)mp[Xi[i]-1][Yi[i]]=min(mp[Xi[i]-1][Yi[i]],Ti[i]);
        else mp[Xi[i]-1][Yi[i]]=Ti[i];
        if(mp[Xi[i]][Yi[i]+1]!=-1)mp[Xi[i]][Yi[i]+1]=min(mp[Xi[i]][Yi[i]+1],Ti[i]);
        else mp[Xi[i]][Yi[i]+1]=Ti[i];
        if(mp[Xi[i]][Yi[i]-1]!=-1)mp[Xi[i]][Yi[i]-1]=min(mp[Xi[i]][Yi[i]-1],Ti[i]);
        else mp[Xi[i]][Yi[i]-1]=Ti[i];
    }
    N0DE sb;
    sb.x=0,sb.y=0,sb.t=0;
    q.push(sb);
    while(!q.empty()){
        N0DE tot=q.front(),k;
        f[tot.x][tot.y]=1;
        q.pop();
        if(mp[tot.x][tot.y]==-1){
            cout<<tot.t;
            return 0;
        }
        if((tot.t+1<mp[tot.x+1][tot.y]||mp[tot.x+1][tot.y]==-1)&&tot.x+1<=300&&f[tot.x+1][tot.y]==0){k.x=tot.x+1,k.y=tot.y,k.t=tot.t+1;q.push(k);}
        if((tot.t+1<mp[tot.x-1][tot.y]||mp[tot.x-1][tot.y]==-1)&&tot.x-1>=0&&f[tot.x-1][tot.y]==0){k.x=tot.x-1,k.y=tot.y,k.t=tot.t+1;q.push(k);}
        if((tot.t+1<mp[tot.x][tot.y+1]||mp[tot.x][tot.y+1]==-1)&&tot.y+1<=300&&f[tot.x][tot.y+1]==0){k.x=tot.x,k.y=tot.y+1,k.t=tot.t+1;q.push(k);}
        if((tot.t+1<mp[tot.x][tot.y-1]||mp[tot.x][tot.y-1]==-1)&&tot.y-1>=0&&f[tot.x][tot.y-1]==0){k.x=tot.x,k.y=tot.y-1,k.t=tot.t+1;q.push(k);}
    }
    cout<<-1;
    return 0;
}

|