49 lines
850 B
C++
49 lines
850 B
C++
#include <iostream>
|
|
#include <tuple>
|
|
#include <vector>
|
|
#include <climits>
|
|
using namespace std;
|
|
const int INF = INT_MAX >> 1;
|
|
int main(){
|
|
int n, edge_num, start;
|
|
cin >> n >> edge_num >> start;
|
|
|
|
//不需要adj, 需要dp 和 edges
|
|
vector<tuple<int, int , int>> edges(edge_num);// p1, p2, w
|
|
vector<int> dp(n, INF);
|
|
dp[start] = 0;
|
|
|
|
//输入
|
|
for(int i = 0; i < edge_num; i++){
|
|
int a, b, w;
|
|
cin >> a >> b >> w;
|
|
edges[i] = {a, b, w};
|
|
}
|
|
|
|
//先进行n - 1次收缩
|
|
for(int i = 0; i < n - 1; i++){
|
|
for(auto [p1, p2, w] : edges){
|
|
if(dp[p2] > dp[p1] + w){
|
|
dp[p2] = dp[p1] + w;
|
|
}
|
|
}
|
|
}
|
|
|
|
//是否有负环
|
|
bool negcc = false;
|
|
for(auto [p1, p2, w] : edges){
|
|
if(dp[p2] > dp[p1] + w){
|
|
negcc = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if(negcc) cout << "Negative Circle Detected" << endl;
|
|
else{
|
|
for(auto x : dp){
|
|
cout << x << " ";
|
|
}
|
|
}
|
|
return 0;
|
|
}
|