82tps!!!
查看原帖
82tps!!!
1273739
nice_names_give_dogs楼主2025/7/24 11:36

82tps求解

#include<bits/stdc++.h>
using namespace std;
const int N=100010;
int n,m,h[N],e[N],ne[N],dist[N],st[N],idx,w[N];
void add(int a,int b,int c){
	e[idx]=b;
	ne[idx]=h[a];
	w[idx]=c;
	h[a]=idx;
	idx++;
}
queue<int> q;
void spfa(){
	q.push(1);
	dist[1]=0;
	st[1]=1;
	while(q.size()){
		int t=q.front();
		st[t]=0;
		q.pop();
		for(int i=h[t];i!=-1;i=ne[i]){
			int j=e[i];
			if(dist[j]<dist[t]+w[i]){
				dist[j]=dist[t]+w[i];
				if(st[j]==0){
					st[j]=1;
					q.push(j);
				}
			}
		}
	}
	if(dist[n]==-0x3f3f3f3f) cout<<-1;
	else cout<<dist[n];
}
int main(){
	memset(h,-1,sizeof h);
	memset(dist,0x3f,sizeof dist);
	cin>>n>>m;
	while(m--){
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c);
	}
	spfa();
	return 0;
}
2025/7/24 11:36
加载中...