Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <set>
- #include <map>
- #include <string>
- #include <cmath>
- #include <climits>
- #include <iomanip>
- #include <stack>
- #include <queue>
- using namespace std;
- #define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define forn(i, n) for(int i = 0; i < (int)n; ++i)
- #define rofn(i, n) for(int i = (int) n - 1; i >= 0; --i)
- #define forab(i, a, b) for (int i = (int)a; i < (int)b; ++i)
- #define pb push_back
- #define mp make_pair
- #define all(a) a.begin(), a.end()
- #define rall(a) a.rbegin(), a.rend()
- #define vi(a, n) vector<int> a(n);
- typedef long long ll;
- const int N = 100000;
- const ll INF64 = 5e18 + 13;
- const int INF = 1e9 + 13;
- struct edge {
- ll a, b, cost;
- };
- int main()
- {
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif
- IOS;
- int n, m;
- int s;
- cin >> n >> m >> s;
- s--;
- vector<edge> e;
- vector<vector<int> > g(n);
- forn(i, m)
- {
- edge cur;
- cin >> cur.a >> cur.b >> cur.cost;
- cur.a--;
- cur.b--;
- g[cur.a].pb(cur.b);
- e.pb(cur);
- }
- vector<ll> d(n, INF64);
- int v = s;
- d[v] = 0;
- for (int i = 0;i < n - 1; ++i) {
- for (int j = 0; j < m; ++j)
- if (d[e[j].a] < INF64)
- if (d[e[j].b] > d[e[j].a] + e[j].cost)
- d[e[j].b] = d[e[j].a] + e[j].cost;
- }
- queue<int> q;
- for (int i = 0; i < 1; ++i) {
- for (int j = 0; j < m; ++j)
- if (d[e[j].a] < INF64)
- if (d[e[j].b] > d[e[j].a] + e[j].cost) {
- d[e[j].b] = max(1LL-INF, d[e[j].a] + e[j].cost);
- q.push(e[j].a);
- }
- }
- vector<bool> used(n);
- while (!q.empty())
- {
- int v = q.front();
- q.pop();
- for (int i = 0; i < g[v].size(); ++i)
- {
- int to = g[v][i];
- if (!used[to])
- {
- used[to] = true;
- d[to] = -INF64;
- q.push(to);
- }
- }
- }
- for (int i = 0; i < n; ++i)
- if (d[i] == INF64)
- cout << "*\n";
- else if (d[i] == -INF64)
- cout << "-\n";
- else
- cout << d[i] << endl;
- // вывод d, например, на экран
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement