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. Spring_使用(JDBC)

    Spring_对JDBC的支持 使用JdbcTemplate更新数据库 导入jar包 创建applicationcontext.xml <?xml version="1.0" ...

  2. 安装vagrant&virtualBox

    https://blog.csdn.net/dream_188810/article/details/78218073 VirtualBox是一款开源免费的虚拟机软件(之前一直使用vm,vm功能较多, ...

  3. poj1087&&hdu1526 最大流

    多源多汇. 比较明显的建图.对于电器,可以从源点与各个电器相连,容量为1,表示这个电器有1个,然后对于各种接头,那可以各个接头与汇点相连,容量为1,表示每个接头只能用一次. 然后对于能够相互转换的接头 ...

  4. hdu2044 dp

    /* 每一种情况都可以由周围2个点得出 */ #include<stdio.h> int main() { __int64 dp[]; int i,t,l,r; dp[]=; dp[]=; ...

  5. [Linux]jenkins的安装 标签: linux服务器 2016-08-21 20:47 1060人阅读 评论(23)

    现阶段遇到一个问题,因为某台服务器需要腾出来,所以需要将这台服务器上jenkins的主节点重新安装到另外一台服务器上.,下面就介绍一下centos7上面jenkins的安装. 一,步骤 注意:新安装的 ...

  6. js错误处理Try-catch和throw

    1.try-catch语句   Try{ //可能会导致错误的代码 }catch(error){ //在错误发生时怎么处理 } 例如: try{ window.someNonexistentFunct ...

  7. 谈一谈Python的上下文管理器

    经常在Python代码中看到with语句,仔细分析下,会发现这个with语句功能好强,可以自动关闭资源.这个在Python中叫上下文管理器Context Manager.那我们要怎么用它,什么时候用它 ...

  8. js280行代码写2048

    2048 原作者就是用Js写的,一直想尝试.但久久未动手. 昨天教学生学习JS代码.最好还是就做个有趣的游戏好了.2048这么火,是一个不错的选择. 思路: 1. 数组 ,2维数组4x4 2. 移动算 ...

  9. 06多次查询某区间内topk问题

            题目描述:给定一个数组,需要多次查找不同区间内的,第k大或者第k小的元素.         考虑题目是多次查找,如果采用只对查询区间内的元素进行排序的思路,然后输出第k大的数的策略,那 ...

  10. MySQL数据库优化(五)——MySQL查询优化

    http://blog.csdn.net/daybreak1209/article/details/51638187 一.mysql查询类型(默认查询出所有数据列)1.内连接       默认多表关联 ...