传送门

题目

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. 做什么职业,也别做程序员,尤其是Java程序员

    千万别做程序员,尤其别做Java这种门槛低,入门快的程序员(别跟我说Java搞精通了也很牛之类的,原因不解释,做5年以上就知道了),程序员本来就是我见过最坑爹的职业了...Java程序员更是,现在满地 ...

  2. Kattis - redblacktree Red Black Tree (树形背包)

    问题:有一课含有n(n<=2e5)个结点的数,有m(m<=1000)个结点是红色的,其余的结点是黑色的.现从树中选若干数量的结点,其中红色的恰有k个,并且每个结点都不是其他任何另一个结点的 ...

  3. CodeForces - 961D:Pair Of Lines (几何,问两条直线是否可以覆盖所有点)

    You are given n points on Cartesian plane. Every point is a lattice point (i. e. both of its coordin ...

  4. Maven环境下多模块项目构建

    Maven环境下多模块项目构建 一.新建项目 1.建立我们的父模块par 2.建立我们的子模块dao层 3.建立我们的子模块service层 4.建立我们的子模块web层 5.全部配置完成后,怎么把我 ...

  5. linux大于2T的磁盘格式化

    fdisk默认只能格式小于2T的磁盘,我们经常会碰到大于2T的磁盘,我们不能fdisk 格式化. 我们得用parted 来的格式化 parted 命令可能没有,yum install -y parte ...

  6. 基于springboot+kotlin+gradle构建的框架的坑

    项目采用以上技术构建,于是本人就尝试构建自己的脚手架,然后遇到一大推问题. 使用的是springinitials构建,IDE是:IDEA 现在也是知其然不知其所以然,但是先记录下来修改过程,以后等知识 ...

  7. Docker资源

    1.Docker入门教程 http://www.code123.cc/docs/docker-practice/repository/config.html 2.Docker入门教程 http://w ...

  8. ASP.NET网站性能提升的几个方法

    1. HTTP 压缩 HTTP 压缩通常用于压缩从服务端返回的页面内容.它压缩HTTP请求和响应,这个会是巨大的性能提升.我的项目是基于Window Server 2003开发的,可以参考这篇文章. ...

  9. HDU2579(bfs迷宫)

    Dating with girls(2) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  10. SpringCloud组件的简单介绍

    springcloud官网springcloud中文网站 最近开始接触springcloud,所以先了解了一下最最基本概念. Spring Cloud ConfigSpring配置管理工具包,让你可以 ...