Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <iostream>
  2. //floyd warshall with path
  3.  
  4. using namespace std;
  5.  
  6. struct adjprev
  7. {
  8. int a;
  9. int prev;
  10.  
  11. };
  12.  
  13. adjprev adj[10000][10000];
  14.  
  15. int main()
  16. {
  17. int v,e,temp1,temp2,w,q;
  18. cin>>v>>e>>q;
  19. for(int i=0;i<v;i++)
  20. {
  21. for(int j=0;j<v;j++)//initiating with infinity
  22. {
  23. adj[i][j].a=100000;
  24. }
  25. adj[i][i].a=0; //if they are equal
  26. adj[i][i].prev=i;
  27. }
  28. for(int i=0;i<e;i++) //initiating all edges
  29. {
  30. cin>>temp1>>temp2>>w;
  31. temp1--;
  32. temp2--;
  33. adj[temp1][temp2].a=w;
  34. }
  35.  
  36. for(int k=0;k<v;k++)
  37. for(int i=0;i<v;i++)
  38. for(int j=0;j<v;j++)
  39. {
  40. if(i==j)
  41. {
  42. continue;
  43. }
  44. //adj[i][j]=min(adj[i][j],adj[i][k]+adj[k][j]);
  45. if(adj[i][j].a>(adj[i][k].a+adj[k][j].a))
  46. {
  47. adj[i][j].a=adj[i][k].a+adj[k][j].a; //replacing
  48. adj[i][j].prev=k;
  49. }
  50. }
  51.  
  52. // for(int i=0;i<v;i++)
  53. // {
  54. // for(int j=0;j<v;j++)
  55. // {
  56. // cout<<i<<" "<<j<<" "<<adj[i][j].a<<endl; //cout all shortest paths;
  57. // }
  58. // }
  59. for(int i=0;i<q;i++)
  60. {
  61. cin>>temp1>>temp2;
  62. temp1--;
  63. temp2--;
  64. cout<<adj[temp1][temp2].a<<endl;
  65. cout<<endl;
  66. cout<<temp2+1<<endl;
  67. while(temp1!=temp2)
  68. {
  69. cout<<adj[temp1][temp2].prev+1<<endl;
  70. temp2=adj[temp1][temp2].prev-1;
  71. }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement