WA 73 SPFA 蒟蒻求助
  • 板块P1807 最长路
  • 楼主guoguo160
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/11/7 20:56
  • 上次更新2024/11/7 20:59:15
查看原帖
WA 73 SPFA 蒟蒻求助
1128559
guoguo160楼主2024/11/7 20:56

使用SPFA但是莫名其妙WA73,大佬求助!!!

提交记录

#include <bits/stdc++.h>

#define int long long 

using namespace std;

const int N=1510,M=50010;

struct edge{
	int to;
	int d;
};

vector<edge> e[N];

void addedge(int u,int v,int w){
	e[u].push_back((edge){v,w});
//	e[v].push_back((edge){u,w});
} 

int dist[N];
int n,m;
bool inque[N];

void init() {
	for(int i=1;i<N;++i){
		dist[i]=LLONG_MIN;
	}
}

void input() {
	cin>>n>>m;
	for(int i=1;i<=m;++i){
		int u,v,w;
		cin>>u>>v>>w;
		addedge(u,v,w);
	}
}

void spfa() {
	dist[1]=0;
	inque[1]=1;
	queue<int> q;
	q.push(1);
	while(!q.empty()) {
//		cerr<<1<<"\n";
		int p=q.front();
		q.pop();
		for(auto v:e[p]){
			if(dist[v.to]<dist[p]+v.d){
				dist[v.to]=dist[p]+v.d;
				if(!inque[v.to]){
					inque[v.to]=1;
					q.push(v.to);
				}
			}
		}
		inque[p]=0;
	}
}

void output(){
	if(dist[n]==LLONG_MIN){
		cout<<-1<<"\n";
	}
	else{
		cout<<dist[n]<<"\n";
	}
}

void solve() {
	input();
	spfa();
	output();
}

signed main() {
	solve();
	return 0;
}
2024/11/7 20:56
加载中...