-
Notifications
You must be signed in to change notification settings - Fork 0
/
1167.cpp
55 lines (51 loc) · 1.22 KB
/
1167.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
#include <bits/stdc++.h>
#define PAIR pair<int, int>
#define W first
#define N second
using namespace std;
int V;
int st, en, w;
vector<PAIR> edge[100005];
int dist[100005];
void dijkstra(int st) {
memset(dist, -1, sizeof(dist));
priority_queue<PAIR, vector<PAIR>, greater<PAIR>> pq;
PAIR cur;
int nDist;
dist[st] = 0;
pq.push({0, st});
while (!pq.empty()) {
cur = pq.top();
pq.pop();
if (dist[cur.N] < cur.W)
continue;
for (auto next : edge[cur.N]) {
nDist = cur.W + next.W;
if (dist[next.N] == -1 || nDist < dist[next.N]) {
dist[next.N] = nDist;
pq.push({nDist, next.N});
}
}
}
}
int main(void) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin >> V;
for (int i = 0; i < V; i++) {
cin >> st;
while (1) {
cin >> en;
if (en == -1)
break;
cin >> w;
edge[st].push_back({w, en});
}
}
dijkstra(1);
int far_from_1 = max_element(dist, dist + V + 1) - dist;
dijkstra(far_from_1);
cout << *max_element(dist, dist + V + 1);
return 0;
}