题目: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. 方兴未艾的云计算:SoCC 2015大会

    ACM 云计算研讨会(ACM Symposium on Cloud Computing, 以下简称SoCC)是由SIGMOD(Special Interest Group on Management ...

  2. 制作UEFI(64位)下的WinPE + Ubuntu + Acronis多启动U盘

    最近研究了一下如何制作一个多启动U盘,其中想包含的功能是WinPE(这里选择WEPE),Ubuntu 18.04,Acronis True Image 2018的ISO恢复盘.这里分享一下制作的经验和 ...

  3. React类型检查

    类型检查 import PropTypes from 'prop-types' 类名==List List.propTypes = { list: PropTypes.array } // 默认值 L ...

  4. LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  5. jsp内置对象(三)-----response对象

    response对象  response对象包含了响应客户端请求的有关信息,但在JSP中很少直接用到它.他是HttpServletResponse类的实例,response对象具有页面作用域,即访问一 ...

  6. Windows下安装虚拟机

    一.准备工作 1.下载centos7操作系统 阿里巴巴站点: http://mirrors.aliyun.com/centos/7/isos/x86_64/ 2.下载VMware虚假机 可以直接通过3 ...

  7. springmvc 的@ResponseBody 如何使用JSONP?

    JSONP解释 在解释JSONP之前,我们需要了解下”同源策略“这个概念,这对理解跨域有帮助.基于安全的原因,浏览器是存在同源策略机制的,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载额文 ...

  8. SQL之开窗函数详解--可代替聚合函数使用

    在没学习开窗函数之前,我们都知道,用了分组之后,查询字段就只能是分组字段和聚合的字段,这带来了极大的不方便,有时我们查询时需要分组,又需要查询不分组的字段,每次都要又到子查询,这样显得sql语句复杂难 ...

  9. javaee作业

    一.单选题(共5题,50.0分) 1 在SqlSession对象的openSession()方法中,不能作为参数executorType的可选值 的是(     ).   A. ExecutorTyp ...

  10. MySQL 统计行数的 count

    MySQL count() 函数我们并不陌生,用来统计每张表的函数.但如果你的表越来越大,并且是 InnoDB 引擎的话,会发现计算的速度会越来越慢.在这篇文章里,会先介绍 count() 实现的原理 ...