我写的二维SPFA(可能是我太菜了)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n,m,k,st,ed;
int dis[10001][11];
struct node
{
int cnt,k;
};
queue<node> q;
struct nodex
{
int nxt,to,val;
}edge[200005];
int head[10001];
int cnt;
bool vis[10001][11];
void add(int st,int ed,int val)
{
edge[++cnt].to=ed;
edge[cnt].nxt=head[st];
edge[cnt].val=val;
head[st]=cnt;
}
int read()
{
int x=0;
char ch=getchar();
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9')
{
x=(x<<3)+(x<<1)+(ch^48);
ch=getchar();
}
return x;
}
void spfa()
{
for(int i=0;i<=k;i++)
{
dis[st][i]=0;
q.push((node){st,i});
}
while(!q.empty())
{
node t=q.front();
q.pop();
vis[t.cnt][t.k]=false;
for(int i=head[t.cnt];i;i=edge[i].nxt)
{
int to=edge[i].to;
if(t.k)
{
if(dis[to][t.k-1]>dis[t.cnt][t.k])
{
dis[to][t.k-1]=dis[t.cnt][t.k];
if(!vis[to][t.k-1])
{
vis[to][t.k-1]=true;
q.push((node){to,t.k-1});
}
}
}
if(dis[to][t.k]>dis[t.cnt][t.k]+edge[i].val)
{
dis[to][t.k]=dis[t.cnt][t.k]+edge[i].val;
if(!vis[to][t.k])
{
vis[to][t.k]=true;
q.push((node){to,t.k});
}
}
}
}
}
int main()
{
memset(dis,0x7f,sizeof(dis));
n=read();
m=read();
k=read();
st=read()+1;
ed=read()+1;
for(int i=1;i<=m;i++)
{
int x,y,z;
x=read()+1;
y=read()+1;
z=read();
add(x,y,z);
add(y,x,z);
}
spfa();
int minn=0x7fffffff;
for(int i=0;i<=k;i++)
{
minn=min(dis[ed][i],minn);
}
printf("%d",minn);
return 0;
}