SPFA pts63 WA+AC+RE+TLE五颜六色但看不出来
code
//分层图--肯定不会重边(不会重复走
//K条路免费(权为0 --分层图
//将原始图复制K次 i -- i+j*n(i<=j<=k)???(上课的板书 现在有点懵 怀疑是j:1->k)的节点
//上一层到下一层创建u-v的边权为0的边,所以 u->v (免费 //首先 同一层中u->v有路;
//ans---从0的起点S --- k层的目标节点T的 W(min;
#include <bits/stdc++.h>
using namespace std;
#define LL long long//头代码之一(确信
const LL N=1e5+10,M=1e6+10;
LL n,m,s,t,k;
LL h[N],enter[M],ne[M],idx,edge[M],dis[N];
int u[10010],v[10010],w[10010];//方便我 前链式(?? 邻接表建k层图
bool vis[N];
queue<LL> q;
void add(LL a,LL b,LL c)
{
edge[idx]=c;
ne[idx]=h[a];
enter[idx]=b;
h[a]=idx++;
}
LL SPFA (LL s,LL t)
{
memset(dis,0x3f,sizeof dis);
dis[s]=0;
q.push(s);
vis[s]=true;
while(q.size())
{
LL x=q.front();
q.pop();
vis[x]=false;
for(int i=h[x]; i!=-1; i=ne[i])
{
int y=enter[i];
if( dis[y] > dis[x]+edge[i] )
{
dis[y]=dis[x]+edge[i];
if(!vis[y])
{
q.push(y);
vis[y]=true;
}
}
}
}
return dis[t];
}
int main() {
memset(h,-1,sizeof h);
cin>>n>>m>>k;
cin>>s>>t;
for(int i=1; i<=m; i++)
{
cin>>u[i]>>v[i]>>w[i];
add(u[i],v[i],w[i]);
add(v[i],u[i],w[i]);
}
for(int i=1;i<=k;i++)//k层图
{
for(int j=1;j<=m;j++)
{
//同一层
add(u[j]+i*n,v[j]+i*n,w[j]);
add(v[j]+i*n,u[j]+i*n,w[j]);
//下一层
add(u[j]+(i-1)*n,v[j]+i*n,0);
add(v[j]+(i-1)*n,u[j]+i*n,0);
}
}
cout<<SPFA(s,t+k*n);
return 0;
}