Saleh127

LO 1094

Jan 14th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5. vector<ll>g[50000];
  6. vector<ll>cost[50000];
  7. bool v[50000];
  8. ll m,n,dis[50000];
  9.  
  10.  
  11. void bfs(ll x)
  12. {
  13. queue<ll>s;
  14. ll i,j,k,l;
  15. for(i=0; i<n; i++)
  16. {
  17. v[i]=0;
  18. dis[i]=10000000000;
  19. }
  20. s.push(x);
  21. v[x]=1;
  22. dis[x]=0;
  23. while(s.empty()==false)
  24. {
  25. ll node=s.front();
  26. s.pop();
  27. for(i=0; i<g[node].size(); i++)
  28. {
  29. ll cur=g[node][i];
  30. if(v[cur]==0)
  31. {
  32. v[cur]=1;
  33. dis[cur]=dis[node]+cost[node][i];
  34. s.push(cur);
  35. }
  36. }
  37. }
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43. ios_base::sync_with_stdio(0);
  44. cin.tie(0);
  45. cout.tie(0);
  46.  
  47. test
  48. {
  49. ll i,j,k,l=0,a,b,c;
  50. cin>>n;
  51. for(i=0; i<n-1; i++)
  52. {
  53. cin>>a>>b>>c;
  54. g[a].push_back(b);
  55. g[b].push_back(a);
  56.  
  57. cost[a].push_back(c);
  58. cost[b].push_back(c);
  59. }
  60. bfs(0);
  61. for(i=0; i<n; i++)
  62. {
  63. if(dis[i]>l)
  64. {
  65. m=i;
  66. l=dis[i];
  67. }
  68. }
  69. bfs(m);
  70. for(i=0; i<n; i++)
  71. {
  72. if(dis[i]>l)
  73. {
  74. l=dis[i];
  75. }
  76. }
  77. cout<<"Case "<<cs<<": "<<l<<endl;
  78. for(i=0; i<n; i++)
  79. {
  80. g[i].clear();
  81. cost[i].clear();
  82. }
  83. }
  84.  
  85.  
  86. return 0;
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment