HDU-6705 Path
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∗10^4\))
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∗10^4\))
It's guaranteed that \(Σn ,Σm, Σq,Σmax(k)≤2.5∗10^5\) 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
题解
给定一张有向图,q次询问,每次询问第k小的路径长度。
离线,预处理出最大的k范围内的所有路径长度。先将所有边按边权排序,用一个set存储当前可以成为答案的边,且set的最大的大小为maxk,每次从set中取出w最小的边,看看能否更新set中的元素,不能更新则break(边权从小到大排序,小边权无法更新之后边权也无法更新),对set中的元素都做一次这样的处理后,我们就得到了[1,maxk]的答案,输出询问即可,复杂度\(O(k*log(m+k))\)
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e4 + 50;
struct node {
int v; ll w;
node (int v = 0, int w = 0): v(v), w(w) {}
bool operator < (const node &b) const {
return w < b.w;
}
};
vector<node> G[N];
struct Edge {
int u, v; ll w;
int id;
Edge(int u = 0, int v = 0, ll w = 0, int id = 0): u(u), v(v), w(w), id(id) {}
bool operator < (const Edge &b) const {
if (w == b.w)
if (u == b.u)
if (v == b.v)
return id < b.id;
else return v < b.v;
else return u < b.u;
else return w < b.w;
}
bool operator == (const Edge &b) const {
return w == b.w && u == b.u && v == b.v && id == b.id;
}
};
int Q[N];
ll ans[N];
int main() {
int t; scanf("%d", &t);
while (t--) {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i++) G[i].clear();
set<Edge> st; st.clear();
int cnt = 0;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
G[u].push_back(node(v, w));
st.insert(Edge(u, v, w, ++cnt));
}
for (int i = 1; i <= n; i++) sort(G[i].begin(), G[i].end());
int maxk = 0;
for (int i = 1; i <= q; i++) {
scanf("%d", &Q[i]);
maxk = max(maxk, Q[i]);
}
while (st.size() > maxk) st.erase(st.end());
for (int i = 1; i <= maxk; i++) {
Edge now = *st.begin();
st.erase(st.begin());
ans[i] = now.w;
if (i == maxk) break;
int u = now.v;
for (int j = 0; j < G[u].size(); j++) {
int v = G[u][j].v;
ll w = G[u][j].w;
if (i + st.size() < maxk) st.insert(Edge(now.u, v, now.w + w, ++cnt));
else {
set<Edge>::iterator it = st.end(); it--;
Edge last = *it;
if (now.w + w < last.w) {
st.erase(it);
st.insert(Edge(u, v, now.w + w, ++cnt));
}
else break;
}
}
}
for (int i = 1; i <= q; i++) printf("%lld\n", ans[Q[i]]);
}
return 0;
}
HDU-6705 Path的更多相关文章
- HDU 6582 Path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...
- HDU - 6582 Path (最短路+最小割)
题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...
- [BFS,A*,k短路径] 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 path (Problem - 6705)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=6705 path Time Limit: 2000/2000 MS (Java/Others) Mem ...
- 2019CCPC网络赛
^&^ (HDU 6702) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 2019CCPC网络预选赛 八道签到题题解
目录 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 6702 & 6703 array 6704 K-th occurrence 6705 path 6706 huntian o ...
- hdu 1973 Prime Path
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...
- hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路
Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- HDU 5492(DP) Find a path
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...
- [HDU 1973]--Prime Path(BFS,素数表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...
随机推荐
- jmeter常用性能监听器分析
jmeter中提供了很多性能数据的监听器,我们通过监听器可以来分析性能瓶颈 本文以500线程的阶梯加压测试结果来描述图表. 常用监听器 1:Transactions per Second 监听动态TP ...
- win10安装Tensorflow1.9GPU版本
前言 看到DateWhale出了一篇安装教程(微信公众号DateWhale),决定体验一下Tensorflow1.9的GPU版本..其实一开始装的是2.0,但是tf.Session()就报错了,说是2 ...
- 14.使用Crunch创建字典----Armitage扫描和利用----设置虚拟渗透测试实验室----proxychains最大匿名
使用Crunch创建字典 kali自带的字典 usr/share/wordlists cd Desktop mkdir wordlists cd wordlists/ crunch --help cr ...
- [每天一课] 今天就讲一讲关于vue-cli 脚手架里 如何调用API
既然vue-resource停更了,就不讲了,但是效果也是差不多了 今天主要讲一下关于axios的方式来调用API,按照vue-cli的模式,这个当然得先引入一个axios这个模块了.那当然得走一遍终 ...
- Marked Ancestor
一道并查集的题目硬是被我当成线段树写了,感觉这样写虽然不是最好的,不过能a就行 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103906 ...
- [Git] 025 标签命令
0. 前言 小时候看<剑花-烟雨-江南>,惊讶于那个多重身份的"小侯爷" 后来发现,现实中拥有比小侯爷更多身份的人多如牛毛 其实,在 "Git" 中 ...
- 关于java范型
1 范型只在编译阶段有效 编译器在编译阶段检查范型结果之后,就会将范型信息删除.范型信息不会进入运行时阶段. 泛型类型在逻辑上看以看成是多个不同的类型,实际上都是相同的基本类型. 2 不能对确定的范型 ...
- [转帖]NetSuite 进入中国市场满一年,甲骨文公布首份成绩单
NetSuite 进入中国市场满一年,甲骨文公布首份成绩单 https://baijiahao.baidu.com/s?id=1617073148682281883&wfr=spider&am ...
- 打印 PRINT
打印 PRINT 字符串和数值类型 可以直接输出. print(1) #out:1 print('a') #out:a 变量 无论什么类型,数值,字符串,列表,字典...都可以直接输出 n = 1 s ...
- Mybatis-学习笔记(N)mybatis-generator 生成DAO、Mapper、entity
1.mybatis-generator 生成DAO.Mapper.entity 所需环境:jdk 所需jar包:mybatis-generator-core-1.3.5.jar.MySQL-conne ...