Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4542    Accepted Submission(s): 1436

Problem Description
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.

 
Sample Input
2
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
 
Sample Output
Case #1: 3
3 4
Case #2: Evil John

Hint

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.

 
 
 
    最短路,在于建图,如果把每个集合的点都两两建边的话,复杂度爆炸,所以我们对每一个集合建立一个新节点,将集合内的点向这个新节点建边,
权值就是ti,然后跑两次dij枚举每个点找到最小时间和符合最小时间的点。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<functional>
using namespace std;
#define LL long long
#define pli pair<long long,int>
#define mp make_pair
#define inf 0x7fffffffffffff
struct Edge{
int v,w,next;
}e[];
int first[];
int tot,n,m;
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
bool vis[];
LL d1[],d2[];
void dij(int s,LL d[]){
memset(vis,,sizeof(vis));
int tot_n=n+m+;
for(int i=;i<=tot_n;++i) d[i]=inf;
priority_queue<pli,vector<pli>,greater<pli> >q;
q.push(mp(,s));
d[s]=;
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(d[e[i].v]>d[u]+e[i].w){
d[e[i].v]=d[u]+e[i].w;
q.push(mp(d[e[i].v],e[i].v));
}
}
}
}
int main()
{
int t,i,j,k;
int cas=;
cin>>t;
while(t--){int ti,si,a;
memset(first,-,sizeof(first));
tot=;
cin>>n>>m;
for(i=;i<=m;++i){
scanf("%d%d",&ti,&si);
while(si--){
scanf("%d",&a);
add(a,n+i,ti);
add(n+i,a,ti);
}
}
printf("Case #%d: ",++cas);
dij(,d1);
dij(n,d2);
LL mint=inf;
for(i=;i<=n;++i){
mint=min(mint,max(d1[i],d2[i]));
}
if(mint==inf){
puts("Evil John");
}
else{
cout<<mint/<<endl;
for(i=;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf("%d",i);
break;
}
}
i++;
for(;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf(" %d",i);
}
}
puts("");
}
}
return ;
}

HDU5521-最短路-建图的更多相关文章

  1. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  2. hdu4725 The Shortest Path in Nya Graph【最短路+建图】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4297574.html      ---by 墨染之樱花 题目链接:http://acm.hdu ...

  3. Codeforces 938D. Buy a Ticket (最短路+建图)

    <题目链接> 题目大意: 有n座城市,每一个城市都有一个听演唱会的价格,这n座城市由m条无向边连接,每天变都有其对应的边权.现在要求出每个城市的人,看一场演唱会的最小价值(总共花费的价值= ...

  4. hdu 5294 Tricks Device 最短路建图+最小割

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Other ...

  5. hdu 4725 The Shortest Path in Nya Graph (最短路+建图)

    The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  6. 第八届河南省赛C.最少换乘(最短路建图)

    C.最少换乘 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 94  Solved: 25 [Submit][Status][Web Board] De ...

  7. 『The Captain 最短路建图优化』

    The Captain(BZOJ 4152) Description 给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小 ...

  8. HDU-4725.TheShortestPathinNyaGraph(最短路 + 建图)

    本题思路:主要是建图比较麻烦,因为结点可以在层与层之间走动,也可以在边上进行走动,所以主要就是需要找到一个将结点和层统一化处理的方法. 所以我们就可以对于存在边的结点建边,层与层之间如果层数相差一也建 ...

  9. bzoj2662: [BeiJing wc2012]冻结 最短路 建图

    好久没有1A题啦♪(^∇^*) 一个sb建图,我居然调样例调了10min 看起来是双向边,其实在建图的时候要当成有向图, 否则他会时间倒流(233) 把每个点裂成k个点,然后把每条边裂成4条边(正向反 ...

随机推荐

  1. python 之操作redis数据库(非关系型数据库,k-v)

    数据库: 1. 关系型数据库 表结构 2. 非关系型数据库 nosql (k - v 速度快),常用的时以下三种: memcache 存在内存里 redis 存在内存里 mangodb 数据还是存在磁 ...

  2. The Cheap KD 10 is my best shoe yet

    10 years of anything is fairly huge Cheap KD 10, but adding something as great as Flyknit causes it ...

  3. 在Windows上安装Elasticsearch 5.x

    在Windows上安装Elasticsearch 5.x 自己想学习Elasticsearch,但是又不懂Linux,按照同事给的Linux安装教程,也是搞不明白,于是想先在Windows上安装一下入 ...

  4. matlab和mathematics最新的FTP地址

    https://dio.obspm.fr/interne/logiciels/matlab/ 分享一个地址,非常好的FTP网站.

  5. 【PS技巧】创建2D对象的描边阴影

    在本场景中,怪物死亡掉落宝袋.所以在玩家眼里,宝袋是掉落在场景里,而不是像其他界面的UI元素,悬浮在场景上的. 所以,我们需要给宝袋添加阴影,增加它与场景之间的视觉过渡,比较简单的办法是使用阴影,正如 ...

  6. wait、notify为什么要放在同步代码块中

    等待方遵循的原则: 获取对象的锁,不满足条件就调用wait()方法,条件满足继续执行 通知方原则: 获取对象的锁,改变条件,然后notify 每个对象都有一个监视器锁,这个监视器锁的数据结构如下: w ...

  7. php中获得数组长度的方法

    php中获得数组长度的方法   count统计数组里元素的个数:  strlen是统计数组中元素的长度: 你如果想统计数组中所有元素的长度,那就用循环统计吧tqeb 代码: $a  =  array( ...

  8. 应用libjpeg提取jpeg质量因子

    http://blog.csdn.net/lzhq28/article/details/7775222 版权声明:本文为博主原创文章,未经博主允许不得转载. data = new BYTE [cinf ...

  9. 前端学习笔记之ES6快速入门

    0x1 let和const let ES6新增了let命令,用于声明变量.其用法类似var,但是声明的变量只在let命令所在的代码块内有效. { let x = 10; var y = 20; } x ...

  10. android ramdisk

    android ramdisk 1.android文件系统的结构android源码编译后得到system.img,ramdisk.img,userdata.img映像文件.其中, ramdisk.im ...