HDU - 6705 K短路(无源点) 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

path

You have a directed weighted graph with n vertexes and m
edges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.

Now there are q queries that you need to answer. Each of the queries is about the k-th minimum value of all the paths.
Input
The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
The first line of each test case contains three positive integers n,m,q. (1≤n,m,q≤5∗104)

Each of the next m lines contains three integers ui,vi,wi, indicating that the i−th edge is from ui to vi and weighted wi.(1≤ui,vi≤n,1≤wi≤109)

Each of the next q lines contains one integer k as mentioned above.(1≤k≤5∗104)

It’s guaranteed that Σn ,Σm, Σq,Σmax(k)≤2.5∗105 and max(k) won’t exceed the number of paths in the graph.
Output
For each query, print one integer indicates the answer in line.
Sample Input
1
2 2 2
1 2 1
2 1 2
3
4
Sample Output
3
3

Hint
1->2 value :1

2->1 value: 2

1-> 2-> 1 value: 3

2-> 1-> 2 value: 3
题意:
给你一个有向图,任意一个点和边都可以经过很多次,问你整个图中,任意起点终点的k短路的长度是多少?你需要回答q个询问,每个询问给一个k

分析:
没有起点和终点,那么我们就将所有的边放到优先级队列里面,建立一个最小堆,这样就可以从堆中取出最小边权的起点,然后去扩展这个起点的下一个边和下一个点的边,这样就可以找到前k条最小的边了

#pragma GCC optimize ("O2")
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
using namespace std;
const int N=5e4+1000;
const int INF=0x3f3f3f3f;
struct node{
    int id,now,to;
    ll dis;
    bool operator <(const node &a)const{
        return dis>a.dis;
    }
};
typedef pair<ll,int>P;
priority_queue<node>p;
vector<P>ve[N];
int ans[N],maxk,n,m,q,qq[N];
void dfs(){
    int cnt=0;
    while(!p.empty()){
        node u=p.top();
        p.pop();
        node t;
        ans[++cnt]=u.dis;
        if(cnt>=maxk) break;
        if(u.id+1<ve[u.now].size()){
            t.dis=u.dis-ve[u.now][u.id].first+ve[u.now][u.id+1].first;
            t.id=u.id+1;
            t.to=ve[u.now][u.id+1].second;
            t.now=u.now;
            p.push(t);
        }
        if(ve[u.to].size()){
            t.dis=u.dis+ve[u.to][0].first;
            t.id=0;
            t.now=u.to;
            t.to= ve[u.to][0].second;
            p.push(t);
        }
    }
}
int main()
{
//    #ifndef ONLINE_JUDGE
//    freopen("in.txt","r",stdin);
//    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    while(T--){
        while(!p.empty()) p.pop();
        scanf("%d%d%d",&n,&m,&q);
        for(int i=1;i<=n;i++) ve[i].clear();
        for(int i=1;i<=m;i++){int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        ve[u].push_back({w,v});
        }

        for(int i=1;i<=n;i++){
            if(ve[i].size()){
                sort(ve[i].begin(),ve[i].end());
                p.push({0,i,ve[i][0].second,ve[i][0].first});
            }
        }
        maxk=0;
        rep(i,1,q) scanf("%d",&qq[i]),maxk=max(maxk,qq[i]);
        dfs();
        rep(i,1,q){
        printf("%d\n",ans[qq[i]]);
        }
    }
    return 0;
}

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页