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的更多相关文章

  1. HDU 6582 Path

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  2. HDU - 6582 Path (最短路+最小割)

    题意:给定一个n个点m条边的有向图,每条边有个长度,可以花费等同于其长度的代价将其破坏掉,求最小的花费使得从1到n的最短路变长. 解法:先用dijkstra求出以1为源点的最短路,并建立最短路图(只保 ...

  3. [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 ...

  4. 2019CCPC网络赛

    ^&^ (HDU 6702) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  5. 2019CCPC网络预选赛 八道签到题题解

    目录 2019中国大学生程序设计竞赛(CCPC) - 网络选拔赛 6702 & 6703 array 6704 K-th occurrence 6705 path 6706 huntian o ...

  6. hdu 1973 Prime Path

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabi ...

  7. hdu 1839 Delay Constrained Maximum Capacity Path 二分/最短路

    Delay Constrained Maximum Capacity Path Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu. ...

  8. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  9. HDU 5492(DP) Find a path

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5492 题目大意是有一个矩阵,从左上角走到右下角,每次能向右或者向下,把经过的数字记下来,找出一条路径是 ...

  10. [HDU 1973]--Prime Path(BFS,素数表)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Time Limit: 5000/1000 MS (Java/Others ...

随机推荐

  1. Java课堂疑问解答与思考4

    一. 请运行以下示例代码StringPool.java,查看其输出结果.如何解释这样的输出结果?从中你能总结出什么? 答:定义的三个字符串如果相等,系统自动创建一个,并调用这个,对于由new创建的字符 ...

  2. python 使用 with open() as 读写文件

    读文件: 要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符: >>> f = open('E:\python\python\test.tx ...

  3. docker--搭建docker swarm集群

    10 搭建docker swarm集群 10.1 swarm 介绍 Swarm这个项目名称特别贴切.在Wiki的解释中,Swarm behavior是指动物的群集行 为.比如我们常见的蜂群,鱼群,秋天 ...

  4. [Git] 014 远程仓库篇 第一话

    0. 前言 在 [Git] 001 初识 Git 与 GitHub 之新建仓库 中,我在 GitHub 上建了一个仓库 "interesting" 这回的任务 把远程的 " ...

  5. PostgreSQL dblink使用过程

    安装: 进入/root/postgresql-11.2/contrib/dblink make && make install 切换到postgres用户 [root@fce40690 ...

  6. nodejs遍历文件夹下并操作HTML/CSS/JS/PNG/JPG

    需求描述,由于工作的需要,需要将原本用于1280 720的网页改为1920 1080的网页(电视端页面).需求可以拆分为两部分,代码部分的修改以及图片的修改.在代码部分,需要将所有位置以及大小相关的值 ...

  7. C# 各种加密

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Sec ...

  8. pandas读取Excel文件

    In [7]: import pandas as pd filname = 'ch02数据导入\\student.xlsx' data = pd.read_excel(filname) data Ou ...

  9. POJ 3667 Hotel (线段树区间合并)

    题目链接:http://poj.org/problem?id=3667 题目大意:一共有n个房间,初始时都是空的,现在有m个操作,操作有以下两种: 1.1 d :询问是否有连续d个空的房间,若有则输出 ...

  10. AtCoder Beginner Contest 089 D - Practical Skill Test

    Problem Statement We have a grid with H rows and W columns. The square at the i-th row and the j-th ...