HDU 6705 path 2019ccpc网络赛

path

Time Limit: 2000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 921    Accepted Submission(s): 192


 

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:  6712 6711 6710 6709 6708 

 

题意:

有环有向图的第k短路径?

分析:

讲得很好了:https://blog.csdn.net/Ratina/article/details/100066384

 

 

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 50005;
struct Node
{
    LL v;
    int x,id;
    bool operator < ( const Node& b )const
    {
        return v >b.v;
    }
};
vector<pair<LL,int> >G[N];
priority_queue<Node> pq;
LL ans[N];
int q[N];



int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
      
        int n,m,k;
      
        scanf("%d%d%d",&n,&m,&k);
        
          while(pq.size())
            pq.pop();
        for( int i =0 ; i <= n; i++ )
            G[i].clear();
		int x,y;
		LL w; 
        for( int i = 1; i <= m; i++ )
        {
            scanf("%d%d%lld",&x,&y,&w);
            G[x].push_back({w,y});
        }
        
        int maxq = 0;
        for( int i = 1; i <= k; i++ )
        {
            scanf("%d",&q[i]);
            maxq = max(maxq,q[i] );
        }
        for( int i = 1; i <= n; i++ )
        {
            sort(G[i].begin(),G[i].end() );
        }
        for( int i = 1; i <= n; i++ )
        {
            if( G[i].size() )
                pq.push( {G[i][0].first,i,0} );
        }
        int cnt=0;
        
        while(!pq.empty())
		{
			Node a=pq.top();
			pq.pop();
			ans[++cnt]=a.v;
			if(cnt==maxq)
			{
				break;
			}
			
			if(a.id<G[a.x].size()-1)
			{
				pq.push({a.v-G[a.x][a.id].first+G[a.x][a.id+1].first,a.x,a.id+1});
			}
			
			int y=G[a.x][a.id].second;
			
			if(G[y].size())
			{
			  pq.push({a.v+G[y][0].first,y,0});	
			}
			
		}
        
        for( int i = 1; i <= k; i++ )
        {
            printf("%lld\n",ans[q[i]]   );
        }
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值