Advertisement
shabbyheart

Untitled

Dec 28th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Node{
  4. public:
  5. int destination;
  6. int weight;
  7. Node *next;
  8. };
  9. class Integer{
  10. public:
  11. Node *head = NULL;
  12. };
  13.  
  14. void addEdge(Integer arr[],int sorc,int dest,int w)
  15. {
  16. Node *newNode = new Node();
  17. newNode->destination = dest;
  18. newNode->weight = w;
  19. newNode->next = NULL;
  20. if( arr[sorc].head == NULL)
  21. arr[sorc].head = newNode;
  22. else
  23. {
  24. Node *temp = arr[sorc].head;
  25. while(temp->next != NULL)
  26. temp = temp->next;
  27. temp->next = newNode;
  28. }
  29. }
  30.  
  31. void display(Integer listt[], int v)
  32. {
  33. for(int i =0; i<=v; i++)
  34. {
  35. cout<<"\n Adjacency list of vertex "<<i<<" -->";
  36. while(listt[i].head != NULL)
  37. {
  38. cout<<" "<<listt[i].head->destination<<" "<<listt[i].head->weight;
  39. listt[i].head = listt[i].head->next;
  40. }
  41. }
  42.  
  43. }
  44. int shortestDist(int n)
  45. {
  46. int cost[100000];
  47. int dest[10000];
  48. cost[n] = 0;
  49. for (int j = n-1 ; j >=1 ; j--)
  50. {
  51.  
  52.  
  53.  
  54.  
  55.  
  56. dist[i] = min(dist[i], graph[i][j] +
  57. dist[j]);
  58. }
  59. }
  60. int main()
  61. {
  62. freopen("input.txt","r",stdin);
  63. int v;
  64. cin>>v;
  65. Integer arr[v];
  66.  
  67. /// eita na korle arr er vitore v+1 dite hobe
  68. for (int i = 0; i <= v; i++)
  69. arr[i].head = NULL;
  70.  
  71. /// getting input
  72. int m,n,w;
  73. while(scanf("%d%d%d",&m,&n,&w) == 3)
  74. {
  75. addEdge(arr,m,n,w);
  76. //addEdge(arr,n,m);
  77. }
  78. display(arr,v);
  79. int result = shortestDist(arr,v);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement