77 lines
2.2 KiB
C++
77 lines
2.2 KiB
C++
#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;
|
||
} |