#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <climits> #include <cstring> #include <cmath> #include <map> #include <set> using namespace std; const int INF = 10000000; int n,v,d[10000]; struct node { int f,e; int w; }; node a[10000]; void bllman_ford(int s){ for(int i = 1;i <= n;i++){ d[i] = INF; } d[s] = 0; for(int i = 0;i < n;i++){ int up = 0; for(int j = 0; j < v;j++){ if(d[a[j].f]!=INF && d[a[j].e] > (d[a[j].f] + a[j].w)){ d[a[j].e] = d[a[j].f] + a[j].w; up = 1; } } if(i == v && !up){ //负圈 printf("-1\n"); } } for(int i = 1; i <= n;i++){ printf("%d\n",d[i]); } } int main(){ while(cin >> n >> v){ for(int i = 0;i < v;i++){ cin >> a[i].f >> a[i].e >> a[i].w; } int s; cin >> s; bllman_ford(s); } return 0; }
时间: 2024-11-05 02:21:00