Advertisement
NHumme

D

Apr 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cmath>
  5. #include <vector>
  6. #include <string>
  7. #include <set>
  8. #include <stack>
  9. #include <queue>
  10. #include <deque>
  11. using namespace std;
  12.  
  13. #define TASK "path"
  14.  
  15. const long long INF = 6e18;
  16.  
  17. struct edge {
  18.     long long a, b, w;
  19.     edge() {}
  20.     edge(long long aa, long long bb, long long ww) { a = aa, b = bb, w = ww; }
  21. };
  22.  
  23. long long n, m, v;
  24. vector < edge > e;
  25. vector < long long > gr[5000];
  26. vector < long long > d(5000, INF);
  27. bool used1[5000], used2[5000];
  28.  
  29. void dfs1(int now) {
  30.     used1[now] = true;
  31.     for (int i = 0; i < gr[now].size(); i++) {
  32.         if (!used1[gr[now][i]]) {
  33.             dfs1(gr[now][i]);
  34.         }
  35.     }
  36. }
  37.  
  38. void dfs2(int now) {
  39.     used2[now] = true;
  40.     for (int i = 0; i < gr[now].size(); i++) {
  41.         if (!used2[gr[now][i]]) {
  42.             dfs2(gr[now][i]);
  43.         }
  44.     }
  45. }
  46.  
  47. void ford() {
  48.     d[v] = 0;
  49.     for (int i = 0; i < n; i++) {
  50.         for (int j = 0; j < m; j++) {
  51.             if (d[e[j].a] < INF) {
  52.                 if (d[e[j].a] + e[j].w < d[e[j].b]) {
  53.                     if (i == n - 1) dfs2(e[j].a);
  54.                     d[e[j].b] = max(-INF, d[e[j].a] + e[j].w);
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
  60.  
  61. int main() {
  62.  
  63. #ifdef _DEBUG
  64.     freopen("debug.in", "r", stdin);
  65.     freopen("debug.out", "w", stdout);
  66. #else
  67.     freopen(TASK".in", "r", stdin);
  68.     freopen(TASK".out", "w", stdout);
  69.     //freopen("input.txt", "r", stdin);
  70.     //freopen("output.txt", "w", stdout);
  71. #endif // _DEBUG
  72.  
  73.     ios_base::sync_with_stdio(0);
  74.     cin.tie(0);
  75.     cout.tie(0);
  76.     cout.precision(6);
  77.  
  78.     long long a, b, c;
  79.     cin >> n >> m >> v;
  80.     v--;
  81.     for (int i = 0; i < m; i++) {
  82.         cin >> a >> b >> c;
  83.         e.push_back(edge(a - 1, b - 1, c));
  84.         gr[a - 1].push_back(b - 1);
  85.     }
  86.     dfs1(v);
  87.     ford();
  88.     for (int i = 0; i < n; i++) {
  89.         if (!used1[i]) {
  90.             cout << "*\n";
  91.             continue;
  92.         }
  93.         if (used2[i]) {
  94.             cout << "-\n";
  95.             continue;
  96.         }
  97.         cout << d[i] << "\n";
  98.     }
  99.  
  100.     return 0;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement