Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define endl "\n"
- using namespace std;
- using ll = long long;
- using pii = pair<int, int>;
- constexpr int N = 2e3+5;
- int n, m, s, a, b;
- ll c, dist[N];
- struct Ed
- {
- int from, to;
- ll weight;
- Ed(int a, int b, ll c):
- from(a), to(b), weight(c){};
- };
- vector<Ed> gr;
- void Solve()
- {
- freopen("path.in", "r", stdin);
- freopen("path.out", "w", stdout);
- cin >> n >> m >> s;
- while(m--)
- {
- cin >> a >> b >> c;
- gr.emplace_back(a, b, c);
- }
- fill(dist, dist+n+1, LLONG_MAX);
- dist[s] = 0;
- for (int i = 0; i < n-1; i++)
- for (auto [a, b, c]: gr)
- if (dist[a]!=LLONG_MAX&&dist[a]+c<dist[b])
- dist[b] = dist[a]+c;
- for (int i = 0; i < n; i++)
- for (auto [a, b, c]: gr)
- if (dist[a]!=LLONG_MAX&&dist[a]+c<dist[b])
- dist[b] = -4e18;
- for (int i = 1; i <= n; i++)
- {
- if (dist[i]==LLONG_MAX)
- cout << '*';
- else if (dist[i]==-4e18)
- cout << '-';
- else cout << dist[i];
- cout << endl;
- }
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- Solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment