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的更多相关文章
随机推荐
- Android检测Cursor泄漏的原理以及使用方法(转)
简介: 本文介绍如何在 Android 检测 Cursor 泄漏的原理以及使用方法,还指出几种常见的出错示例.有一些泄漏在代码中难以察觉,但程序长时间运行后必然会出现异常.同时该方法同样适合于其他需要 ...
- LogCat大量Unexpected value from nativeGetEnabledTags: 0
在执行模拟器的时候.LogCat 输出非常多Unexpected value from nativeGetEnabledTags: 0 提示.导致非常多本来须要输出的信息被瞬间覆盖了,查询后得知是sd ...
- PHP截断函数mb_substr()
提示:mb_substr在于php中是默认不被支持的我们需要在在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到;extension=php_mbstring.dll ...
- linux cp 复制文件夹
复制文件夹需要添加 -r 或 -R 参数(recursive: 递归的:循环的) 如 cp -r DIR_A DIR_B; 同理,rm 也一样. 如 rm -r DIR_B
- Cookie文件格式解析
原文参考:http://blog.csdn.net/lixianlin/article/details/2738229 1.Cookie文件的实质 Cookie实际上是Web服务端与客户端(典型的是浏 ...
- [python每日一库]——hotshot
High performance logging profiler 官方文档:http://docs.python.org/2/library/hotshot.html#module-hotshot ...
- Facebook开源技术识别网购评论
1.自然语言处理2.情感分析3.监督学习模型4.词向量 5.fasttext 汉藏语系,是语言系属分类(Language family)的一种,分为汉语族和藏缅语族,是用汉语和藏语的名称概括与其有亲属 ...
- mysql数据库binary log中的事件到底是什么?
需求描述: 最近看mysql备份恢复的时候,基于时间点恢复,提到了binary log中存的是"事件" 那么到底什么是事件呢 概念解释: binary log中存的是事件(even ...
- mysql中,如何查看数据库中当前可用的校勘?字符集默认的collation?
需求描述: mysql的字符集在使用的过程中会有一些规则,这些规则就组成了校勘, 也就是通过什么规则做什么事,比如,如何比较两个字符的大小,后台都是有一些 规则,这些规则就是校勘的一部分. 那么,查看 ...
- 扁平化你的Qt应用程序
什么是扁平化 这里的扁平化指的是交互设计方面的一种风格. 扁平化是随着极简注意的风潮流行起来的,这个概念最核心的地方就是放弃一切装饰效果,诸如阴影.透视,纹理,渐变等等能做出3D效果的元素一概不用.全 ...