题目:http://acm.hdu.edu.cn/showproblem.php?pid=6705

path

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

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
 
Recommend
liuyiding

题意:

给n个点和m条条有权边.路径值为路径中的边权和,问第k小条的路径值为多少

思路:

考虑BFS,如果路径不止一条能知道路径值最小的路径,就能推出第2小的,由第2小推出第3小,由第k-1小推出第k小
先把边权排序,这样可以从边权最小的开始访问
搜索队列用优先队列可以每次取出路径和最小的,记录下答案,现在记录过当前点连接当前邻接点的答案了,就搜索当前点不连这个邻接点而是下一个邻接点的情况和当前邻接点连接下一个边权最小的当前邻接点的邻接点的情况
最后输出答案

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define fi first
#define se second
typedef long long ll;
typedef pair<ll,int> pii;
const int amn=5e4+;
vector<pii> eg[amn];
struct node{
int u,v;
ll w;
node(int uu,int vv,ll ww){u=uu;v=vv;w=ww;}
bool operator<(const node a)const{return w>a.w;}
};
priority_queue<node> pq;
int n,m,q,u,v,k[amn],maxk;
ll w,ans[amn];
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++)eg[i].clear(); ///初始化vector
while(pq.size())pq.pop(); ///初始化优先队列
for(int i=;i<=m;i++){
scanf("%d%d%lld",&u,&v,&w);
eg[u].pb(pii(w,v)); ///pair的first存边权是因为排序时先按first排再按second排
}
for(int i=;i<=n;i++)sort(eg[i].begin(),eg[i].end()); ///对每个节点的邻接点按边权升序排序
maxk=;
for(int i=;i<=q;i++){
scanf("%d",&k[i]); ///记录询问顺序,下面离线处理答案
maxk=max(maxk,k[i]); ///记录一个最大的第k小,就要搜索到这里
}
for(int i=;i<=n;i++)
if(eg[i].size())
pq.push(node(i,,eg[i][].fi)); ///把每个点边权最小的邻接点加入搜索队列
int tp=;
while(pq.size()){
node cu=pq.top();pq.pop();
ans[++tp]=cu.w; ///记录路径和第tp小的答案
if(tp>=maxk)break; ///知道了最大的第k小的路径就不用搜索了
if(cu.v+<eg[cu.u].size())pq.push(node(cu.u,cu.v+,cu.w-eg[cu.u][cu.v].fi+eg[cu.u][cu.v+].fi)); ///现在记录过当前点连接当前邻接点的答案了,就搜索当前点不连这个邻接点而是下一个邻接点的情况
int v=eg[cu.u][cu.v].se;
if(eg[v].size())pq.push(node(v,,cu.w+eg[v][].fi)); ///当前邻接点连接下一个边权最小的当前邻接点的邻接点的情况
}
for(int i=;i<=q;i++)printf("%lld\n",ans[k[i]]); ///按询问顺序输出答案
}
}
/**
给n个点和m条条有权边.路径值为路径中的边权和,问第k小条的路径值为多少
考虑BFS,如果路径不止一条能知道路径值最小的路径,就能推出第2小的,由第2小推出第3小,由第k-1小推出第k小
先把边权排序,这样可以从边权最小的开始访问
搜索队列用优先队列可以每次取出路径和最小的,记录下答案,现在记录过当前点连接当前邻接点的答案了,就搜索当前点不连这个邻接点而是下一个邻接点的情况和当前邻接点连接下一个边权最小的当前邻接点的邻接点的情况
最后输出答案
**/

[BFS,A*,k短路径] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 path (Problem - 6705)的更多相关文章

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

    $$2019中国大学生程序设计竞赛(CCPC)\ -\ 网络选拔赛$$ \(A.\hat{} \& \hat{}\) 签到,只把AB都有的位给异或掉 //#pragma comment(lin ...

  2. [贪心,dp] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 Fishing Master (Problem - 6709)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=6709 Fishing Master Time Limit: 2000/1000 MS (Java/Othe ...

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

    传送门 A.^&^ 题意: 找到最小的正数\(C\),满足\((A\ xor\ C)\&(B\ xor \ C)\)最小. 思路: 输出\(A\&B\)即可,特判答案为0的情况 ...

  4. 【赛后总结+部分题解】2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛

    赛后总结: T:今天状态一般,甚至有点疲惫.然后12点比赛开始,和队友开始看题,从最后往前面看,发现数学题公式看不懂.然后发现队友已经双开做1001和1006了,我看着1007有人A,开始做1007. ...

  5. 【2019中国大学生程序设计竞赛-女生专场】C - Function

    原题 韦神提供的思路orz 首先一个显然的性质,所有的c可以提出来,方程变成ax^2+bx的形式 因为x的值是离散的,而m的值又不大 所以一开始让x都为1(注意!x是正整数),然后每次挑一个x让他加一 ...

  6. 2019中国大学生程序设计竞赛-女生专场(重现赛)部分题解C-Function(贪心+优先队列) H-clock(模拟)

    Function 题目链接 Problem Description wls 有 n 个二次函数 Fi(x) = aix2 + bix + ci (1 ≤ i ≤ n). 现在他想在∑ni=1xi = ...

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

    A - ^&^ Bit operation is a common computing method in computer science ,Now we have two positive ...

  8. 2016中国大学生程序设计竞赛(长春) Ugly Problem 模拟+大数减法

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5920 我们的思路是: 对于一个串s,先根据s串前一半复制到后一半构成一个回文串, 如果这个回文串比s小, ...

  9. HDU 5963 朋友 【博弈论】 (2016年中国大学生程序设计竞赛(合肥))

    朋友 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Descr ...

随机推荐

  1. Promethues配置

    # my global config global: scrape_interval: 10s # Set the scrape interval to every 15 seconds. Defau ...

  2. Gnu pgp加密解密

    在生成密钥的时候,无法生成足够多的随机数,提示“ Not enough random bytes available. Please do some other work to givethe OS ...

  3. Class file version does not support constant tag 16 in class file

    启动服务时提示 Caused by: java.lang.ClassFormatError: Class file version does not support constant tag 16 i ...

  4. HTTP协议 有这篇文章足够了

    HTTP 协议详解 HTTP(HyperText Transfer Protocol)超文本传输协议.其最初的设计目的是为了提供一种发布和接收HTML页面的方法. HTTP是一个客户端(用户)和服务端 ...

  5. 什么是x86什么是x64 它们有什么区别

    1.内存寻址不同: 32位系统,最大支持3.5G内存,如果在32位系统中使用4G或更大的内存,电脑最多只可以识别3.4G左右可用,而64位系统最大可以支持128G大内存. 2.运算速度不同: 64位系 ...

  6. JDK 1.8 新特性之Date-Time API

    来源:请点击查看 1.8之前的日期类: 线程不安全:java.util.Date 这个类线程不安全,而且所有日期类都是可变的. 时间处理麻烦:默认的开始日期从1900年,不支持国际化,不提供时区支持, ...

  7. 30分钟学会Objective-C

    注: 本文首发于我的个人博客:https://evilpan.com/2019/04/05/objc-basics/ 请原谅我的标题党.但是如果你有其他语言的学习经验,要学习Objective-C的语 ...

  8. java异常和throw和throws的区别

    之前在编程中编译完成后,运行时,会遇见一些常见的错误,如NullPointerException,ArrayIndexOutOfBoundsException等等 在今天重新回顾学习了java异常,总 ...

  9. Ubuntu系统下环境安装遇到依赖冲突问题

    问题场景:在ubuntu系统下使用docker拉了一个python3.6的镜像,要在该容器中安装vim结果总是报已安装某些依赖的版本不满足要求 解决方法: 1.安装aptitude apt-get i ...

  10. echarts 图点击事件

    有三种方式,介绍一下,大家学习哈 1.利用tooltip记录信息,使用zr 监听事件,进行事件处理. 这种方法是利用showTip方法或者tooltip的formatter函数记录选中的数据信息,并在 ...