AcWing 853. 有边数限制的最短路
原题链接
简单
作者:
dsyami
,
2021-06-04 21:31:20
,
所有人可见
,
阅读 236
#include <iostream>
#include <cstring>
using namespace std;
const int N = 510, M = 10010;
int n, m, k;
int dist[N], backup[N]; //backup[N]为回溯数组,记录上一次的结果
struct Egde
{
int a, b, w;
}edge[M];
void bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for(int i = 0; i < k; i ++ )
{
memcpy(backup, dist, sizeof dist);
for(int j = 1; j <= m; j ++ ) //松弛操作
{
int a = edge[j].a, b = edge[j].b, w = edge[j].w;
dist[b] = min(dist[b], backup[a] + w);
}
}
}
int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= m; i ++ )
{
int a, b, w;
cin >> a >> b >> w;
edge[i] = {a, b, w};
}
bellman_ford();
if(dist[n] > 0x3f3f3f3f / 2) cout << "impossible" << endl;
else cout << dist[n] << endl;
return 0;
}