-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneps183.cpp
More file actions
72 lines (57 loc) · 1.33 KB
/
Copy pathneps183.cpp
File metadata and controls
72 lines (57 loc) · 1.33 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
/*
* Contest : OBI 2018 - Fase 1
* Problem : Ilhas
* Link : https://neps.academy/br/exercise/183
*/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fastio ios::sync_with_stdio(0); cin.tie(0);
typedef pair<int, int> pii;
vector<vector<pii>> adj;
vector<int> dist;
vector<bool> visited;
int n, m;
void djikstra(int no) {
dist.assign(n + 1, INT_MAX);
visited.assign(n + 1, false);
priority_queue<pii, vector<pii>, greater<pii>> q;
dist[no] = 0;
q.push({0, no});
while (!q.empty()) {
int curr = q.top().second;
q.pop();
if (visited[curr]) continue;
visited[curr] = true;
for (auto &V : adj[curr]) {
int v = V.first, w = V.second;
if (dist[curr] + w < dist[v]) {
dist[v] = dist[curr] + w;
q.push({dist[v], v});
}
}
}
}
int main() {
fastio
cin >> n >> m;
adj.resize(n + 1);
int u, v, p;
while (m--) {
cin >> u >> v >> p;
adj[u].push_back({v, p});
adj[v].push_back({u, p});
}
int s; cin >> s;
djikstra(s);
int maior = dist[1], menor = INT_MAX;
for (int i = 1; i <= n; ++i) {
maior = max(maior, dist[i]);
if (dist[i]) menor = min(menor, dist[i]);
cerr << dist[i] << " ";
}
cerr << "\n";
cout << abs(menor - maior) << "\n";
cerr << "maior: " << maior << " menor: " << menor << "\n";
return 0;
}