853. 有边数限制的最短路 - AcWing题库
算法思路
#include<iostream>
#include<cstring>
using namespace std;
const int N=510,M=10010;
int dist[N],p[N];
int n,m,k;
struct Node{int a,b,w;
}node[M];
void bellman_ford()
{memset(dist,0x3f,sizeof dist);dist[1]=0;for(int i=0;i<k;i++){memcpy(p,dist,sizeof dist);for(int j=0;j<m;j++){int a=node[j].a,b=node[j].b,w=node[j].w;dist[b]=min(dist[b],p[a]+w);}}}
int main()
{cin>>n>>m>>k;for(int i=0;i<m;i++){int a,b,w;cin>>a>>b>>w;node[i]={a,b,w};}bellman_ford();if(dist[n]>0x3f3f3f3f/2)cout<<"impossible"<<endl;elsecout<<dist[n]<<endl;return 0;
}