forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
benefact.cpp
53 lines (53 loc) · 1.29 KB
/
benefact.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 2009-05-07
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
int T,V,i,u,v,d;
scanf("%d",&T);
while (T--)
{
scanf("%d",&V);
vector<pair<int,int> > adj[50000];
for (i=0; i<V-1; i++)
{
scanf("%d %d %d",&u,&v,&d); u--; v--;
adj[u].push_back(make_pair(v,d));
adj[v].push_back(make_pair(u,d));
}
int bestv=0,bestd;
queue<pair<int,pair<int,int> > > Q;
bestd=0;
Q.push(make_pair(0,make_pair(-1,0)));
while (!Q.empty())
{
pair<int,pair<int,int> > P=Q.front(); Q.pop();
if (P.first>bestd)
{
bestd=P.first;
bestv=P.second.second;
}
for (i=0; i<adj[P.second.second].size(); i++)
if (adj[P.second.second][i].first!=P.second.first)
Q.push(make_pair(P.first+adj[P.second.second][i].second,make_pair(P.second.second,adj[P.second.second][i].first)));
}
bestd=0;
Q.push(make_pair(0,make_pair(-1,bestv)));
while (!Q.empty())
{
pair<int,pair<int,int> > P=Q.front(); Q.pop();
if (P.first>bestd)
{
bestd=P.first;
bestv=P.second.second;
}
for (i=0; i<adj[P.second.second].size(); i++)
if (adj[P.second.second][i].first!=P.second.first)
Q.push(make_pair(P.first+adj[P.second.second][i].second,make_pair(P.second.second,adj[P.second.second][i].first)));
}
printf("%d\n",bestd);
}
return 0;
}