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的更多相关文章
随机推荐
- dm8127前段采集和抓拍
高清监控(944275216) 2014-1-17 9:36:24自主研发高清网络摄像机,720P.960P.1080P系列产品,经济型.低照型.宽动态型等各种机型可选,支持onvif.P2 ...
- poj 3414 Pots(广搜BFS+路径输出)
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:id=3414">http://poj.org/probl ...
- WordCount 远程集群源码
package test; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop ...
- SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery) 1.启用Ad Hoc Distributed Queries 在使用openrowset ...
- ChemOffice Professional 16.0新增了哪些功能
ChemOffice Professional 16.0是为终极化学和生物组件设计,可满足化学家和生物学家的需求.ChemOffice Professional帮助科学家有效地检索数据库,包括SciF ...
- 【转】VS2008快速将代码中字符串改为_T(“”)风格的方法
用VC在修改一些老程序的时候,经常面临“UNICODE化”的工作.就是将一些传统C语言风格的字符串,如“string”,改为既能够通过多字节编码工程编译,又能通过UNICODE工程编译的代码,即形如_ ...
- 【ExtJs】 ExtJs4.2 基本表单组件的使用
包含ExtJs 基本的组件radioGroup,ComboBox,File,NumberField... <%-- Created by IntelliJ IDEA. User: Adminis ...
- Linux 任务计划:crontab
(1) 什么是任务计划:也就是设置服务器在某个指定的时间执行某个指定的任务,比如执行一个命令,或执行一个脚本(2) Linux 使用 cron 服务来制定任务计划,cron 是服务名称,crond 是 ...
- Python 内部类
内部类也就是在类的内部再定义类,如下: #!/usr/bin/env python #-*- coding:utf-8 -*- class People(object): class Chinese( ...
- PyQt4颜色对话框QColorDialog
QColorDialog提供了用于显示颜色的对话框. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...