HDU 5521:Meeting(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5521
Meeting
fences they were separated into different blocks. John's farm are divided into n blocks labelled from 1 to n.
Bessie lives in the first block while Elsie lives in the n-th one. They have a map of the farm
which shows that it takes they ti minutes to travel from a block in Ei to another block
in Ei where Ei (1≤i≤m) is a set of blocks. They want to know how soon they can meet each other
and which block should be chosen to have the meeting.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
In the first case, it will take Bessie 1 minute travelling to the 3rd block, and it will take Elsie 3 minutes travelling to the 3rd block. It will take Bessie 3 minutes travelling to the 4th block, and it will take Elsie 3 minutes travelling to the 4th block. In the second case, it is impossible for them to meet.
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
#define N 1000005
#define M 2000005
const LL inf = 1e17;
/*
最短路
*/
struct node
{
int v, nxt;
LL w;
}edge[M]; struct nd
{
int p;
LL d;
nd(){}
nd(LL _d, int _to):d(_d), p(_to) {}
bool operator < (const nd &a) const {
return d > a.d;
}
}; int tot, n, m, nn, used[N], head[N];
LL da[N], db[N]; void add(int u, int v, LL w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
} void Dijkstra(int s, LL d[])
{
priority_queue<nd> que;
while(!que.empty()) que.pop();
for(int i = ; i <= nn; i++) d[i] = inf;
memset(used, , sizeof(used));
d[s] = ;
que.push(nd(d[s], s));
while(!que.empty()) {
nd top = que.top(); que.pop();
int u = top.p;
if(used[u]) continue;
used[u] = ;
for(int k = head[u]; ~k; k = edge[k].nxt) {
int v = edge[k].v;
int w = edge[k].w;
if(d[u]+w < d[v]) {
d[v] = d[u] + w;
que.push(nd(d[v], v));
}
}
}
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
tot = ;
nn = n;
for(int i = ; i < m; i++) {
LL dis;
int num, a;
//就是这里了,在外面建一个点,然后集合里面所有点都和它相连
nn++;
scanf("%I64d%d", &dis, &num);
for(int j = ; j < num; j++) {
scanf("%d", &a);
add(a, nn, dis);
add(nn, a, dis);
}
} Dijkstra(, da);
Dijkstra(n, db);
LL ans = inf; for(int i = ; i <= n; i++)
ans = min(ans, max(da[i], db[i])); printf("Case #%d: ", cas);
if(ans==inf) printf("Evil John\n");
else{
//因为求出的距离是两倍,所以要除以二
printf("%I64d\n", ans/);
bool f = ;
for(int i = ; i <= n; i++) {
if(max(da[i], db[i]) == ans){
if(f) printf(" ");
f = ;
printf("%d", i);
}
}
puts("");
}
}
return ;
}
HDU 5521:Meeting(最短路)的更多相关文章
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5521 Meeting(虚拟节点+最短路)
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- hdu 5521 Meeting(最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...
- HDU 5521 Meeting【最短路】
今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...
- HDU 5521 Meeting (最短路,dijstra)
题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n,有M个点集,集合内的数表示任意两点的距离为dis ,现在问,如果两个人要见面, 需要最短距离是多少,有哪几个点能被当成见面点. 析:分别对 ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- HDU 5521 Meeting
2015 ACM / ICPC 沈阳站现场赛 M题 最短路 设置N+M个节点,前N个节点是Block,后M个节点是Set,每一组Set中的点向该Set连边,从1和n开始分别求最短路.注意爆int. # ...
- HDU - 5521 Meeting (Dijkstra)
思路: 看了好久才看懂题意,文中给了n个点,有m个集合,每个集合有s个点,集合内的每两个点之间有一个权值为t的边,现在有两个人,要从1号点,和n号点,走到同一个顶点,问最少花费以及花费最少的点. 那就 ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- 在Docker中创建Mongo容器的后续设置
后续设置包括设置数据库管理员账号密码.创建业务数据库以及设置账户密码 需要注意的是,在创建Mongo容器后,需要映射到本机 以管理员身份打开powershell 先切换到mongdo bash # ` ...
- WPF 使用Propereties:Resources.resx里面的资源
<Window x:Class="Wpf180706.Window7" xmlns="http://schemas.microsoft.com/win ...
- WPF 寻找控件模板中的元素
<Window x:Class="Wpf180706.Window10" xmlns="http://schemas.microsoft.com/wi ...
- QT Linux Demo程序编译
我手上的qt源码包为:qt-everywhere-opensource-src-4.7.0.tar.gz 在Linux下编译比较容易,解压后直接 ./configure,一般会报缺少什么库这些.自己遇 ...
- WPF DataGrid自动生成列
<Window x:Class="DataGridExam.MainWindow" xmlns="http://schemas.microsoft.c ...
- 所有语言的Awesome(2)
Curated list of awesome lists https://awesomeweekly.co https://github.com/sindresorhus/awesome ✨ Pre ...
- NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密
本文列举了 数据加密算法(Data Encryption Algorithm,DEA) 密码学中的高级加密标准(Advanced EncryptionStandard,AES)RSA公钥加密算法 ...
- Win10《芒果TV》更新v3.6.0秋收版:新增追剧磁贴、记忆续播、跳转列表
热血青春,唱响革命战歌,<秋收起义>正在芒果TV热播,Win10版<芒果TV>更新v3.6.0秋收版,新增追剧磁贴.记忆续播.跳转列表. Win10版<芒果TV>V ...
- Android零基础入门第61节:滚动视图ScrollView
原文:Android零基础入门第61节:滚动视图ScrollView 前面几期学习了ProgressBar系列组件.ViewAnimator系列组件.Picker系列组件和时间日期系列组件,接下来几期 ...
- 配置QSslConfiguration让客户端程序跳过本地SSL验证
大家下午好哦.今天我们在重新制作我们萌梦聊天室的时候,出现了这样的问题.那就是我们的客户端能够对qtdream.com服务器进行登录,但是不能对localhost服务器(也就是本机啦)进行登录.这究竟 ...