Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7.  
  8. typedef struct node
  9. {
  10. int id;
  11. int weight;
  12. vector< pair<int, int> > neighbours;
  13. bool visited;
  14. } Node;
  15. Node graph[1001];
  16. void BellmanFord();
  17. int n_cases;
  18. int n_star_systems;
  19. int n_wormholes;
  20. int dist[1001];
  21. int main()
  22. {
  23. scanf("%d", &n_cases);
  24. while(n_cases--){
  25. scanf("%d %d", &n_star_systems, &n_wormholes);
  26. for(int i=0; i<n_wormholes; i++){
  27. int src_system, dest_system, weight;
  28. scanf("%d %d %d", &src_system, &dest_system, &weight);
  29. graph[src_system].id = src_system;
  30. graph[src_system].weight = INT_MAX;
  31. pair<int, int> p;
  32. p = make_pair(dest_system,weight);
  33. graph[src_system].neighbours.push_back(p);
  34.  
  35. }
  36. graph[0].weight = 0;
  37. BellmanFord();
  38. }
  39. return 0;
  40. }
  41. void BellmanFord(){
  42. for(int i =0; i<n_star_systems-1;i++){
  43. for(int j = 0; j<(int)graph[i].neighbours.size(); j++){
  44. //int u = graph[i].id;
  45. int v = graph[i].neighbours[j].first;
  46. if(graph[i].weight != INT_MAX && graph[i].weight + graph[i].neighbours[j].second < graph[v].weight){
  47. graph[v].weight = graph[i].weight + graph[i].neighbours[j].second;
  48. }
  49. }
  50. }
  51.  
  52. for(int i =0; i<n_star_systems-1;i++){
  53. for(int j = 0; j<(int)graph[i].neighbours.size(); j++){
  54. // int u = graph[i].id;
  55. int v = graph[i].neighbours[j].first;
  56. if(graph[i].weight != INT_MAX && graph[i].weight + graph[i].neighbours[j].second < graph[v].weight){
  57. printf("not possible\n");
  58. return;
  59. }
  60. }
  61. }
  62. printf("possible\n");
  63.  
  64.  
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement