70分,Bellman写错了嘛
  • 板块P1807 最长路
  • 楼主Rhss
  • 当前回复5
  • 已保存回复5
  • 发布时间2022/10/21 00:48
  • 上次更新2023/10/27 06:42:53
查看原帖
70分,Bellman写错了嘛
684890
Rhss楼主2022/10/21 00:48
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef pair<int,int> PLL;
const int N = 1505;
const int INF = 0x3fffffff;
vector<PLL> v[N];
int n,m;
int dist[N];
bool Bellman(int x){
	memset(dist,INF,sizeof dist);
	dist[x] = 0;
	for(int i = 0;i<n-1;i++){
		for(int u = 0;u<n;++u){
			for(int j = 0 ;j<v[u].size();++j){
				int q = v[u][j].first;
				int dis = v[u][j].second;
				if(dist[u] + dis < dist[q]){
					dist[q] = dist[u] + dis;
				}
			}
		}
	}
	for(int u = 0;u<n;++u){
		for(int j = 0;j<v[u].size();++j){
			int q = v[u][j].first;
			int dis = v[u][j].second;
			if(dist[u] + dis <dist[q]){
				return false;
			}
		}
	}
	return true;
}
int main(){
	cin>>n>>m;
	if(m==0){
		cout<<-1;
		return 0;
	}
	while(m--){
		int x,y,u;
		cin>>x>>y>>u;
		v[x].push_back({y,-u});
	}	
	if(Bellman(1)){
		cout<<-1 * dist[n];		
	}else{
		cout<<-1;
	}
	return 0;
}
2022/10/21 00:48
加载中...