hdu 6705 path(re,待更正)

 

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 (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

 

 

Source

2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6724 6723 6722 6721 6720 

题意:给你一个图,求第k小路径

思路:把每个点连的边从小到大排下序,然后将每个点最小的边放到优先队列,然后每次对最小的边弹出后进行扩展,扩展有两个方向,它要么再往后连它邻接一条最短的边,要么将这条换成父节点邻接的更大一点的边

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<math.h>
#define maxn 1005000
#define ll long long
#include<vector>
#include<queue>
using namespace std;
ll n,m,q;
struct node
{
    ll last,now,rnk,dis;
    friend bool operator<(node x,node y)
    {
        return x.dis>y.dis;
    }
};
vector<pair<ll,ll> >e[maxn];
void add(int u,int v,int w)
{
    e[u].push_back(make_pair(v,w));
}
bool cmp(pair<ll,ll>a,pair<ll,ll>b)
{
    return a.second<b.second;
}
ll query[maxn],Max=0,num[maxn];priority_queue<node>que;
void init()
{
    Max=0;
    for(int i=1;i<=n;i++) e[i].clear();
    while(!que.empty())
    {
        que.pop();
    }
}
void slove()
{
    node b;
    for(int i=1;i<=n;i++)
    {
        if(e[i].size())
        {
            b.dis=e[i][0].second;
            b.last=i;
            b.rnk=1;
            b.now=e[i][0].first;
            que.push(b);
        }
    }
    ll s=0;
    while(!que.empty())
    {
        node st=que.top();
        que.pop();
        num[++s]=st.dis;
        if(s>Max) break;
        if(e[st.now].size())
        {
            b.last=st.now;
            b.rnk=1;
            b.dis=st.dis+e[st.now][0].second;
            b.last=e[st.now][0].first;
            que.push(b);
        }
        if(st.rnk+1<=e[st.last].size())
        {
            b.last=st.last;
            b.dis=st.dis-e[st.last][st.rnk].second+e[st.last][st.rnk+1].second;
            b.now=e[st.last][st.rnk+1].first;
            b.rnk=st.rnk+1;
            que.push(b);
        }
    }
    for(int i=1;i<=q;i++)
    {
        printf("%lld\n",num[query[i]]);
    }

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        init();
        scanf("%lld %lld %lld",&n,&m,&q);
        for(int i=1;i<=m;i++)
        {
            ll u,v,w;
            scanf("%lld %lld %lld",&u,&v,&w);
            //add(i,i+1,i);
            add(u,v,w);
        }
        for(int i=1;i<=n;i++)
        {
            sort(e[i].begin(),e[i].end(),cmp);
        }
        for(int i=1;i<=n;i++)
        {
            ll x=i;
            scanf("%lld",&x);
            query[i]=x;
            Max=max(Max,x);
        }
        slove();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值