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.
52 lines
1.1 KiB
52 lines
1.1 KiB
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 2510;
|
|
const int M = 6200 * 2 + 10;
|
|
|
|
typedef pair<int, int> PII;
|
|
|
|
int h[N], w[M], e[M], ne[M], idx;
|
|
bool st[N];
|
|
int dis[N];
|
|
|
|
void add(int a, int b, int c) {
|
|
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
|
|
}
|
|
|
|
int n, m, S, T;
|
|
|
|
int dijkstra() {
|
|
memset(dis, 0x3f, sizeof dis);
|
|
dis[S] = 0;
|
|
|
|
priority_queue<PII, vector<PII>, greater<PII>> q;
|
|
q.push({0, S});
|
|
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});
|
|
}
|
|
}
|
|
}
|
|
return dis[T];
|
|
}
|
|
|
|
int main() {
|
|
cin >> n >> m >> S >> T;
|
|
memset(h, -1, sizeof h);
|
|
|
|
while (m--) {
|
|
int a, b, c;
|
|
cin >> a >> b >> c;
|
|
add(a, b, c), add(b, a, c);
|
|
}
|
|
printf("%d\n", dijkstra());
|
|
return 0;
|
|
} |