谢谢回答的大佬,真的很需要帮助
#include<bits/stdc++.h>
using namespace std;
int n,m,k,s,t;
struct EDGE{
int floor;
int node;
int quan;
};
vector <EDGE> edge[15][10005];
struct NODE{
int floor;
int node;
int ans;
bool operator<(const NODE& other)const{
return ans>other.ans;
}
};
priority_queue <NODE> dcl;
int flag[15][10005]={},ans[15][10005];
void read(){
cin>>n>>m>>k>>s>>t;
for(int i=0,a,b,c;i<m;i++){
cin>>a>>b>>c;
edge[1][a].push_back({1,b,c});
edge[1][b].push_back({1,a,c});
for(int j=1;j<k;j++){
edge[j][a].push_back({j+1,b,0});
edge[j][b].push_back({j+1,a,0});
}
}
}
void work(){
dcl.push({1,s,0});
NODE cl;
while(!dcl.empty()){
cl=dcl.top();
if(cl.floor==k and cl.node==t){
cout<<cl.ans;
return ;
}
dcl.pop();
if(flag[cl.floor][cl.node]==1)continue;
flag[cl.floor][cl.node]=1;
for(int i=0;i<edge[cl.floor][cl.node].size();i++){
if(ans[edge[cl.floor][cl.node][i].floor][edge[cl.floor][cl.node][i].node]>cl.ans+edge[cl.floor][cl.node][i].quan){
ans[edge[cl.floor][cl.node][i].floor][edge[cl.floor][cl.node][i].node]=cl.ans+edge[cl.floor][cl.node][i].quan;
dcl.push({edge[cl.floor][cl.node][i].floor,edge[cl.floor][cl.node][i].node,ans[edge[cl.floor][cl.node][i].floor][edge[cl.floor][cl.node][i].node]});
}
if(cl.floor<k){
dcl.push({cl.floor+1,edge[cl.floor][cl.node][i].node,ans[cl.floor][cl.node]});
}
}
}
}
int main(){
memset(ans,0x3f3f3f,sizeof ans);
read();
work();
return 0;
}
感谢大佬耐心看完