Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <vector>
- #include <algorithm>
- #include <bits/stdc++.h>
- using namespace std;
- typedef struct node
- {
- int id;
- int weight;
- vector< pair<int, int> > neighbours;
- bool visited;
- } Node;
- Node graph[1001];
- void BellmanFord();
- int n_cases;
- int n_star_systems;
- int n_wormholes;
- int dist[1001];
- int main()
- {
- scanf("%d", &n_cases);
- while(n_cases--){
- scanf("%d %d", &n_star_systems, &n_wormholes);
- for(int i=0; i<n_wormholes; i++){
- int src_system, dest_system, weight;
- scanf("%d %d %d", &src_system, &dest_system, &weight);
- graph[src_system].id = src_system;
- graph[src_system].weight = INT_MAX;
- pair<int, int> p;
- p = make_pair(dest_system,weight);
- graph[src_system].neighbours.push_back(p);
- }
- graph[0].weight = 0;
- BellmanFord();
- }
- return 0;
- }
- void BellmanFord(){
- for(int i =0; i<n_star_systems-1;i++){
- for(int j = 0; j<(int)graph[i].neighbours.size(); j++){
- //int u = graph[i].id;
- int v = graph[i].neighbours[j].first;
- if(graph[i].weight != INT_MAX && graph[i].weight + graph[i].neighbours[j].second < graph[v].weight){
- graph[v].weight = graph[i].weight + graph[i].neighbours[j].second;
- }
- }
- }
- for(int i =0; i<n_star_systems-1;i++){
- for(int j = 0; j<(int)graph[i].neighbours.size(); j++){
- // int u = graph[i].id;
- int v = graph[i].neighbours[j].first;
- if(graph[i].weight != INT_MAX && graph[i].weight + graph[i].neighbours[j].second < graph[v].weight){
- printf("not possible\n");
- return;
- }
- }
- }
- printf("possible\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement