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.

58 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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; // 1号点为出发点距离为0
// 小顶堆
priority_queue<PII, vector<PII>, greater<>> q;
q.push({0, 1});
while (q.size()) {
PII t = q.top();
q.pop();
int u = t.second;
if (st[u]) continue; // Dijkstra第一次出队列为最小值
st[u] = 1;
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;
}