UVA 558 - Wormholes Solution
Problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=499
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- #define mx 2009
- #define inf INT_MAX
- typedef struct {
- int u, v, w;
- } Edge;
- Edge edge[mx];
- int n, m;
- int dis[mx];
- int BellmanFord(int source) {
- for (int i = 0; i < n; i++) {
- dis[i] = inf;
- }
- dis[source] = 0;
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < m; j++) {
- if (dis[edge[j].v] > dis[edge[j].u] + edge[j].w) {
- dis[edge[j].v] = dis[edge[j].u] + edge[j].w;
- }
- }
- }
- for (int j = 0; j < m; j++) {
- if (dis[edge[j].v] > dis[edge[j].u] + edge[j].w) {
- return 0;
- }
- }
- return 1;
- }
- int32_t main() {
- int t;
- cin >> t;
- while (t--) {
- cin >> n >> m;
- for (int i = 0; i < m; i++) {
- cin >> edge[i].u >> edge[i].v >> edge[i].w;
- }
- if (BellmanFord(0)) cout << "not ";
- cout << "possible" << endl;
- }
- return 0;
- }
No comments