Acwing模板:https://www.acwing.com/problem/content/855/
请你求出从 1 号点到 \(n\) 号点的最多经过 \(k\) 条边的最短距离,如果无法从 1 号点走到 \(n\) 号点,输出 impossible。
#include <bits/stdc++.h>
using namespace std;
const int N = 510, M = 1e4 + 10, INF = 0x3f3f3f3f;
int n, m, k, d[N], t[N];
struct edge{
int u, v, w;
}e[M];
void bellman_ford(){
memset(d, 0x3f, sizeof d);
d[1] = 0;
for (int i = 0; i < k; i ++ ){
memcpy(t, d, sizeof d);
for (int j = 0; j < m; j ++ )
d[e[j].v] = min(d[e[j].v], t[e[j].u] + e[j].w);
}
}
int main(){
cin >> n >> m >> k;
for (int i = 0; i < m; i ++ ){
int u, v, w;
cin >> u >> v >> w;
e[i] = {u, v, w};
}
bellman_ford();
if (d[n] > INF / 2) cout << "impossible\n";
else cout << d[n] << "\n";
return 0;
}