Created
May 13, 2015 06:47
-
-
Save sebastianvera/6b24c1c434e94a46761c to your computer and use it in GitHub Desktop.
UVA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
#define INF std::numeric_limits<int>::max() | |
using namespace std; | |
typedef struct { | |
int dest, weight; | |
} Edge; | |
typedef vector<Edge> Edges; | |
typedef vector<Edges> Graph; | |
int dist[1000]; | |
int main() { | |
int t; | |
cin >> t; | |
for(int i=0; i < t; i++) { | |
int n, m; | |
cin >> n >> m; | |
Graph graph(n); | |
bool possible = false; | |
fill(dist, dist+n, INF); | |
dist[0] = 0; | |
for (int j = 0; j < m; j++) { | |
Edge aux; | |
int src, dest, weight; | |
cin >> src >> dest >> weight; | |
aux.dest = dest; aux.weight = weight; | |
graph[src].push_back(aux); | |
} | |
for(int i = 0; i < (int)graph.size() && !possible; i++) { | |
for(auto edge : graph[i]) { | |
if (dist[i] + edge.weight < dist[edge.dest]) { | |
if (dist[edge.dest] != INF) { | |
possible = true; | |
break; | |
} | |
dist[edge.dest] = dist[i] + edge.weight; | |
} | |
} | |
} | |
if (possible) cout << "possible"; | |
else cout << "not possible"; | |
cout << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment