0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-24 03:31:59 +00:00

853. 有边数限制的最短路

https://www.acwing.com/problem/content/submission/code_detail/6594231/
This commit is contained in:
Baoshuo Ren 2021-07-21 23:39:59 +08:00 committed by Baoshuo Ren
parent ed22c66a10
commit d9969cbf64
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

34
AcWing/853/853.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <bits/stdc++.h>
using namespace std;
struct node {
int u, v, w;
} g[10005];
int n, m, k, dist[505], backup[505];
void bellman_ford() {
memset(dist, 0x3f, sizeof(dist));
dist[1] = 0;
for (int i = 1; i <= k; i++) {
memcpy(backup, dist, sizeof(dist));
for (int j = 1; j <= m; j++) {
dist[g[j].v] = min(dist[g[j].v], backup[g[j].u] + g[j].w);
}
}
}
int main() {
cin >> n >> m >> k;
for (int i = 1; i <= m; i++) {
cin >> g[i].u >> g[i].v >> g[i].w;
}
bellman_ford();
if (dist[n] > 0x1f1f1f1f) {
cout << "impossible" << endl;
} else {
cout << dist[n] << endl;
}
return 0;
}