题目: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. ES常见问题整理

    1.集群状态red.yellow处理方法 1.red表示主分片数据不完整,通常时由于某个索引的主分片为分片unassigned,找出这个分片未分配的原因,解决即可: curl -XGET http:/ ...

  2. java.lang.SecurityException: class "javax.servlet.AsyncContext"'s signer information does not match signer information of other classes in the same package

    最近在写个Http协议的压测挡板时,遇到以下错误. 2018-03-08 10:34:07.808:INFO:oejs.Server:jetty-8.1.9.v20130131 2018-03-08 ...

  3. C#开发BIMFACE系列30 服务端API之模型对比1:发起模型对比

    系列目录     [已更新最新开发文章,点击查看详细] 在实际项目中,由于需求变更经常需要对模型文件进行修改.为了便于用户了解模型在修改前后发生的变化,BIMFACE提供了模型在线对比功能,可以利用在 ...

  4. unittest实战(三):用例编写

    # coding:utf-8import unittestfrom selenium import webdriverimport timefrom ddt import ddt, data, unp ...

  5. fsLayuiPlugin数据字典使用

    概述 数据字典主要解决下拉框数据填充和数据表格转义处理,一个数据字典可以多处使用. 1.多个页面下拉框使用同样的数据,改一个地方需要把所有页面都要修改 2.数据表格转义代替自己手动写templet解析 ...

  6. pycharm专业版激活破解(亲测有效)

    完成破解步骤,亲测有效! 1.打开路径,修改hosts文件:C:\Windows\System32\drivers\etc 找到hosts文件打开 最后一行添加这行代码:   0.0.0.0 acco ...

  7. 解决IOS下window.open页面打不开问题

    问题如标题所写,在ajax回调里面拿到即将要跳转的链接url,使用window.open(linkUrl),没有起作用,而且代码也没有报错,查找原因是:大部分现代的浏览器(Chome/Firefox/ ...

  8. javascript中this指向的问题

    javascript中this只有函数执行时候才能确定到底指向谁,实际this最终指向是那个调用它的对象. 1,匿名函数中的this——window function foo(){ var lastN ...

  9. 轻量级MVC框架(自行开发)

    源码及demo: https://github.com/killallspree/myFrame/

  10. 【Android】四大组件归纳总结

    随着学习持续更新 四大组件均可使用android:process="name"在Manifest中声明成独立进程 Activity 生命周期 4种启动模式 Android使用回退栈 ...