CSU 2005: Nearest Maintenance Point(Dijkstra + bitset)
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)的更多相关文章
- CSU 2005 Nearest Maintenance Point(最短路+bitset)
https://vjudge.net/problem/CSU-2005 题意:给出带权值的图,图上有一些特殊点,现在给出q个询问,对于每个询问,输出离该点最近的特殊点,如果有多个,则按升序输出. 思路 ...
- 最短路径:(Dijkstra & Floyd)
Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...
- 狄斯奎诺(dijkstra 模板)
/*狄斯奎诺算法(dijkstra)<邻接表> */ #include<stdio.h> #include<string.h> #include<stdlib ...
- 51nod-迷宫问题(Dijkstra算法)
关于Dijkstra算法的博文 http://www.cnblogs.com/skywang12345/p/3711512.html#anchor2 Dijkstra算法是一个经典的算法——他是荷兰计 ...
- 数据结构(C#):图的最短路径问题、(Dijkstra算法)
今天曾洋老师教了有关于图的最短路径问题,现在对例子进行一个自己的理解和整理: 题目: 要求:变成计算出给出结点V1到结点V8的最短路径 答: 首先呢,我会先通过图先把从V1到V8的各种路径全部计算下来 ...
- Poj1062 昂贵的聘礼 (dijkstra算法)
一.Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长 ...
- 【BZOJ 1003】[ZJOI2006]物流运输(Dijkstra+DP)
题链 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n ...
- 7-9 旅游规划 (25 分)(Dijkstra算法)
题意: 思路:单源最短路问题,Dijkstra算法搞定就可以了,因为要找出最便宜的最短路,所以需要在更新最短距离的时候加一个条件(即当最短距离相等的时候,如果该路径的花费更小,就更新最小花费)就可 ...
- SQL Server 2005数据库定期备份(非常详细)与 SQL Server 2005数据库备份定期清理
SQL Server 2005数据库定期备份 分类: SQL Server 20052011-01-06 16:25 3320人阅读 评论(1) 收藏 举报 sql server数据库sqlserv ...
随机推荐
- 【JZOJ4811】【NOIP2016提高A组五校联考1】排队
题目描述 输入 输出 样例输入 5 4 1 2 1 3 3 4 3 5 1 4 2 4 1 2 2 5 样例输出 3 1 1 2 数据范围 样例解释 解法 可推知原树可以转换为一个序列,即优先序列: ...
- Ubuntu16.04安装Caffe最全最详细教程(CPU)
转载请附上本文链接:https://www.cnblogs.com/acgoto/p/11570188.html 一.前言 为了安装caffe,本人已经在centos7.x上试错了1次,目前弃疗~:在 ...
- MacOS利用Terminal访问远程Linux服务器
常用命令 默认22端口访问 ssh x.x.x.x 指定端口访问 ssh x.x.x.x -p port 指定用户访问(默认是MacOS当前用户) ssh user@x.x.x.x [-p port] ...
- uva 10739【基础(区间)dp】
Uva 10739 题意:给定字符串,可以增加.删除.修改任意字符,问最少经过多少次操作使字符串回文. 题解:定义dp[l][r]表示把从l到r的子串Sl...Sr变成回文串需要操作的最少次数.字符可 ...
- Cmake在编译osgEarth时遇到的一个错误
CMake Error at src/osgEarthDrivers/CMakeLists.txt:7 (PROJECT): The CMAKE_C_COMPILER: llvm-gcc-4.2 is ...
- java中URLEncode和URLDecode
URLEncode和URLDecode用于完成普通字符串和 application/x-www-from-urlencoded MIME字符串之间的相互转化 如果传递的字符串中包含非西欧字符的字符串, ...
- KiCad EDA 5.1.4 发布了
KiCad EDA 5.1.4 发布了 KiCad EDA 自豪地宣布 KiCad 5 系列最新稳定版发布.5.1.4 稳定版修复了来自 5.1.2 和 5.1.3 版本的关键错误修复和其他一些小改进 ...
- mysql通过TEXT字段进行关联的优化方案
mysql如果通过超长的字段进行on关联,会导致效率很低,7k关联1.4k,结果为30+W的数据量,执行时间高达50秒. 将这个字段进行md5,然后再通过md5后的值进行关联,执行效率会大大优化,同样 ...
- java 简单实现FtpClient
1. 引入喜闻乐见的maven地址 <dependency> <groupId>commons-net</groupId> <artifactId>co ...
- linux centos 一键安装环境
phpStudy for Linux 支持Apache/Nginx/Tengine/Lighttpd, 支持php5.2/5.3/5.4/5.5切换 已经在centos-6.5,debian-7.4. ...