91pts求调
查看原帖
91pts求调
1277550
cx2021hjxhjx楼主2024/7/18 15:35

rt

#include<bits/stdc++.h>
using namespace std;
struct node{
    int to,w,next;
}e[500010];
int num,head[500010],dis[500010],v[500010],n,m,s,x,y,z;
queue<int>q;
void add(int x,int y,int z){
    e[++num].to=y;
    e[num].w=z;
    e[num].next=head[x];
    head[x]=num;
}
void spfa(int s){
    v[s]=1;
    q.push(s);
    while(q.size()){
        int u=q.front();
        q.pop();
        v[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next) {
            int to=e[i].to;
            if(dis[to]<dis[u]+e[i].w){
                dis[to]=dis[u]+e[i].w;
                if(v[to]==0) {
                    q.push(to);
                    v[to]=1;
                }
            }
        }
    }
    if(dis[n]==0) cout<<-1;
    else cout<<dis[n];
}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	memset(head,-1,sizeof head);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        cin>>x>>y>>z;
        add(x,y,z);
    }
    spfa(1);
    return 0;
}
2024/7/18 15:35
加载中...