Meeting

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

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.

/*
hdu 5521 最短路 problem:
给你n个点的图,甲在1,乙在n. 它们同时移动,问相聚时的最小花费. 然后是m个点集,点集内的任意两点之间的移动花费为ti solve:
因为是同时移动. 所以分别对1和n求一个最短路. 然后每个节点取两个最短路中的最大值就能得到花费.
最开始想的是建立所有边,但是边的数量会太多.
所有走到一个节点时,将其所在的所有点集处理一遍. 而且只需要处理一次即可,已经维护了一个最短状态. hhh-2016-08-30 19:56:48
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson i<<1
#define rson i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const ll mod = 1e9+7;
const int maxn = 100010; struct qnode
{
int v,c;
qnode(int _v = 0 ,int _c =0) : v(_v),c(_c){}
bool operator <(const qnode &a)const
{
return c > a.c;
}
}; bool vis[maxn];
bool tvis[maxn];
ll dis1[maxn], dis2[maxn],cost[maxn];
int anspos[maxn];
int num[maxn];
vector<int> pos[maxn];
vector<int> have[maxn];
int T,n,m;
void dijkstra(int start,ll dis[])
{
for(int i= 1;i <= n;i++)
{
vis[i] = tvis[i] = 0;
dis[i] = inf;
}
priority_queue<qnode> q;
q.push(qnode(start,0));
dis[start] = 0;
qnode t;
while(!q.empty())
{
t = q.top();
q.pop();
int u = t.v;
if(vis[u]) continue;
vis[u] = 1;
for(int i = 0 ;i < pos[u].size();i++)
{
int t = pos[u][i];
// if(tvis[t]) continue;
// tvis[t] = 1;
for(int j = 0;j<have[t].size();j++)
{
int v = have[t][j];
if(v == u)
continue;
if(dis[v] > dis[u] + cost[t])
{
dis[v] = dis[u] + cost[t];
q.push(qnode(v,dis[v]));
}
}
}
}
} int main()
{
int x;
// freopen("in.txt","r",stdin);
scanfi(T);
int cas = 1;
while(T--)
{
scanfi(n),scanfi(m);
for(int i = 0 ;i <= n;i++)
pos[i].clear();
for(int i = 0;i <= m;i++)
have[i].clear();
for(int i = 1;i <= m;i++)
{
scanf("%I64d%d",&cost[i],&num[i]);
for(int j = 0;j < num[i];j++)
{
scanfi(x);
pos[x].push_back(i);
have[i].push_back(x);
}
}
dijkstra(1,dis1);
dijkstra(n,dis2);
int cnt = 0;
ll ans = inf;
for(int i = 1;i <= n;i++)
{
ll t = max(dis1[i],dis2[i]);
if(t != inf){
ans = min(ans,t);
}
}
for(int i =1; i <= n;i++)
{
if(max(dis1[i],dis2[i]) == ans)
anspos[cnt++] = i;
}
printf("Case #%d: ",cas++);
if(ans != inf)
{
printf("%I64d\n",ans);
for(int i = 0;i < cnt;i++)
{
printf("%d%c",anspos[i],i == cnt-1 ? '\n':' ');
}
}
else
printf("Evil John\n");
}
return 0;
}

  

hdu 5521 最短路的更多相关文章

  1. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  2. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  3. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  5. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  6. hdu 5521 Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...

  7. HDU 5521 Meeting【最短路】

    今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...

  8. HDU 5521:Meeting(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5521 Meeting Problem Description   Bessie and her friend E ...

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

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

随机推荐

  1. alpha-咸鱼冲刺day9

    一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 大概差不多了.不过提交似乎又出了问题正在修改ing 四,问题困难 页面整合啥的凑合一下.因为后面跟着学长速成的PHP,现在还在研 ...

  2. 《Effective Objective-C 2.0》摘要

    前一段时间将<Effective Objective-C 2.0>这本书浏览了一遍,说一下几个觉得比较有意思的知识点. 感觉这本书是ios开发必看的一本书,最基础的,以及稍微高阶一点的oc ...

  3. 用python实现简单购物车功能

    all_asset = 0 i1 = input("请输入总资产:") all_asset = int(i1) goods = [ {'name':'电脑','price':199 ...

  4. 0基础菜鸟学前端之Vue.js

    简介:0基础前端菜鸟,啃了将近半月前端VUE框架,对前端知识有了初步的了解.下面总结一下这段时间的学习心得. 文章结构 前端基础 Vue.js简介 Vue.js常用指令 Vue.js组件 Vue.js ...

  5. thinkphp中定义自己的函数

    可以在前台和后台的公共文件夹中common.php中定义自己的函数,这样就可以在控制器中调用,而不需要调用对象了 /** * @name addvtorandp * @author 黄峰1664253 ...

  6. Spring Cache扩展:注解失效时间+主动刷新缓存(二)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. URL编码和Base64编码 (转)

    我们经常会遇到所谓的URL编码(也叫百分号编码)和Base64编码.      先说一下Bsae64编码.BASE64编码是一种常用的将二进制数据转换为64个可打印字符的编码,常用于在通常处理文本数据 ...

  8. 使用location.href跳转页面在火狐浏览器中报错404

    HTML文件中引入外部js文件,在该js文件里用window.location.href跳转相对路径下的html地址,火狐浏览器会报错404,而谷歌浏览器却显示正常·,分析了一下原因:在识别相对路径时 ...

  9. python利用递归函数输出嵌套列表的每个元素

    1.先用 for 循环取. for item in l: if isinstance(item ,list): for newitem in item: print(newitem) else: pr ...

  10. Java8新特性第3章(Stream API)

    Stream作为Java8的新特性之一,他与Java IO包中的InputStream和OutputStream完全不是一个概念.Java8中的Stream是对集合功能的一种增强,主要用于对集合对象进 ...