传送门

题目

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. 又是毕业季1&&又是毕业季2

    又是毕业季2 n/k; 又是毕业季2 一开始很容易想到枚举n个数取k个的所有组合,然后分别用辗转相除法求最大公约数,但是复杂度明显不符合要求,于是必须换一种思路. 我们想到,k个数的公约数含义就是这k ...

  2. 一段tcl代码

    #!/usr/bin/wish proc icanspeak {} { set name [.ent get] } { exec s $name } } label .lab -text " ...

  3. I.MX6 dts 在哪里、怎么编译

    /************************************************************************ * I.MX6 DTS 在哪里.怎么编译 * 说明: ...

  4. burpsuite使用以及repeater模块实现重放攻击

    第一.burp suit是什么? Burp Suite 包含了一系列burp 工具,这些工具之间有大量接口可以互相通信,之所以这样设计的目的是为了促进和提高 整个攻击的效率.平台中所有工具共享同一ro ...

  5. 洛谷 P1062 数列

    题目描述 给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列,例如,当k=3时,这个序列是: 1,3,4,9,10,12,13,… (该序列实际上就是 ...

  6. 利用sort对数组快速排序

    // sort内部使用快速排序,每次比较两个元素大小的时候如果没有参数,则直接判断字母表,如果有参数,则把正在比较的两个参数传入自定义方法并调用(正在比较的两个数会传给自定义方法的v1.v2),如果返 ...

  7. XML数据库的尝试

    首先祝大家新年快乐.身体健康,平安就是福气. 对于一般的个人迷你项目,数据量不大的时候,完全没有必要使用数据库,管理数据使用XML就可以了. 自己尝试写了一个XML数据库,插入1w条小记录,大概3M大 ...

  8. 使用bat文件实现批量重命名功能

    在生活中我们总会碰到对大量文件进行重命名操作,这时如果一个一个的,选取文件→右键→重命名→选取文件,这样操作势必会浪费大量时间. 现在小编就告诉大家一个使用bat文件(命令行)的方法,快速对文件进行重 ...

  9. hl7 V2中Message Control ID的含义及应用

    HL7 v2中的MSH,MSA段都有Message Control ID. 有几点需要注意: 1.所有的MessageControlID必须唯一 2.对于MSH中的MessageControlID, ...

  10. LeetCode第五题:Longest Palindromic Substring

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...