[bzoj2763][JLOI2011]飞行路线——分层图最短路
水题。不多说什么。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10010;
const int maxk = 15;
int n, m, k, s, t;
struct edge {
int to, value;
};
struct state {
int pos;
int k;
};
vector<edge> G[maxn];
int dist[maxn][maxk], inq[maxn][maxk];
void add_edge(int u, int v, int w) {
G[u].push_back((edge){v, w});
G[v].push_back((edge){u, w});
}
void spfa() {
memset(dist, 0x3f, sizeof(dist));
dist[s][0] = 0;
queue<state> q;
q.push((state){s, 0});
memset(inq, 0, sizeof(inq));
inq[s][0] = 1;
while (!q.empty()) {
state u = q.front();
q.pop();
inq[u.pos][u.k] = 0;
for (int i = 0; i < G[u.pos].size(); i++) {
edge &e = G[u.pos][i];
if (dist[e.to][u.k] > dist[u.pos][u.k] + e.value) {
dist[e.to][u.k] = dist[u.pos][u.k] + e.value;
if (!inq[e.to][u.k]) {
q.push((state){e.to, u.k});
inq[e.to][u.k] = 1;
}
}
if (u.k < k && dist[e.to][u.k + 1] > dist[u.pos][u.k]) {
dist[e.to][u.k + 1] = dist[u.pos][u.k];
if (!inq[e.to][u.k + 1]) {
q.push((state){e.to, u.k + 1});
inq[e.to][u.k + 1] = 1;
}
}
}
}
}
int main() {
// freopen("input", "r", stdin);
scanf("%d %d %d", &n, &m, &k);
scanf("%d %d", &s, &t);
while (m--) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
add_edge(a, b, c);
}
spfa();
int ans = 0x3f3f3f;
for (int i = 0; i <= k; i++)
ans = min(ans, dist[t][i]);
printf("%d\n", ans);
return 0;
}