有边数限制的最短路
作者:
橙外
,
2021-02-03 10:17:49
,
所有人可见
,
阅读 274
创一个复制数组,再不断更新路径,防止一直更新到尾
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
#define INF 5000000
#define N 10000
int dis[N],f[N],k,n,m;
struct edge
{
int a,b,w;
}e[N];
int main()
{
cin >>n >>m >>k;
for (int i = 1; i <= m; i++)
{
int a,b,w;
cin >>a >> b>> w;
e[i] = {a,b,w};
}
memset(dis,0x3f,sizeof dis);
dis[1] = 0;
for (int i = 1; i <= k; i++)
{
memcpy(f , dis, sizeof dis);
for (int j = 1; j <= m; j++)
{
int a = e[j].a,b = e[j].b,w = e[j].w;
if (f[a] != 0x3f3f3f3f && dis[b] > f[a] + w)
dis[b] = f[a] + w;
}
}
if (dis[n] == 0x3f3f3f3f)
{
puts("impossible");
return 0;
}
cout <<dis[n];
}