传送门

题目

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 casesfollow.
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 multipleoptional blocks, output all of them in ascending order.

题目大意

现在给出一个图,其中分成点分成一个集合一个集合的,每一集合中的点互相之间的距离都是相同的,也给出来了;现在要求两个人分别从1和n出发,问最短多长时间才能遇到,且给出这些可能的相遇点;

分析

这个题主要在构图,思路比较好想。将所有点和它所属的集合依次连边,原本的点无点权,集合所代表的点权即为每一集合中的点互相之间的距离。然后以第1个点和第n个点分别为起点计算出点权最短路即可,最终每点的相遇所需时间即为两次计算的值中较大的那一个。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
vector<int>v[];
priority_queue<pair<int,int> >q;
int vis[],d1[],d2[],val[];
int ans[];
int main()
{ int n,m,i,j,k,t,p,s,x;
scanf("%d",&t);
for(p=;p<=t;p++){
scanf("%d%d",&n,&m);
for(i=;i<=n+m;i++)v[i].clear();
memset(vis,,sizeof(vis));
memset(d1,0x3f,sizeof(d1));
memset(d2,0x3f,sizeof(d2));
memset(val,,sizeof(val));
for(i=;i<=m;i++){
scanf("%d%d",&val[i+n],&s);
for(j=;j<=s;j++){
scanf("%d",&x);
v[x].push_back(i+n);
v[i+n].push_back(x);
}
}
d1[]=;
q.push(make_pair(,));
while(!q.empty()){
x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=;
for(i=;i<v[x].size();i++){
int y=v[x][i];
if(d1[x]+val[y]<d1[y]){
d1[y]=d1[x]+val[y];
q.push(make_pair(-d1[y],y));
}
}
}
memset(vis,,sizeof(vis));
d2[n]=;
q.push(make_pair(,n));
while(!q.empty()){
x=q.top().second;
q.pop();
if(vis[x])continue;
vis[x]=;
for(i=;i<v[x].size();i++){
int y=v[x][i];
if(d2[x]+val[y]<d2[y]){
d2[y]=d2[x]+val[y];
q.push(make_pair(-d2[y],y));
}
}
}
int pl,minn=0x3f3f3f3f;
for(i=;i<=n;i++)
if(max(d1[i],d2[i])<minn){
minn=max(d1[i],d2[i]);
pl=i;
}
int cnt=;
printf("Case #%d: ",p);
if(minn<0x3f3f3f3f){
printf("%d\n",minn);
for(i=;i<=n;i++)
if(max(d1[i],d2[i])==minn)
ans[++cnt]=i;
for(i=;i<cnt;i++)
printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
else printf("Evil John\n");
}
return ;
}
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.

hdu5521 Meeting的更多相关文章

  1. HDU5521 Meeting(dijkstra+巧妙建图)

    HDU5521 Meeting 题意: 给你n个点,它们组成了m个团,第i个团内有si个点,且每个团内的点互相之间距离为ti,问如果同时从点1和点n出发,最短耗时多少相遇 很明显题目给出的是个无负环的 ...

  2. hdu-5521 Meeting(最短路)

    题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  3. hdu5521(Meeting)spfa 层次网络最短路

    题意:给出几个集合,每个集合中有Si个点 且任意两个点的距离为ti,现在要求两个人分别从1和n出发,问最短多长时间才能遇到,且给出这些可能的相遇点; 取两个人到达某点时所用时间大的值 然后取最小的   ...

  4. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

  5. HDU5521 Meeting 题解 最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意: 有 \(n\) 个点 \(m\) 个集合,一个点可能处于若干个集合内,属于第 \(i ...

  6. [hdu5521 Meeting]最短路

    题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...

  7. 「赛后补题」Meeting(HDU-5521)

    题意 A,B两个人分别在1和n区.每个区有若干点(区之间的点可以重复,各个区内点间的距离一致),给出区之间有联系的图以及到达所需时间.求两个人见面最短时间以及在哪个区碰面(可有多个) 分析 隐式图搜索 ...

  8. 【HDU5521】Meeting

    题目大意:给定一张\(N\)个点的图,构成了\(M\)个团,每个团内的边权均相等,求图上有多少个点满足到\(1\)号节点和\(N\)号节点的最大值最小. 题解: 本题的核心是如何优化连边,考虑对于每一 ...

  9. [LeetCode] Best Meeting Point 最佳开会地点

    A group of two or more people wants to meet and minimize the total travel distance. You are given a ...

随机推荐

  1. Python安装升级步骤

    1)安装Pyhton2.7wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2tar xjvf Python-2.7.5.t ...

  2. Python中类的约束

    如何在python中进行类的约束 使某些类必须有一些方法 1 python 的抽象类实现 === 约束性不高 Python是 解释性语言 from abc import ABCMeta from ab ...

  3. spring学习-2

    Spring_属性配置细节 1.若字面值包含特殊字符,可以使用<[CDATA[]]>把字面值包裹起来 例:<value><![CDATA[<3333>^]]& ...

  4. Android 关于后台杀死App之后改变服务器状态的一些尝试

    前言: 如题,我的需求是:我需要在App在后台运行(未退出),调出最近运行记录,杀死App服务时,程序能够向服务器发送一条指令,以此达到我想要的目的. Android方面刚刚才开始玩,我一开始想的是可 ...

  5. SQL多表联查总结

    交叉连接:(不常用)返回两个表的笛卡尔乘积(也即全组合排列)中符合查询条件的数据行. 内连接返回连接表中符合连接条件和查询条件的数据行. 左外连接返回符合连接条件和查询条件(即:内连接)的数据行,且还 ...

  6. loj #6216. 雪花挂饰

    (今天碰到的题怎么这么小清新 $n$ 个不相同的点,$q$ 组询问,每次给定 $l,r$,问在 $n$ 个点中,选出 $x$ 个点 $(x \in [l,r])$,用边连起来,能构成多少种不同的树 $ ...

  7. an easy problem(贪心)

    An Easy Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8333   Accepted: 4986 D ...

  8. xmind的使用及入门

    初识 xmind是什么 比较 入门 下载 基本操作 主界面 美化 工具 导出 初识 xmind是什么 说白了就是传说中的思维导图,用它我们可以画出下面这些图: 额,这张图好丑. 好吧,换一张: 他就是 ...

  9. systemd详解(CentOS 7)

    http://blog.51cto.com/xuding/1730952 一.init进程演变 1.init的发展 CentOS 5: SysV init,串行 CentOS 6:Upstart,并行 ...

  10. Asp.net工作流workflow实战之工作流启动与继续(三)

    工作流帮助类: //让工作流继续沿着书签的位置向下执行value是向书签传递参数 wfc.ResumeBookmark(bookmarkName,value); //把传过来的数据value作为输出参 ...