Description

A county consists of n cities (labeled 1, 2, …, n) connected by some bidirectional roads. Each road connects a pair of distinct cities. A robot company has built maintenance points in some cities. Now, they need your help to write a program to query the nearest maintenance point to a specific city.

Input

There will be at most 200 test cases. Each case begins with four integers n, m, s, q(2 ≤ n ≤ 104, 1 ≤ m ≤ 5 × 104, 1 ≤ s ≤ min{n, 1000},1 ≤ q ≤ min{n, 1000}), the number of cities, the number of roads, the number of cities which have maintenance points and the number of queries. Then m lines contain the descriptions of roads. Each of them contains three integers ui, vi, wi(1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ wi ≤ 1000), denoting there is a road of length wi which connects city ui and vi. The next line contains s integers, denoting the cities which have maintenance points. Then q lines contain the descriptions of queries. Each of them contains one integer denoting a specific city. The size of the whole input file does not exceed 6MB.

Output

For each query, print the nearest city which has a maintenance point. If there are multiple such cities, print all of them in increasing order separated by a single space. It is guaranteed that there will be at least one such city.

Sample Input

4 4 2 3
1 2 1
2 3 2
2 4 1
4 3 2
1 3
3
2
4

Sample Output

3
1
1 3 题目大意:有n个城市,m条边将一些城市连接起来,现在有s个城市维护点,q次查询,现在对于每一次查询的城市,输出离他最近的城市维护点,如果有多个则按递增顺序打印。
思路: 把s个城市维护点当起点搜一下最短路,其中用bitset来维护其他城市距离维护点最近的那个起点,对于城市x来说,若有多个起点城市到它的最短距离是相等的就用或运算将之存在bitset,否则直接将最短的那一个维护点赋值给x
  代码上也做了一些注释(如果对于bitset不是很熟悉的可以向大家推荐一篇感觉还不错的博客。http://www.cppblog.com/ylfeng/archive/2010/03/26/110592.html)
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<bitset>
#include<vector>
#include<queue> using namespace std;
const int INF = << ;
const int maxn = ;
struct node {
int to, cost;
node() {}
node(int a, int b) :to(a), cost(b) {}
bool operator<(const node&a)const {
return cost > a.cost;
}
};
vector<node>e[maxn];
bitset<>bt[maxn];
int n, m, s, q;
int dis[maxn], cha[maxn], vis[maxn];
void Dijkstra()
{
priority_queue<node>Q;
for (int i = ; i <= n; i++) {
dis[i] = INF; bt[i].reset();//初始化距离和清空bitset表
vis[i] = ;
}
for (int i = ; i <= s; i++) {
dis[cha[i]] = ;
Q.push(node(cha[i], dis[cha[i]]));
bt[cha[i]][i] = ;//bt[i][j] = 1 代表第i个城市最近的城市维护点是第j个
}
while (!Q.empty()) {
node t = Q.top(); Q.pop();
if (vis[t.to])continue;
vis[t.to] = ;//对于每一个点我们只需要以他为起点遍历一次就ok,避免大量的重复计算
for (int i = ; i < e[t.to].size(); i++) {
int tmp = e[t.to][i].to;
int ct = e[t.to][i].cost;
if (dis[tmp] > dis[t.to] + ct) {
dis[tmp] = dis[t.to] + ct;
Q.push(node(tmp, dis[tmp]));
bt[tmp] = bt[t.to];//如果有比当前更近的城市维护点则将更近的维护点赋值给现在的点
}
else if (dis[tmp] == (dis[t.to] + ct))
bt[tmp] |= bt[t.to];//如果有多个城市维护点的距离相同就取或赋值
}
}
}
int main()
{
while (scanf("%d%d%d%d", &n, &m, &s, &q) != EOF) {
for (int i = ; i <= n; i++)e[i].clear();
for (int a, b, c, i = ; i < m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[a].push_back(node(b, c));
e[b].push_back(node(a, c));
}
for (int i = ; i <= s; i++)scanf("%d", &cha[i]);
sort(cha + , cha + s + );//因为题目要我们如果有多个点满足要求,则递增输出,所以我们可以事先排个序
Dijkstra();
int x;
while (q--) {
int a[], cnt = ;
scanf("%d", &x);
for (int i = ; i <= s; i++)
if (bt[x][i])//对于s个城市维护点来说,若bt[x][i]==1则代表第i个城市到x的距离是最近的
a[++cnt] = cha[i];
for (int i = ; i < cnt; i++)
printf("%d ", a[i]);//最后按格式输出即可
printf("%d\n", a[cnt]);
}
}
return ;
}

CSU 2005: Nearest Maintenance Point(Dijkstra + bitset)的更多相关文章

  1. CSU 2005 Nearest Maintenance Point(最短路+bitset)

    https://vjudge.net/problem/CSU-2005 题意:给出带权值的图,图上有一些特殊点,现在给出q个询问,对于每个询问,输出离该点最近的特殊点,如果有多个,则按升序输出. 思路 ...

  2. 最短路径:(Dijkstra & Floyd)

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  3. 狄斯奎诺(dijkstra 模板)

    /*狄斯奎诺算法(dijkstra)<邻接表> */ #include<stdio.h> #include<string.h> #include<stdlib ...

  4. 51nod-迷宫问题(Dijkstra算法)

    关于Dijkstra算法的博文 http://www.cnblogs.com/skywang12345/p/3711512.html#anchor2 Dijkstra算法是一个经典的算法——他是荷兰计 ...

  5. 数据结构(C#):图的最短路径问题、(Dijkstra算法)

    今天曾洋老师教了有关于图的最短路径问题,现在对例子进行一个自己的理解和整理: 题目: 要求:变成计算出给出结点V1到结点V8的最短路径 答: 首先呢,我会先通过图先把从V1到V8的各种路径全部计算下来 ...

  6. Poj1062 昂贵的聘礼 (dijkstra算法)

    一.Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长 ...

  7. 【BZOJ 1003】[ZJOI2006]物流运输(Dijkstra+DP)

    题链 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n ...

  8. 7-9 旅游规划 (25 分)(Dijkstra算法)

    题意: ​ 思路:单源最短路问题,Dijkstra算法搞定就可以了,因为要找出最便宜的最短路,所以需要在更新最短距离的时候加一个条件(即当最短距离相等的时候,如果该路径的花费更小,就更新最小花费)就可 ...

  9. SQL Server 2005数据库定期备份(非常详细)与 SQL Server 2005数据库备份定期清理

     SQL Server 2005数据库定期备份 分类: SQL Server 20052011-01-06 16:25 3320人阅读 评论(1) 收藏 举报 sql server数据库sqlserv ...

随机推荐

  1. Spark day06

    SparkStreaming简介 SparkStreaming是流式处理框架,是Spark API的扩展,支持可扩展.高吞吐量.容错的实时数据流处理,实时数据的来源可以是:Kafka, Flume, ...

  2. nginx与apache

    参考链接:https://www.cnblogs.com/changning0822/p/7844004.html

  3. JDK源码解析string之二

    (28) public boolean startsWith(String prefix, int toffset) { char ta[] = value; int to = toffset; ch ...

  4. 小爬爬5:scrapy介绍3持久化存储

    一.两种持久化存储的方式 1.基于终端指令的吃持久化存储: 特点:终端指令的持久化存储,只可以将parse方法的返回值存储到磁盘文件 因此我们需要将上一篇文章中的author和content作为返回值 ...

  5. execute和submit的区别与联系

    execute和submit都属于线程池的方法,execute只能提交Runnable类型的任务,而submit既能提交Runnable类型任务也能提交Callable类型任务. execute会直接 ...

  6. C++复制初始化的限制

    相比于直接初始化,复制初始化有更加严格的限制. 1:在复制初始化时,不能使用声明为explicit的构造函数进行的隐式转换.而直接初始化则是允许的: struct Exp { explicit Exp ...

  7. 分享 KubeCon 2019 (上海)关于 Serverless 及 Knative 相关演讲会议

    有幸参加了 KubeCon 2019 上海大会,并参加了 Knative 及 Serverless 相关的几场分享会,收获满满.这里简单介绍一下各个演讲主题的主要内容.详细的演讲主题文档可以在Kube ...

  8. LeetCode99 Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. JavaScriptBreak 语句 continue 语句

    break 语句用于跳出循环. continue 用于跳过循环中的一个迭代. Break 语句 我们已经在本教程之前的章节中见到过 break 语句.它用于跳出 switch() 语句. break ...

  10. 【批量添加】-拼接sql字符串 标签: 批量添加 2015-12-13 17:49 2070人阅读 评论(33)

    现在做的一个项目需要用到批量添加,但是封装的底层没有这个方法,所以自食其力,自己来写.我们用的是拼接sql字符串的方法来实现功能. 具体实现流程:首先将需要的数据存储到实体的list中,然后将这个li ...