单源最短路板子,用spfa即可(题目数据小,好写)
#include <bits/stdc++.h>
#define sz(a, b) ((a + b - 1) / b)
#define all(_) _.begin(), _.end()
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using u32 = uint32_t;
const int N = 2510, M = 6210;
struct edge { int to; int w; int nxt; } e[M << 1];
int n, m, st, ed;
int h[N], dist[N], idx;
bitset<N> vis;
void add(int a, int b, int c) {
e[idx] = {b, c, h[a]};
h[a] = idx++;
}
int spfa() {
memset(dist, 0x3f, sizeof dist);
queue<int> q;
dist[st] = 0;
q.push(st);
vis[st] = true;
while(q.size()) {
int t = q.front();
q.pop();
vis[t] = false;
for(int i = h[t]; ~i; i = e[i].nxt) {
int j = e[i].to, w = e[i].w;
if(dist[j] > dist[t] + w) {
dist[j] = dist[t] + w;
if(!vis[j]) {
q.push(j);
vis[j] = true;
}
}
}
}
return dist[ed];
}
int main() {
cin.tie(nullptr) -> ios::sync_with_stdio(false);
memset(h, -1, sizeof h);
cin >> n >> m >> st >> ed;
for(; m --; ) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
int ans = spfa();
cout << ans << "\n";
}