Gym - 100625D Destination Unknown 最短路
题意:给你一张无向图,t个可能的目的地,问在这t个点中哪些点的最短路中经过了g和h
思路:这是傻逼题,我直接dijstra用vector< set<int> > 保存路径, 最后再去判断下。。好像这并不是出题者想要的解法,不过时间还可以,300+ms
#pragma comment(linker, "/STACK:1000000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define MAXN 100005
#define INF 0x3f3f3f3f
#define eps 1e-8
using namespace std;
struct Edge
{
int from, to, dist;
Edge(int from, int to, int dist):from(from), to(to), dist(dist){};
};
struct HeapNode
{
int d, u;
HeapNode(int d, int u):d(d), u(u){};
bool operator <(const HeapNode& rhs) const{
return d > rhs.d;
}
};
vector< set<int> > road[MAXN];
struct Dijstra
{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
bool done[MAXN];
int d[MAXN];
int p[MAXN]; void init(int n){
this->n = n;
for(int i = ; i <= n; i++){
G[i].clear();
road[i].clear();
}
edges.clear();
} void AddEdge(int from, int to, int dist){
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m - );
} void dijstra(int s){
priority_queue<HeapNode> Q;
for(int i = ; i <= n; i++){
d[i] = INF;
}
d[s] = ;
memset(done, , sizeof(done));
Q.push(HeapNode(, s));
while(!Q.empty()){
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = true;
for(int i = ; i < G[u].size(); i++){
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist){
d[e.to] = d[u] + e.dist;
road[e.to].clear();
if(road[u].empty()){
set<int> tmp;
tmp.clear();
tmp.insert(u);
road[e.to].push_back(tmp);
}
else{
for(int j = ; j < road[u].size(); j++){
road[e.to].push_back(road[u][j]);
road[e.to][j].insert(u);
}
}
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to], e.to));
}
else if(d[e.to] == d[u] + e.dist){
int w = road[e.to].size();
for(int j = ; j < road[u].size(); j++){
road[e.to].push_back(road[u][j]);
road[e.to][w + j].insert(u);
}
}
}
}
}
};
int n, m, t, g, h, s;
Dijstra p;
vector<int> res;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // OPEN_FILE
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d%d", &n, &m, &t);
scanf("%d%d%d", &s, &g, &h);
int x, y, z;
p.init(n);
for(int i = ; i <= m; i++){
scanf("%d%d%d", &x, &y, &z);
p.AddEdge(x, y, z);
p.AddEdge(y, x, z);
}
p.dijstra(s);
res.clear();
for(int i = ; i <= t; i++){
scanf("%d", &x);
for(int j = ; j < road[x].size(); j++){
road[x][j].insert(x);
if(road[x][j].find(g) != road[x][j].end() && road[x][j].find(h) != road[x][j].end()){
res.push_back(x);
break;
}
}
}
sort(res.begin(), res.end());
int w = res.size();
for(int i = ; i < w; i++){
printf("%d ", res[i]);
}
printf("\n");
}
}
Gym - 100625D Destination Unknown 最短路的更多相关文章
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Gym - 100338C Important Roads 最短路+tarjan
题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到 ...
- 暑假集训-WHUST 2015 Summer Contest #0.2
ID Origin Title 10 / 55 Problem A Gym 100625A Administrative Difficulties 4 / 6 Problem B Gym 1006 ...
- Hadoop集群模式安装出现的若干问题
一.域名解析问题 域名解析暂时失败问题 vim /etc/sysconfig/network 查看主机名 vim etc/hosts 配置IP地址与主机名 192.168.60.132 centos ...
- [转]自动驾驶平台Apollo 2.5环境搭建
原文地址:https://blog.csdn.net/jinzhuojun/article/details/80210180,转载主要方便随时查阅,如有版权要求,请及时联系. 我们知道,自动驾驶在学界 ...
- 【最短路】NEERC15 F Froggy Ford(2015-2016 ACM-ICPC)(Codeforces GYM 100851)
题目链接: http://codeforces.com/gym/100851 题目大意: 一只青蛙跳过宽为W的河,河中游N个石头,坐标xi,yi,现在往河中间添加一个石头,使得每次跳跃的最大的距离最小 ...
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- Gym 101873C - Joyride - [最短路变形][优先队列优化Dijkstra]
题目链接:http://codeforces.com/gym/101873/problem/C 题意: 这是七月的又一个阳光灿烂的日子,你决定和你的小女儿一起度过快乐的一天.因为她真的很喜欢隔壁镇上的 ...
- Gym - 100625J Jailbreak 最短路+搜索
http://codeforces.com/gym/100625/attachments/download/3213/2013-benelux-algorithm-programming-contes ...
随机推荐
- ubuntu安装和使用
1.查看ubuntu是32位还是64位 教程:jingyan.baidu.com/article/db55b609ab531f4ba30a2f13.html 2.安装maven 教程:www.linu ...
- 安装 glusterfs yum源报错
yum install glusterfs-server yum 一直报错 把/etc/yum.repos.d 备份 删除了所有文件,从测试机192..168.59.128上同步过来 一直报错 已加载 ...
- C++虚函数与纯虚函数用法与区别
1. 虚函数和纯虚函数可以定义在同一个类(class)中,含有纯虚函数的类被称为抽象类(abstract class),而只含有虚函数的类(class)不能被称为抽象类(abstract class) ...
- Java基础学习总结(2)——接口
一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如"金丝猴是一种动物",金丝猴从动物这个类继承,同时"金丝猴是一种值钱的东西",金丝猴 ...
- vps上运行serv-u的问题
为了给产品环境建个测试站,今天特意申请一个vps来做开发用,但运行了Serv-U的ServUDaemon.exe后始终提示: 响应: 530 User czhan cannot log in. 很无语 ...
- android删除表和清空表
删除某一表: //删除某一个表 public void dropTable(SQLiteDatabase db){ db.execSQL("drop from tab_name") ...
- hpuoj--校赛--考试来了(水题)
问题 C: 感恩节KK专场--考试来了 时间限制: 1 Sec 内存限制: 128 MB 提交: 475 解决: 112 [提交][状态][讨论版] 题目描述 很多课程马上就结课了,随之而来的就是 ...
- App server 与 Web server之间的区别
原文: http://www.javaworld.com/javaqa/2002-08/01-qa-0823-appvswebserver.html 简单来说,web服务器提供页面给浏览器,而app服 ...
- 关键字super
1.super,相较于关键字this,可以修饰属性.方法.构造器 2.super修饰属性.方法:在子类的方法.构造器中,通过super.属性或者super.方法的形式,显式的调用父类的指定 属性或方法 ...
- dom4j组装xml 以及解析xml
dom4j组装xml 以及解析xml: 1.下载dom4j的jar包,地址:https://dom4j.github.io/ 2.java代码: package test; import java.i ...