-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1167.cpp
More file actions
83 lines (71 loc) · 1.44 KB
/
1167.cpp
File metadata and controls
83 lines (71 loc) · 1.44 KB
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
82
83
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
vector<pair<int, int>> edge[100005];
int main() {
int n, u, v, w, curr, dist, fnode, fdist, ans;
bool searchnd;
bool visit[100005];
queue<pair<int, int>> q;
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (int i = 1; i <= n; i++) {
visit[i] = false;
}
for (int i = 0; i < n; i++) {
cin >> u;
cin >> v;
while (v != (-1)) {
cin >> w;
edge[u].push_back({ v,w });
cin >> v;
}
}
fdist = -1;
q.push({ 1,0 });
visit[1] = true;
while (!q.empty()) {
curr = q.front().first;
dist = q.front().second;
q.pop();
searchnd = true;
for (int i = 0; i < edge[curr].size(); i++) {
if (visit[edge[curr].at(i).first]) {
continue;
}
q.push({ edge[curr].at(i).first,dist + edge[curr].at(i).second });
visit[edge[curr].at(i).first] = true;
searchnd = false;
}
if (searchnd) {
if (dist > fdist) {
fnode = curr;
fdist = dist;
}
}
}
ans = -1;
q.push({ fnode,0 });
for (int i = 1; i <= n; i++) {
visit[i] = false;
}
visit[fnode] = true;
while (!q.empty()) {
curr = q.front().first;
dist = q.front().second;
q.pop();
ans = max(ans, dist);
for (int i = 0; i < edge[curr].size(); i++) {
if (visit[edge[curr].at(i).first]) {
continue;
}
q.push({ edge[curr].at(i).first,dist + edge[curr].at(i).second });
visit[edge[curr].at(i).first] = true;
}
}
cout << ans;
return 0;
}