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.

77 lines
2.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> PII;
const int N = 1e4 * 2 * 11 + 10; //节点数1e4,无向图1e4*2,共k+1(k<=10)层:1e4*2*11
const int M = N << 1; //边数
//邻接表
int e[M], h[N], idx, w[M], ne[M];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
int dist[15][N], st[15][N];
int n, m, s, t, k;
void dijkstra() {
priority_queue<PII, vector<PII>, greater<PII>> q;
dist[0][s] = 0; //第零层起点s
q.push({0, s});
while (q.size()) {
int u = q.top().second;
q.pop();
int r = u / n; //行
u %= n; //列
if (st[r][u]) continue;
st[r][u] = true;
//更新同行,不使用免费机会
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (st[r][j]) continue;
if (dist[r][j] > dist[r][u] + w[i]) {
dist[r][j] = dist[r][u] + w[i];
q.push({dist[r][j], j + r * n});
}
}
//更新下一行,使用免费机会
if (r < k) {
//出发点u,目标点下一行的j位置
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (st[r + 1][j]) continue; //下一行j列走过吗?
if (dist[r + 1][j] > dist[r][u]) { //从r,u直接通过零成本过来 (r+1,j)
dist[r + 1][j] = dist[r][u];
q.push({dist[r + 1][j], j + (r + 1) * n});
}
}
}
}
}
int main() {
while (~scanf("%d%d%d", &n, &m, &k)) {
memset(h, -1, sizeof(h));
memset(dist, 0x3f, sizeof(dist));
memset(st, false, sizeof(st));
idx = 0;
scanf("%d%d", &s, &t);
while (m--) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
dijkstra();
int ans = INF;
for (int i = 0; i <= k; i++)
ans = min(ans, dist[i][t]);
printf("%d\n", ans);
}
return 0;
}