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 ...
随机推荐
- python学习笔记:第六天
一.元组(通用格式a=(1,),结束后面加个逗号,不同与数组是中括号,只能是只读的,不能修改,是有序的): 列表之间可以嵌套(列表之间嵌套,嵌套元组,是有序的):a[b[1,2],c[3,4]],输出 ...
- python基础6(函数 Ⅰ)
函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段 定义 def function_name(args...): function_body #例子 def print_somethin ...
- 紫书 例题 11-5 UVa 10048 (Floyd求最大权值最小的路径)
这道题是Floyd的变形 改成d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))就好了. #include<cstdio> #include< ...
- thymeleaf 拼接 超链接
<dd><a th:href="@{/get/{id}(id=${user.id})}">基本资料</a></dd>
- JAVA SSL
http://docs.oracle.com/javase/1.5.0/docs/guide/security/jsse/JSSERefGuide.html#InstallationAndCustom ...
- HDU4324 Triangle LOVE【拓扑排序】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- ,典型递归问题-F(1025)mod 5 的值
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...
- angularjs 工具方法
<!DOCTYPE HTML> <html ng-app> <head> <meta http-equiv="Content-Type" ...
- jni传递对象中包含arraylist对象。
相信在使用jni的过程中,总是要传递各种各样的类型,在这其中,我也碰到了一些问题. 简单的传一些内容,相信在网上一搜一大把. 所以我们就来说说.传递对象中包含arraylist吧. 在这里先给大家一个 ...
- Spring深入浅出(一)IOC的基本知识
Spring前言 Spring是一个企业级开发框架,为解决企业级项目开发过于复杂而创建的,框架的主要优势之一就是分层架构,允许开发者自主选择组件. Spring的两大核心机制是IOC(控制反转)和AO ...