fork download
  1. //Not my code. Correcting the code given at https://d...content-available-to-author-only...f.com/questions/92244/what-is-the-wrong-in-this-code
  2.  
  3. #include<bits/stdc++.h>
  4. #define pii pair<int,int>
  5. #define infinity 99999999999
  6. using namespace std;
  7.  
  8. long int n,m,u,v,w;
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15. long int dijkstra(int start, vector<pii>G[])
  16. {
  17. long int *dist = new long int[n+1];
  18.  
  19. priority_queue<pii,vector<pii>,greater<pii> >q;
  20.  
  21. for(int i=1; i<=n; i++)
  22. {
  23. dist[i]=infinity;
  24. }
  25.  
  26. q.push(pii(0,start));
  27. dist[start]=0;
  28.  
  29. while(!q.empty())
  30. {
  31. u=q.top().second;
  32. q.pop();
  33. for(int i=0; i<G[u].size();i++)
  34. {
  35. v=G[u][i].second;
  36. w=G[u][i].first;
  37. if(dist[u]+w<dist[v])
  38. {
  39. dist[v]=dist[u]+w;
  40. q.push(pii(dist[v],v));
  41. }
  42. }
  43. }
  44.  
  45. long int mx = 0;
  46. for(int i=1; i<=n; i++)
  47. {
  48. mx = max(mx, dist[i]);
  49. }
  50. return mx;
  51. free(dist);
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. int main()
  59. {
  60. ios::sync_with_stdio(false);
  61. cin>>n>>m;
  62. vector<pii>G[n+1];
  63. while(m--)
  64. {
  65. cin>>u>>v>>w;
  66. G[u].push_back(pii(w,v));
  67. G[v].push_back(pii(w,u));
  68. }
  69. long int ans=-infinity;
  70. for(int i=1; i<=n; i++) //Now start from each i, and take maximum of all values mx returned by your function.
  71. {
  72. int start = i;
  73. ans=max(ans,dijkstra(start,G));
  74. }
  75. cout<<ans<<endl;
  76. return 0;
  77.  
  78. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
-99999999999