You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.2 KiB
56 lines
1.2 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef pair<int, int> PII;
|
|
const int INF = 0x3f3f3f3f;
|
|
const int N = 110;
|
|
const int M = 2 * 210; // 无向图,需要开二倍的数组长度!
|
|
|
|
int n, m;
|
|
int h[N], e[M], w[M], ne[M], idx;
|
|
void add(int a, int b, int c) {
|
|
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
int dis[N];
|
|
bool st[N];
|
|
|
|
int dijkstra() {
|
|
memset(dis, 0x3f, sizeof dis);
|
|
dis[1] = 0;
|
|
|
|
priority_queue<PII, vector<PII>, greater<int>> q;
|
|
q.push({0, 1});
|
|
|
|
while (q.size()) {
|
|
PII t = q.top();
|
|
q.pop();
|
|
int u = t.second;
|
|
if (st[u]) continue;
|
|
st[u] = true;
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int v = e[i];
|
|
if (dis[v] > dis[u] + w[i]) {
|
|
dis[v] = dis[u] + w[i];
|
|
q.push({dis[v], v});
|
|
}
|
|
}
|
|
}
|
|
int mx = 0;
|
|
for (int i = 1; i <= n; i++) {
|
|
if (dis[i] == INF) return -1;
|
|
mx = max(mx, dis[i]);
|
|
}
|
|
return mx;
|
|
}
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
while (m--) {
|
|
int a, b, c;
|
|
cin >> a >> b >> c;
|
|
add(a, b, c), add(b, a, c);
|
|
}
|
|
printf("%d\n", dijkstra());
|
|
return 0;
|
|
}
|