-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfsdfs - D.cpp
81 lines (73 loc) · 1.44 KB
/
bfsdfs - D.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const long sz = 30010;
ll mxdist;
int test;
int n;
ll u, v, w, s;
int cnt;
queue<ll> q;
bool visited[sz];
ll distOf[sz];
vector< pair<ll, ll> > adjList[sz];
void init(int n)
{
mxdist = -1;
for(int i=0; i<n; i++)
{
distOf[i] = 0;
visited[i] = false;
adjList[i].clear();
}
}
void mxDistFrom(ll source)
{
int u, v, w;
q.push(source);
while(q.empty()!=true)
{
u = q.front();
q.pop();
for(int i=0; i<adjList[u].size(); i++)
{
v = adjList[u][i].first;
w = adjList[u][i].second;
if(visited[v])
{
continue;
}
q.push(v);
distOf[v] = distOf[u] + w;
mxdist = max(distOf[v], mxdist);
}
visited[u] = true;
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
// freopen("_in.txt", "r", stdin);
cin >> test;
while(test--)
{
init(n);
cin >> n;
for(int i=1; i<n; i++)
{
cin >> u >> v >> w;
adjList[u].push_back(make_pair(v, w));
adjList[v].push_back(make_pair(u, w));
}
s = 0;
mxDistFrom(s);
for(int i=0; i<n; i++)
{
// init(n);
// mxDistFrom(i);
}
cout << "Case " << ++cnt << ": " << mxdist << "\n";
}
return 0;
}