1 条题解

  • 1
    @ 2025-11-27 17:20:45
    #include<bits/stdc++.h>
    using namespace std;
    const int N=2e5+10;
    int n,m,k,u,v,value,s,t;
    struct edge{
        int to,cost;
    };
    vector<edge> G[N];
    int dis[N];
    bool vis[N];
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    void dijkstra(){
        memset(dis,0x3f,sizeof(dis));
        dis[s]=0;
        q.push({0,s});
        while(q.size()){
            int u=q.top().second;
            q.pop();
            if(vis[u]) continue;
            vis[u]=1;
            for(auto it:G[u]){
                int v=it.to;
                int num=it.cost;
                if(dis[v]>dis[u]+num){
                    dis[v]=dis[u]+num;
                    q.push({dis[v],v});
                }
            }
        }
        return ;
    }
    int main(){
        ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);
        cin>>n>>m>>k;
        cin>>s>>t;
        for(int i=1;i<=m;i++){
            cin>>u>>v>>value;
            G[u].push_back({v,value});
            G[v].push_back({u,value});
            for(int j=1;j<=k;j++){
                G[u+(j-1)*n].push_back({v+j*n,0});
                G[v+(j-1)*n].push_back({u+j*n,0});
                G[u+j*n].push_back({v+j*n,value});
                G[v+j*n].push_back({u+j*n,value});
            }
        }
        for(int i=1;i<=k;i++){
            G[t+(i-1)*n].push_back({t+i*n,0});
        }
        dijkstra();
        cout<<dis[t+k*n];
        return 0;
    }
    

    信息

    ID
    8595
    时间
    1000ms
    内存
    125MiB
    难度
    6
    标签
    递交数
    37
    已通过
    9
    上传者