path

path

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1100    Accepted Submission(s): 219


Problem Description
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 (1t100), denoting the number of the test cases.
The first line of each test case contains three positive integers n,m,q. (1n,m,q5104)

Each of the next m lines contains three integers ui,vi,wi, indicating that the ith edge is from ui to vi and weighted wi.(1ui,vin,1wi109)

Each of the next q lines contains one integer k as mentioned above.(1k5104)

It's guaranteed that Σn ,ΣmΣq,Σmax(k)2.5105 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

 

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1e5 + 10;
struct node{
    ll to,val;
    bool operator<(const node& s)const {
        return val>s.val;
    }
};
int T,n,m,q,query[maxn];
ll ans[maxn];
vector<pii>e[maxn];
priority_queue<node>Q;
multiset<int>s;
int main() {
    //freopen("1.txt", "r", stdin);
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d%d",&n,&m,&q);
        s.clear();
        while(!Q.empty())Q.pop();
        for(register int i=1;i<=n;++i)e[i].clear();
        for(register int i=1,u,v,w;i<=m;++i){
            scanf("%d%d%d",&u,&v,&w);
            e[u].emplace_back(make_pair(w,v));
            s.insert(w);
            Q.push(node{v,w});
        }
        for(register int i=1;i<=n;++i){
            sort(e[i].begin(),e[i].end());
        }
        int mx=-1;
        for(register int i=1,k;i<=q;++i){
            scanf("%d",&query[i]);
            mx=max(mx,query[i]);
        }
        int cnt=0;
        while(cnt<mx){
            node cur=Q.top();
            Q.pop();
            ans[++cnt]=cur.val;
            if(cnt>=mx)break;
            int u=cur.to;
            for(auto it:e[u]){
                ll v=it.second;
                ll d=cur.val+it.first;
                if(s.size()==mx){
                    auto po=--s.end();
                    if(d>=*po)break;
                    s.erase(po);
                    s.insert(d);
                }
                else{
                    s.insert(d);
                }
                Q.push(node{v,d});
            }
        }
        for(register int i=1;i<=q;++i){
            printf("%d\n",ans[query[i]]);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/czy-power/p/11404431.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值