#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
struct NODE
{
int a;
int b;
int c;
};
const int N = 510, M = 10010;
int dist[N], last[N];
int n, m, k;
NODE node[M];
int bellman_ford()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for(int i = 0; i < k; i ++)
{
memcpy(last, dist, sizeof last);
for(int j = 0; j < m; j ++)
{
NODE t = node[j];
dist[t.b] = min(dist[t.b], last[t.a] + t.c);
}
}
if(dist[n] > 0x3f3f3f3f / 2) return -1;
return dist[n];
}
int main()
{
cin >> n >> m >> k;
for(int i = 0; i < m; i ++)
{
int a, b ,c;
cin >> a >> b >> c;
node[i] = {a, b, c};
}
if(bellman_ford() == -1)
{
puts("impossible");
}
else
{
cout << bellman_ford();
}
return 0;
}//来自acwing
853. 有边数限制的最短路(Bellman_ford算法)
最新推荐文章于 2021-09-11 18:04:32 发布
关键词由CSDN通过智能技术生成