Submission #2125865
Source Code Expand
#include <vector> #include <iostream> #include <utility> using namespace std; struct Edge { int from; int to; int cost; }; const long long inf = 1e18; long long n, m; bool has_neg_loop(long long n, const vector<Edge> &edges) { bool f = false; vector<long long> dist(n,0); for (int i=0; i<n; i++) { for (auto e: edges) { if (dist[e.from] != inf && dist[e.to] > dist[e.from] + e.cost) { dist[e.to] = dist[e.from] + e.cost; if (i==n-1) f |= true; } } } return f; } vector<long long> bellman_ford(vector<long long> &dist, const vector<Edge> &edges) { for (int i=0; i<n; i++) { for (auto e: edges) { if (dist[e.from] != inf && dist[e.to] > dist[e.from] + e.cost) { dist[e.to] = dist[e.from] + e.cost; } } } return dist; } int main () { cin >> n >> m; vector<Edge> edges(m); for (auto &e: edges) { int f, t, c; cin >> f >> t >> c; e = {f-1, t-1, -c}; } vector<long long> distance(n, inf); distance[0] = 0; distance = bellman_ford(distance, edges); assert(distance[n-1] != INF); if (has_neg_loop(n, edges)) { cout << "inf" << endl; } else { cout << -1*distance[n-1] << endl; } }
Submission Info
Submission Time | |
---|---|
Task | D - Score Attack |
User | odan |
Language | C++14 (GCC 5.4.1) |
Score | 0 |
Code Size | 1398 Byte |
Status | CE |
Compile Error
./Main.cpp: In function ‘int main()’: ./Main.cpp:53:29: error: ‘INF’ was not declared in this scope assert(distance[n-1] != INF); ^ ./Main.cpp:53:32: error: ‘assert’ was not declared in this scope assert(distance[n-1] != INF); ^