分层图
记f[i][j]表示在i,且还可以免费j次的最小代价,直接跑类似最短路即可
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#define N 10005
#define M 50001
using namespace std;
struct edge{int next,to,v;}e[M<<1];
const int INF = 0x7fffffff;
int ecnt=1, last[N], f[N][12], n, m, k, s, t;
bool vis[N][12];
void add(int a, int b, int c)
{
e[++ecnt]=(edge){last[a],b,c};
last[a]=ecnt;
}
struct node{int id, step;};
void SPFA()
{
queue<node> q;
memset(f,63,sizeof(f));
f[s][k]=0;
q.push((node){s,k});
while(!q.empty())
{
int x=q.front().id, step=q.front().step;
q.pop();
vis[x][step]=0;
for(int i = last[x]; i; i=e[i].next)
{
int y=e[i].to;
if(f[y][step]>f[x][step]+e[i].v)
{
f[y][step]=f[x][step]+e[i].v;
if(!vis[y][step])
{
vis[y][step]=1;
q.push((node){y,step});
}
}
if(step >= 1 && f[y][step-1]>f[x][step])
{
f[y][step-1]=f[x][step];
if(!vis[y][step-1])
{
vis[y][step-1]=1;
q.push((node){y,step-1});
}
}
}
}
}
int main()
{
scanf("%d%d%d%d%d",&n,&m,&k,&s,&t);
for(int i = 1, a, b, c; i <= m; i++)
scanf("%d%d%d",&a,&b,&c),add(a,b,c),add(b,a,c);
SPFA();
int ans=INF;
for(int i = 0; i <= k; i++)
ans=min(ans,f[t][i]);
printf("%d\n",ans);
}