hdu5521 Meeting
题目
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.
题目大意
现在给出一个图,其中分成点分成一个集合一个集合的,每一集合中的点互相之间的距离都是相同的,也给出来了;现在要求两个人分别从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 ;
}
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.
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.
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的更多相关文章
- HDU5521 Meeting(dijkstra+巧妙建图)
HDU5521 Meeting 题意: 给你n个点,它们组成了m个团,第i个团内有si个点,且每个团内的点互相之间距离为ti,问如果同时从点1和点n出发,最短耗时多少相遇 很明显题目给出的是个无负环的 ...
- hdu-5521 Meeting(最短路)
题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- hdu5521(Meeting)spfa 层次网络最短路
题意:给出几个集合,每个集合中有Si个点 且任意两个点的距离为ti,现在要求两个人分别从1和n出发,问最短多长时间才能遇到,且给出这些可能的相遇点; 取两个人到达某点时所用时间大的值 然后取最小的 ...
- ACM学习历程—HDU5521 Meeting(图论)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...
- HDU5521 Meeting 题解 最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意: 有 \(n\) 个点 \(m\) 个集合,一个点可能处于若干个集合内,属于第 \(i ...
- [hdu5521 Meeting]最短路
题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...
- 「赛后补题」Meeting(HDU-5521)
题意 A,B两个人分别在1和n区.每个区有若干点(区之间的点可以重复,各个区内点间的距离一致),给出区之间有联系的图以及到达所需时间.求两个人见面最短时间以及在哪个区碰面(可有多个) 分析 隐式图搜索 ...
- 【HDU5521】Meeting
题目大意:给定一张\(N\)个点的图,构成了\(M\)个团,每个团内的边权均相等,求图上有多少个点满足到\(1\)号节点和\(N\)号节点的最大值最小. 题解: 本题的核心是如何优化连边,考虑对于每一 ...
- [LeetCode] Best Meeting Point 最佳开会地点
A group of two or more people wants to meet and minimize the total travel distance. You are given a ...
随机推荐
- java学习笔记 --- 多线程(1)
1:要想了解多线程,必须先了解线程,而要想了解线程,必须先了解进程,因为线程是依赖于进程而存在. 2:什么是进程? 通过任务管理器我们就看到了进程的存在. 而通过观察,我们发现只有运行的程序才会出现进 ...
- canvas基础学习(一)
一.概述 canvas它和其它的HTML5标签的使用基本一致,但是它相当于在浏览器中建立一个画布,可以再这个画布上画图.创建动画甚至是3D游戏.由于canvas要适配不同终端的分辨率,所以尽可能的在标 ...
- linux shell 学习笔记--比较操作
整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" -ne "$b&qu ...
- 242. Valid Anagram Add to List
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- redis3.2.11 安装
wget http://download.redis.io/releases/redis-3.2.11.tar.gz [root@hdp01 src]# .tar.gz -C /opt/ [root@ ...
- 每天一个linux命令(15):head命令
版权声明更新:2017-05-19博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- java向excel写数据
package pymongo1; import java.io.File;import java.io.IOException;import java.io.OutputStream; import ...
- Unity项目UI图片压缩格式(UGUI)
http://blog.csdn.net/bobodan123/article/details/70316538 UI制作时候使用的是Ps 8位 RGB通道的色彩. 但导出的是16位RGBA色彩的图片 ...
- c#多线程实现定时执行代码与lock锁操作
总结以下三种方法,实现c#每隔一段时间执行代码: 方法一:调用线程执行方法,在方法中实现死循环,每个循环Sleep设定时间: 方法二:使用System.Timers.Timer类: 方法三:使用Sys ...
- 系列文章----.Net程序员学用Oracle系列
.Net程序员学用Oracle系列(18):PLSQL Developer 攻略 .Net程序员学用Oracle系列(17):数据库管理工具(SQL Plus) .Net程序员学用Oracle系列(1 ...