分层图,实在找不出哪里错了,求大佬相助qwq。
#include <iostream>
#include <queue>
using namespace std;
const int N=1e4+5,M=2e5+5,inf=0x3f3f3f3f;
struct edge{
int to,nxt,val;
}e[M];
int n,m,k,u,v,w,t,ans=inf;
int head[N],top;
int dist[N],vis[N]; // 链式前向星存图
priority_queue<pair<int,int>> q;
inline int read(){
int X=0; bool flag=1; char ch=getchar();
while(ch<'0' || ch>'9'){if(ch=='-') flag=0; ch=getchar();}
while(ch>='0' && ch<='9') X=(X<<1)+(X<<3)+ch-'0',ch=getchar();
if(flag) return X;
return ~(X-1);
}
inline void write(int X){
if(X<0) putchar('-'),X=~(X-1);
int s[20],top=0;
while(X) s[++top]=X%10,X/=10;
if(!top) s[++top]=0;
while(top) putchar(s[top--]+'0');
putchar('\n');
}
void add(int u,int v,int w){
top++;
e[top].to=v;
e[top].val=w;
e[top].nxt=head[u];
head[u]=top;
}
void dijkstra(){ // 板子,不多说
for(int i=1,l=n*k+n; i<=l; i++){
dist[i]=inf;
}
dist[1]=0;
q.push(make_pair(0,1));
while(!q.empty()){
u=q.top().second;
q.pop();
vis[u]=1;
for(int i=head[u]; i; i=e[i].nxt){
v=e[i].to;
if(!vis[v] && dist[v]>dist[u]+e[i].val){
dist[v]=dist[u]+e[i].val;
q.push(make_pair(dist[v],v));
}
}
}
}
int main(){
n=read(),m=read(),k=read();
while(m--){
u=read(),v=read(),w=read();
for(int i=0; i<k; i++){ // 先建 k 层,并将相邻两层相连
t=i*n;
add(u+t,v+t,w);
add(v+t,u+t,w);
add(u+t,v+t+n,w/2);
add(v+t,u+t+n,w/2); // 分层图建边
}
t=k*n; // 第 k+1 层
add(u+t,v+t,w);
add(v+t,u+t,w);
}
dijkstra();
for(int i=1; i<=k+1; i++){
ans=min(ans,dist[i*n]); // 取每一层的最短路
}
write(ans);
return 0;
}