This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_B"
#include "../../../template/template.hpp"
#include "../../../graph/template.hpp"
#include "../../../graph/bellman-ford.hpp"
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,m,r;
cin >> n >> m >> r;
graph root(n);
rep(i, m){
int a,b,c;
cin >> a >> b >> c;
root[a].push_back({ a, b, c });
}
BellmanFord<int,min> bf(root);
bf.solve(r);
rep(i, n) if(bf.is_cycle(i)){
cout << "NEGATIVE CYCLE\n";
return 0;
}
rep(i, n){
if(bf[i] > (int)1e9) cout << "INF\n";
else cout << bf[i] << "\n";
}
}
#line 1 "test/aoj/GRL/GRL_1_B.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_B"
#line 1 "template/template.hpp"
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <bitset>
#include <cctype>
#include <climits>
#include <functional>
#include <cassert>
#include <numeric>
#include <cstring>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define per(i, n) for(int i = (n) - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
constexpr int mod = 1000000007;
using namespace std;
template<class T, class U>
bool chmax(T &a, const U &b){ return a < b ? (a = b, 1) : 0; }
template<class T, class U>
bool chmin(T &a, const U &b){ return a > b ? (a = b, 1) : 0; }
#line 4 "test/aoj/GRL/GRL_1_B.test.cpp"
#line 2 "graph/template.hpp"
/**
* @brief Graph Template
*/
template <class T>
struct Edge {
int from,to;
T cost;
int idx;
Edge(){};
Edge(int f, int t, T c=1, int i=-1) : from(f), to(t), cost(c), idx(i){}
Edge(int t) : to(t), from(-1), cost(1), idx(-1){}
operator int() const{ return to; }
bool operator<(const Edge &e){ return cost < e.cost; }
};
template <class T>
struct Graph : vector<vector<Edge<T>>> {
Graph(){}
Graph(const int &n) : vector<vector<Edge<T>>>(n){}
void add_edge(int a, int b, T c=1, int i=-1){
(*this)[a].push_back({ a, b, c, i });
}
};
using graph = Graph<int>;
#line 2 "graph/bellman-ford.hpp"
template <class T, const T&(*op)(const T&, const T&)>
struct BellmanFord {
BellmanFord(const Graph<T> &r): root(r), n(r.size()){
inf = -op(inf, -inf);
res.assign(n, inf);
}
void solve(const int s){
assert(0 <= s && s < n);
res[s] = 0;
for(int i = 1; i < n; i++) for(int j = 0; j < n; j++){
if(res[j] != inf) for(const auto &x : root[j]){
res[x] = op(res[x], res[j] + x.cost);
}
}
vector<int> loop(n);
for(int i = 0; i < n; i++) for(int j = 0; j < n; j++){
if(res[j] != inf) for(const auto &x : root[j]){
if(op(res[x] + op(1,-1), res[j] + x.cost) == res[j] + x.cost){
res[x] = res[j] + x.cost;
loop[x] = 1;
}
if(loop[j]) loop[x] = 1;
}
}
for(int i = 0; i < n; i++) if(loop[i]) res[i] = -inf;
}
const T &operator[](const int i) const{
assert(0 <= i && i < n);
return res[i];
}
bool is_cycle(const int i) const{
assert(0 <= i && i < n);
return res[i] == -inf;
}
private:
const Graph<T> &root;
vector<T> res;
int n;
T inf = numeric_limits<T>::max()-1;
};
#line 7 "test/aoj/GRL/GRL_1_B.test.cpp"
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n,m,r;
cin >> n >> m >> r;
graph root(n);
rep(i, m){
int a,b,c;
cin >> a >> b >> c;
root[a].push_back({ a, b, c });
}
BellmanFord<int,min> bf(root);
bf.solve(r);
rep(i, n) if(bf.is_cycle(i)){
cout << "NEGATIVE CYCLE\n";
return 0;
}
rep(i, n){
if(bf[i] > (int)1e9) cout << "INF\n";
else cout << bf[i] << "\n";
}
}