Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int long long
- using namespace std;
- const int maxen = 4e18;
- const int Size = 1e5;
- signed main()
- {
- freopen("path.in", "r", stdin);
- freopen("path.out", "w", stdout);
- int n, m, s;
- cin >> n >> m >> s;
- s--;
- vector <pair <int, pair<int, int>>> edges(Size);
- vector <int> d(Size, maxen);
- vector <int> path;
- vector <int> p(Size, -1);
- d[s] = 0;
- for (int i = 0; i < m; i++) {
- int u, v, weight;
- cin >> u >> v >> weight;
- u--;
- v--;
- edges[i].first = weight;
- edges[i].second.first = u;
- edges[i].second.second = v;
- }
- int checkOnLastIt;
- for (int i = 0; i < n + n; i++) {
- checkOnLastIt = -1;
- for (int j = 0; j < m; j++) {
- if (d[edges[j].second.first] < maxen) {
- if (d[edges[j].second.second] > d[edges[j].second.first] + edges[j].first) {
- d[edges[j].second.second] = max(-maxen, d[edges[j].second.first] + edges[j].first);
- p[edges[j].second.second] = edges[j].second.first;
- checkOnLastIt = edges[j].second.second;
- if (i > n - 1) {
- path.push_back(checkOnLastIt);
- }
- }
- }
- }
- }
- /*
- for (int i = 0; i < path.size(); i++) {
- cout << path[i] << ' ';
- }
- return 0;
- if (checkOnLastIt == -1){
- cout << "Negative cycle: ";
- for (int i = 0; i < path.size(); i++) {
- cout << path[i] << ' ';
- }
- } else {
- int otherCheckOnLastIt = checkOnLastIt;
- for (int i = 0; i < n; i++) {
- otherCheckOnLastIt = p[otherCheckOnLastIt];
- }
- for (int cur = otherCheckOnLastIt; ; cur = p[cur]) {
- path.push_back(cur);
- if (cur == otherCheckOnLastIt && path.size() > 1) {
- break;
- }
- }
- reverse (path.begin(), path.end());
- }
- */
- bool checkCont = true;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < path.size(); j++) {
- if (path[j] == i) {
- cout << '-' << '\n';
- checkCont = false;
- break;
- }
- }
- if (!checkCont) {
- checkCont = true;
- continue;
- }
- if (d[i] != maxen) {
- cout << d[i] << '\n';
- } else {
- cout << '*' << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement