-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathT4 地铁修建.cpp
52 lines (47 loc) · 1.11 KB
/
T4 地铁修建.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
#include<bits/stdc++.h>
#define pa pair<int,int>
using namespace std;
struct Edge {
int v, w, nxt;
} edge[500000];
int head[100005], dis[100005], vis[100005];
int n, m, a, b, c, tot;
void add(int x, int y, int z) {
edge[tot].v = y;
edge[tot].w = z;
edge[tot].nxt = head[x];
head[x] = tot++;
}
void dij() {
priority_queue<pa, vector<pa >, greater<pa>> q;
q.push(make_pair(0, 1));
while (!q.empty()) {
int sta = q.top().second;
q.pop();
if (vis[sta]) continue;
vis[sta] = 1;
int v, w;
for (int i = head[sta]; i != -1; i = edge[i].nxt) {
v = edge[i].v;
w = edge[i].w;
if (dis[v] > max(w, dis[sta])) {
dis[v] = max(w, dis[sta]);
q.push(make_pair(dis[v], v));
}
}
}
}
int main() {
cin >> n >> m;
memset(head, -1, sizeof(head));
memset(dis, 0x3f3f3f3f, sizeof(dis));
for (int i = 0; i < m; i++) {
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
dis[1] = 0;
dij();
cout << dis[n] << endl;
return 0;
}