# 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

#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;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

Charon_HN

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
10-24
04-24

09-15
08-24 305
02-05 477