limimage

зфер

Feb 1st, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define endl "\n"
  4. using namespace std;
  5. using ll = long long;
  6. using pii = pair<int, int>;
  7.  
  8. constexpr int N = 2e3+5;
  9.  
  10. int n, m, s, a, b;
  11. ll c, dist[N];
  12.  
  13.  
  14. struct Ed
  15. {
  16. int from, to;
  17. ll weight;
  18. Ed(int a, int b, ll c):
  19. from(a), to(b), weight(c){};
  20. };
  21. vector<Ed> gr;
  22.  
  23.  
  24. void Solve()
  25. {
  26. freopen("path.in", "r", stdin);
  27. freopen("path.out", "w", stdout);
  28. cin >> n >> m >> s;
  29. while(m--)
  30. {
  31. cin >> a >> b >> c;
  32. gr.emplace_back(a, b, c);
  33. }
  34. fill(dist, dist+n+1, LLONG_MAX);
  35. dist[s] = 0;
  36. for (int i = 0; i < n-1; i++)
  37. for (auto [a, b, c]: gr)
  38. if (dist[a]!=LLONG_MAX&&dist[a]+c<dist[b])
  39. dist[b] = dist[a]+c;
  40. for (int i = 0; i < n; i++)
  41. for (auto [a, b, c]: gr)
  42. if (dist[a]!=LLONG_MAX&&dist[a]+c<dist[b])
  43. dist[b] = -4e18;
  44. for (int i = 1; i <= n; i++)
  45. {
  46. if (dist[i]==LLONG_MAX)
  47. cout << '*';
  48. else if (dist[i]==-4e18)
  49. cout << '-';
  50. else cout << dist[i];
  51. cout << endl;
  52. }
  53. }
  54.  
  55. int main()
  56. {
  57. ios_base::sync_with_stdio(false);
  58. cin.tie(nullptr);
  59. cout.tie(nullptr);
  60. Solve();
  61. return 0;
  62. }
Add Comment
Please, Sign In to add comment