有边数限制的最短路
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N=10010,M=10010;
int n,m,k;
int dist[N];//存储距离
int back[N];//备份
struct Edge
{
int a,b,w;
}edges[N];
int bellman_ford()
{
memset(dist,0x3f,sizeof dist);
dist[1]=0;
for(int j=0;j<k;j++)//一共走了k步
{
memcpy(back,dist,sizeof dist);//先存储下来,用存下来的去放松
for(int i=0;i<m;i++)//放松每条边
{
int a=edges[i].a,b=edges[i].b,w=edges[i].w;
dist[b]=min(dist[b],w+back[a]);
}
}
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,w;
cin>>a>>b>>w;
edges[i]={a,b,w};
}
int t=bellman_ford();
if(t==-1)puts("impossible");
else printf("%d\n",t);
return 0;
}