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. 第0001题 : 产生随机数(顺便读random模块官方文档)

    看这个之前我准备先看一下random模块的官方文档... 在整个随机模块中,  最基础的就是random, 它产生一个 [0.0, 1.0)的浮点数. 这个模块下所有的函数实际上是绑定在一个叫做ran ...

  2. C# WebClient的使用

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  3. WPF 创建无边框的圆角窗口

    原文:WPF 创建无边框的圆角窗口 如题所述,在WPF中要创建一个没有边框且为圆角的窗体,有如下几步工作要进行: 第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为Tru ...

  4. Expression Blend实例动画-大坝水位升高演示

    原文:Expression Blend实例动画-大坝水位升高演示 Expression Blend是个很强大的东西,用它可以完成很多不好做的动画效果. 动画的主要元素: 时间线(Timeline) : ...

  5. Qt之使用setWindowFlags方法遇到的问题(追踪进入QWidget的源码分析原因,最后用WINAPI解决问题)good

    一.简述 前段时间在使用setWindowFlags方法时遇到了一个坑,具体情况是想通过窗口界面上一个checkBox来控制窗口当前状态是否置顶,而Qt提供了Qt::WindowStaysOnTopH ...

  6. Win8Metro(C#)数字图像处理--2.26图像减法

    原文:Win8Metro(C#)数字图像处理--2.26图像减法  [函数名称] 图像减法函数SubtractionProcess(WriteableBitmap src, WriteableBi ...

  7. Windows 10开发基础——指针事件和操作事件(一)

    主要内容: 1.指针事件 2.操作事件 1.指针事件 指针事件由各种活动输入源引发,包括触摸.触摸板.笔和鼠标(它们替代传统的鼠标事件).指针事件基于单一输入点(手指.笔尖.鼠标光标),但不支持基于速 ...

  8. DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS & PUBLISHING THEM TO THE MICROSOFT STORE

    原文: DEPLOYING NATIVE UWP (UNIVERSAL WINDOWS PLATFORM) APPS FOR JAVA DEVELOPERS & PUBLISHING THEM ...

  9. JavaScript 数据实用程序库:Datalib

    Datalib 是一个 JavaScript 数据实用程序库. 快速使用Romanysoft LAB的技术实现 HTML 开发Mac OS App,并销售到苹果应用商店中.   <HTML开发M ...

  10. 关于案例中核心dao的解释

    很多小伙伴不太理解核心dao,说这是干什么的,接下来我将一一为大家解答: 1.说到核心dao不得不说到单表操作,单表操作顾名思义是对单张数据库表的CRUD操作,实际情况中我们追求将表与表的关系映射到对 ...