Advertisement
shabbyheart

find sortest path from maltistage graph

Dec 29th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int cost[100];
  4. int dest[100];
  5. class Node
  6. {
  7. public:
  8.     int destination;
  9.     int weight;
  10.     Node *next;
  11. };
  12. class Integer
  13. {
  14. public:
  15.     Node *head = NULL;
  16. };
  17. void addEdge(Integer arr[],int sorc,int dest,int w)
  18. {
  19.     Node *newNode = new Node();
  20.     newNode->destination = dest;
  21.     newNode->weight = w;
  22.     newNode->next = NULL;
  23.     if( arr[sorc].head == NULL)
  24.         arr[sorc].head = newNode;
  25.     else
  26.     {
  27.         Node *temp = arr[sorc].head;
  28.         while(temp->next != NULL)
  29.             temp = temp->next;
  30.         temp->next = newNode;
  31.     }
  32. }
  33. void display(Integer listt[], int v)
  34. {
  35.     for(int i =0; i<=v; i++)
  36.     {
  37.         cout<<"\n Adjacency list of vertex "<<i<<" -->";
  38.         while(listt[i].head != NULL)
  39.         {
  40.             cout<<" "<<listt[i].head->destination<<" "<<listt[i].head->weight;
  41.             listt[i].head = listt[i].head->next;
  42.         }
  43.     }
  44. }
  45. int shortestDist(Integer adj[],int n)
  46. {
  47.  
  48.     cost[n] = 0;
  49.     for (int j = n-1 ; j >=1 ; j--)
  50.     {
  51.         cout<<endl;
  52.         int mini = 55555;
  53.         int d=12;
  54.         Node *temp = adj[j].head;
  55.         while(temp != NULL)
  56.         {
  57.             int c;
  58.             c = temp->weight+cost[temp->destination];
  59.             if( c<mini)
  60.             {
  61.                 mini = c;
  62.                 d = temp->destination;
  63.             }
  64.             temp = temp->next;
  65.         }
  66.         cost[j] = mini;
  67.         cout<<j<<" "<<cost[j]<<" ";
  68.         dest[j] = d;
  69.         cout<<dest[j]<<endl;
  70.     }
  71.     return cost[1];
  72. }
  73. int main()
  74. {
  75.     freopen("input.txt","r",stdin);
  76.     int v;
  77.     cin>>v;
  78.     Integer arr[v];
  79.     for (int i = 0; i <= v; i++)
  80.         arr[i].head = NULL;
  81.     int m,n,w;
  82.     while(scanf("%d%d%d",&m,&n,&w) == 3)
  83.     {
  84.         addEdge(arr,m,n,w);
  85.     }
  86.     int ans =shortestDist(arr,v);
  87.     cout<<endl<< ans<<endl;
  88.     int temp = 1;
  89.     cout<<1<<" ";
  90.     for(int i=1; i<5; i++)
  91.     {
  92.         cout<<dest[temp]<<" ";
  93.         temp = dest[temp];
  94.     }
  95.  
  96. }
  97.  
  98. /*
  99. 12
  100.  
  101. 1 2 9
  102. 1 3 7
  103. 1 4 3
  104. 1 5 2
  105. 2 6 4
  106. 2 7 2
  107. 2 8 1
  108. 3 6 2
  109. 3 7 7
  110. 4 8 11
  111. 5 7 11
  112. 5 8 8
  113. 6 9 6
  114. 6 10 5
  115. 7 9 4
  116. 7 10 3
  117. 8 10 5
  118. 8 11 6
  119. 9 12 4
  120. 10 12 2
  121. 11 12 5
  122.  
  123. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement