HDU_5521_Meeting
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3704 Accepted Submission(s): 1187
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.
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
3 4
Case #2: Evil John
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.
- 在路径1->n中取一点使得max(time(1->vex),time(n->vex))最小
- 很容易考虑从1和n分别跑一遍单源最短路得到两个距离数组dis1和disn,然后逐个比较下就可以了
- 但是这题难点在于给出结点间关系是以set方式给予的,我们如果不能加速自当前点寻找可达点的查找速度就会tle
- 每个set看似分离,实际上因为set相交的缘故,在不同set中的两个点是可达的
- 我们可以先对于单个set考虑,可以通过缩点,对于一个set中的点用一个新的点表示,然后用新点和其他set相连
- 接下来就是考虑怎样建边使得同一个set中的点之间的距离相同
- 新加的点不会增加两个点之间的距离,自set中一点到新点不应有距离的增加,那么我们建边的时候set内的点到新点距离为0,新点到set内每一个点的距离都相同
- 通过这样的缩点和建边就可以将原本的set结构等价转换,我们只需要跑两遍最短路即可
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1e6 + ;
const LL inf = 0x3f3f3f3f3f;
const int npos = - ;
const int mod = 1e9 + ;
const int mxx = + ;
const double eps = 1e- ;
const double PI = acos(-1.0) ; struct node{
LL len;
int i;
bool operator < (const node &r)const{
if(len!=r.len)
return len<r.len;
else
return i<r.i;
}
};
struct enode{
int v;
LL w;
int next;
};
struct qnode{
int u;
LL dis;
bool operator < (const qnode &r)const{
return dis>r.dis;
}
};
enode edge[maxn<<];
int head[maxn<<], cnt;
void addedge(int x, int y, LL z){
edge[cnt]=(enode){y,z,head[x]};
head[x]=cnt++;
}
std::vector< node > buf;
int T, n, m, u, v, s, no1, non;
LL w, dis1[maxn], disn[maxn], ans;
bool vis[maxn];
void DIJ(LL *dis, int source){
LL now;
priority_queue< qnode > Q;
qnode temp;
for(int i=;i<=n+m;i++){
vis[i]=false;
dis[i]=inf;
}
dis[source]=0LL;
Q.push((qnode){source,dis[source]});
while(!Q.empty()){
temp=Q.top();
Q.pop();
if(!vis[temp.u]){
u=temp.u;
now=temp.dis;
vis[u]=true;
for(int i=head[u];i!=-;i=edge[i].next){
v=edge[i].v;
w=edge[i].w;
if(now+w<=dis[v]){
dis[v]=now+w;
Q.push((qnode){v,dis[v]});
}
}
}
}
}
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d",&T)){
for(int kase=;kase<=T;kase++){
no1=;
non=;
scanf("%d %d",&n,&m);
cnt=;
memset(head,-,sizeof(head));
for(int i=;i<=m;i++){
scanf("%lld %d",&w,&s);
for(int j=;j<=s;j++){
scanf("%d",&u);
if(u==)no1=;
if(u==n)non=;
v=n+i;
addedge(u,v,0LL);
addedge(v,u,w);
}
}
printf("Case #%d: ",kase);
if(!(no1&&non)){
printf("Evil John\n");
continue;
}
DIJ(dis1,);
DIJ(disn,n);
buf.clear();
for(int i=;i<=n;i++)
buf.push_back((node){max(dis1[i],disn[i]),i});
sort(buf.begin(),buf.end());
ans=buf[].len;
if(ans==inf){
printf("Evil John\n");
}else{
printf("%lld\n",ans);
printf("%d",buf[].i);
int i=;
while(i<buf.size() && buf[i].len==ans)
printf(" %d",buf[i++].i);
cout<<endl;
}
}
}
return ;
}
HDU_5521_Meeting的更多相关文章
随机推荐
- windowmasker 标记基因组中的重复序列和低复杂度序列
下载地址:ftp://ftp.ncbi.nlm.nih.gov/pub/agarwala/windowmasker/ 在这个目录下 其中windowmasker 为linux 平台的可执行文件 win ...
- 【Java面试题】16 静态代码块,main方法,构造代码块,构造方法
public class HelloB extends HelloA{ public HelloB() { System.out.println("HelloB"); } { Sy ...
- jquery注册文本框获取焦点清空,失去焦点赋值的简单实例
在我们开发过程中特别是用户注册时会有一个效果,就是文本框获取焦点清空提示,如果用户没有输入信息失去焦点赋值上我们的提示语. <html> <head> <meta h ...
- .NETFramework、C#、VisualStudio 这三者之间关系,你了解吗!
.NetFrameWork 是微软开发的以"虚拟机"运行,以通用语言运行库为基础,在其上面进行各种语言开发的一个开发平台. C# 是一个和平台更好交互,以托管在虚拟机上的一个语法糖 ...
- linux下mysql 启动命令
1,使用service 启动.关闭MySQL服务 service mysql start service mysql stop service mysql restart 运行上面命令,其实是serv ...
- 给IT同学推荐这15个不错的学习网站,收藏起来慢慢看吧
1.学堂在线:http://www.xuetangx.com 目前,学堂在线运行了包括包括清华大学.北京大学.复旦大学.斯坦福大学.麻省理工学院.加州大学伯克利分校等国内外几十所顶尖高校的优质课程.在 ...
- GIS-001-gdal软件下载地址
http://www.gisinternals.com/ http://download.gisinternals.com/sdk/downloads/release-1600-x64-gdal-1- ...
- Python 数据类型:元组
一.元组介绍 1. 元组可以存储一系列的值,使用括号来定义,每个元素之间用逗号隔开,形如 ('a', 'b', 'c', 'd')2. 元组一旦定义,元组内的元素就不能再修改,除非重新定义一个新的元组 ...
- capitalize()
capitalize() 是字符串的一个方法,用于把字符串的第一个字母转换成大写 In [1]: str = 'hello world' In [2]: str.capitalize() Out[2] ...
- Linux 下配置网卡的别名即网卡子IP的配置
what 什么是ip别名?用windows的话说,就是为一个网卡配置多个ip.when 什么场合增加ip别名能派上用场?布网需要.多ip访问测试.特定软件对多ip的需要...and so on. ho ...