-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathboj.1167.cc
79 lines (69 loc) · 1.57 KB
/
boj.1167.cc
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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void)
{
int v;
scanf("%d",&v);
vector<vector<pair<int, int>>> edges(v);
for(int i = 0, node, a, b; i<v; ++i)
{
scanf("%d", &node);
--node;
while(true)
{
scanf("%d", &a);
if(a == -1)
{
break;
}
--a;
scanf("%d", &b);
edges[node].push_back(make_pair(a, b));
}
}
vector<int> distance(v, -1);
queue<int> q;
int maxNode = 0, maxLen = 0;
q.push(0);
distance[0] = 0;
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto avi: edges[node])
{
int av = avi.first, w = avi.second;
if(distance[av] != -1)
{
continue;
}
distance[av] = distance[node] + w;
q.push(av);
if(maxLen < distance[av])
{
maxNode = av, maxLen = distance[av];
}
}
}
fill(distance.begin(), distance.end(), -1);
maxLen = 0, distance[maxNode] = 0;
q.push(maxNode);
while(!q.empty())
{
int node = q.front();
q.pop();
for(auto avi: edges[node])
{
int av = avi.first, w = avi.second;
if(distance[av] != -1)
{
continue;
}
maxLen = max(maxLen, distance[av] = distance[node] + w);
q.push(av);
}
}
printf("%d", maxLen);
return 0;
}