AcWing 853. 有边数限制的最短路
原题链接
简单
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 100010;
int n, m, k;
struct Edge{
int a,b,w;
}edges[N];
int dist[N], backup[N];
int 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 = 0;j < m;j++)
{
int a = edges[j].a, b = edges[j].b, w=edges[j].w;
dist[b] = min(dist[b], backup[a] + w);
}
}
if(dist[n] >= 0x3f3f3f3f/2) return -1;
return dist[n];
}
int main()
{
cin >> n >> m >> k; // n个点,最多经过k条边
for(int i = 0;i < m;i++)
{
int a,b,w;
cin >> a >> b >> w;
edges[i] = {a,b,w};
}
int s = bellman_ford();
if(s == -1) puts("impossible");
else cout << dist[n]<<endl;
return 0;
}