This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B"
#include "../../code/graphs/bellmanFord.cc"
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int E, r;
cin >> V >> E >> r;
edges.resize(E);
F0R(i, E) {
int u, v, d;
cin >> u >> v >> d;
edges.eb(u, v, d);
}
vi res = bellmanFord(r);
if(!SZ(res)) {
cout << "NEGATIVE CYCLE\n";
} else {
for(int d : res) {
if(d == inf)
cout << "INF\n";
else
cout << d << endl;
}
}
}
#line 1 "tests/aoj/bellman_ford.single_source_shortest_path_negative_edges.test.cpp"
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_B"
#line 1 "code/template.cc"
// this line is here for a reason
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define fi first
#define se second
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define endl '\n'
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define SZ(x) (int)(x).size()
#define FOR(a, b, c) for (auto a = (b); (a) < (c); ++(a))
#define F0R(a, b) FOR (a, 0, (b))
template <typename T>
bool ckmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <typename T>
bool ckmax(T& a, const T& b) { return a < b ? a = b, true : false; }
#ifndef DEBUG
#define DEBUG 0
#endif
#define dout if (DEBUG) cerr
#define dvar(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#line 2 "code/graphs/bellmanFord.cc"
const int inf = 1e9;
// vertex a, vertex b, distance
vector<tuple<int, int, int>> edges;
int V;
// Returns empty vector on negative cycle
vi bellmanFord(int start) {
vi dist(V, inf);
dist[start] = 0;
bool negCycle = false;
F0R (i, V) {
negCycle = false;
for (auto [a, b, d] : edges)
if (dist[a] < inf && ckmin(dist[b], dist[a] + d))
negCycle = true;
}
return negCycle ? vi() : dist;
}
#line 4 "tests/aoj/bellman_ford.single_source_shortest_path_negative_edges.test.cpp"
int main() {
cin.tie(0);
ios_base::sync_with_stdio(0);
int E, r;
cin >> V >> E >> r;
edges.resize(E);
F0R(i, E) {
int u, v, d;
cin >> u >> v >> d;
edges.eb(u, v, d);
}
vi res = bellmanFord(r);
if(!SZ(res)) {
cout << "NEGATIVE CYCLE\n";
} else {
for(int d : res) {
if(d == inf)
cout << "INF\n";
else
cout << d << endl;
}
}
}