http://acm.hdu.edu.cn/showproblem.php?pid=5521

Meeting

Problem Description
 
Bessie and her friend Elsie decide to have a meeting. However, after Farmer John decorated his
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.
 
 
Input
 
The first line contains an integer T (1≤T≤6), the number of test cases. Then T test cases
follow.
The first line of input contains n and m. 2≤n≤105. The following m lines describe the sets Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109) and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that ∑mi=1Si≤106.
 
 
Output
 
For each test case, if they cannot have the meeting, then output "Evil John" (without quotes) in one line.
Otherwise, output two lines. The first line contains an integer, the time it takes for they to meet.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
 
 
Sample Input
 
2
5 4
1 3 1 2 3
2 2 3 4
10 2 1 5
3 3 3 4 5
3 1
1 2 1 2
 
 
Sample Output
 
Case #1: 3
3 4
Case #2: Evil John
 
Hint
 

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.

 
题意:给出n个点还有m个集合,每个集合给出一个dis和s个点,这个集合里面的点去其他点的花费是dis,然后求从1到n和从n到1同时走到相同的点的最短距离。
思路:由于给出的点的关系是集合,而且数据范围不允许我们两两建边,看了一下别人的结题报告,发现一个比较巧妙的方法:我们可以在这些点之外再弄出m个点来,每个集合的点和n之外的一个点存在边,这样的话每个集合的建边就是2*s而已,这样的建边让距离乘以2了,我们可以先保留这个距离,到输出答案时候再除以2.
 
 #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(最短路)的更多相关文章

  1. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  2. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  3. hdu 5521 Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...

  4. HDU 5521 Meeting【最短路】

    今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...

  5. HDU 5521 Meeting (最短路,dijstra)

    题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n,有M个点集,集合内的数表示任意两点的距离为dis ,现在问,如果两个人要见面, 需要最短距离是多少,有哪几个点能被当成见面点. 析:分别对 ...

  6. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  7. HDU 5521 Meeting

    2015 ACM / ICPC 沈阳站现场赛 M题 最短路 设置N+M个节点,前N个节点是Block,后M个节点是Set,每一组Set中的点向该Set连边,从1和n开始分别求最短路.注意爆int. # ...

  8. HDU - 5521 Meeting (Dijkstra)

    思路: 看了好久才看懂题意,文中给了n个点,有m个集合,每个集合有s个点,集合内的每两个点之间有一个权值为t的边,现在有两个人,要从1号点,和n号点,走到同一个顶点,问最少花费以及花费最少的点. 那就 ...

  9. hdu 5521 最短路

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. WPF 窗体显示最前端

    原文:WPF 窗体显示最前端 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/jjx0224/article/details/8782845 如何做一 ...

  2. Symbol not found: _lua_objlen

    lua: error loading module 'cjson' from file '/usr/local/lib/lua/5.3/cjson.so': dlopen(/usr/local/lib ...

  3. 基于IdentityServer4的单点登录——Client

    以MvcClient项目为例 1.新建项目并添加引用 新建一个asp .net core 2.0的项目引用IdentityModel 2.配置 比之前的控制台客户端多这个步骤,需要配置这个客户端的Cl ...

  4. c# 全局钩子实现扫码枪获取信息。

    原文:c# 全局钩子实现扫码枪获取信息. 1.扫描枪获取数据原理基本相当于键盘数据,获取扫描枪扫描出来的数据,一般分为两种实现方式. a)文本框输入获取焦点,扫描后自动显示在文本框内. b)使用键盘钩 ...

  5. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.

    Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when ...

  6. centos7 防火墙问题

    centos从7开始默认用的是firewalld,这个是基于iptables的,虽然有iptables的核心,但是iptables的服务是没安装的.所以你只要停止firewalld服务即可:sudo ...

  7. Django 创建超级管理员失败

    django.db.utils.DataError: (1406, "Data too long for column 'gender' at row 1") 解决方案 在执行mi ...

  8. 深入理解Amazon Alexa Skill(二)

    理解skill调用 本节来更详细的讨论alexa是如何确定调用哪个skill的. 参考:https://developer.amazon.com/zh/docs/custom-skills/under ...

  9. 图像滤镜艺术---Glow Filter发光滤镜

    原文:图像滤镜艺术---Glow Filter发光滤镜 Glow Filter发光滤镜 Glow Filter发光滤镜是一种让图像产生发光效果的滤镜,它的实现算法如下: 1,对原图P进行高斯模糊得到图 ...

  10. 零元学Expression Blend 4 - Chapter 39 虾米?!同款?不同师傅!告诉你Visible、Hidden与Collapsed的差异!

    原文:零元学Expression Blend 4 - Chapter 39 虾米?!同款?不同师傅!告诉你Visible.Hidden与Collapsed的差异! 由此可知 Hidden为隐藏项目,但 ...