| 比赛 | 2024暑假C班集训A | 评测结果 | AAAAAAAAAA | 
    | 题目名称 | 行动!行动! | 最终得分 | 100 | 
    | 用户昵称 | flyfree | 运行时间 | 0.473 s | 
    | 代码语言 | C++ | 内存使用 | 9.66 MiB | 
    | 提交时间 | 2024-07-10 10:53:45 | 
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
int hd[MAXN],nxt[MAXN],ed[MAXN],val[MAXN];
int dis[MAXN][15],used[MAXN][15];
int n,m,k,s,t,idx;
void build(int x,int y,int z){
    nxt[++idx]=hd[x];
    hd[x]=idx;
    val[idx]=z;
    ed[idx]=y;
}
struct node{
    int num,h;
};
bool operator <(const node a,const node b){
    return dis[a.num][a.h]>dis[b.num][b.h];
}
void dij(){
    priority_queue<node> q;
    memset(dis,127,sizeof(dis));
    for(int i=0;i<=k;i++){
        q.push((node){s,i});
        dis[s][i]=0;
    }
    while(!q.empty()){
        int fst=q.top().num,hz=q.top().h;
        q.pop();
        if(used[fst][hz])continue;
//        cout<<fst<<" "<<hz<<endl;
        used[fst][hz]=1;
        for(int i=hd[fst];i;i=nxt[i]){
            int y=ed[i];
            if(hz<k){
                dis[y][hz+1]=min(dis[y][hz+1],dis[fst][hz]);
                if(!used[y][hz+1]){
                    q.push((node){y,hz+1});
                }
            }
            dis[y][hz]=min(dis[y][hz],dis[fst][hz]+val[i]);
            if(!used[y][hz]){
                q.push((node){y,hz});
            }
        }
    }
}
int main(){
    freopen("move.in","r",stdin);
    freopen("move.out","w",stdout);
    cin>>n>>m>>k;
    cin>>s>>t;
    for(int i=1;i<=m;i++){
        int x,y,z;
        cin>>x>>y>>z;
        build(x,y,z);
        build(y,x,z);
    }
    dij();
    cout<<dis[t][k];
    return 0;
}