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. Xcode8免证书生产IPA打包文件

    免证书生产IPA打包文件   修改Xcode配置文件: 关闭Xcode.然后打开“其他-终端”,就是命令行工具 cd /Applications/Xcode.app/Contents/Develope ...

  2. 机器学习第2周---炼数成金-----线性回归与Logistic

    重点归纳 回归分析就是利用样本(已知数据),产生拟合方程,从而(对未知数据)迚行预测用途:预测,判别合理性例子:利用身高预测体重:利用广告费用预测商品销售额:等等.线性回归分析:一元线性:多元线性:广 ...

  3. Does Daemon Thread Exit with Main Thread?

    主线程(进程)退出后,主线程创建的守护线程也会退出吗? 通过下面的代码测试: Demo1: 进程创建普通线程 #!/usr/bin/python3 # FileName: daemonThread.p ...

  4. libxml2 在mingw中 xmlfree连接错误问题

    libxml2 在mingw中 xmlfree连接错误问题 2013年10月02日 ⁄ 综合 ⁄ 共 1527字 ⁄ 字号 小 中 大 ⁄ 评论关闭 原地址:http://blog.csdn.net/ ...

  5. Delphi APP 開發入門(十)REST Client 開發

    Delphi APP 開發入門(十)REST Client 開發 分享: Share on facebookShare on twitterShare on google_plusone_share ...

  6. php array_mutisort

    PHP中array_multisort可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序. 关联(string)键名保持不变,但数字键名会被重新索引. 输入数组被当成一个表的列并以 ...

  7. 阿里云搭建go开发环境

    开通了一个阿里云来玩,记录一下环境搭建的过程 运行环境 ECS Ubuntu 16.04 64位 过程 #切换到安装文件夹 cd /usr/local #下载go #由于墙的原因,直接下载官方的可能会 ...

  8. dubbo总结

     一 .Dubbo产生背景 单一应用架构当网站流量很小时,只需一个应用,将所有功能都部署在一起,以减少部署节点和成本.此时,用于简化增删改查工作量的 数据访问框架(ORM) 是关键. 垂直应用架构当访 ...

  9. Mybatis 一对一、一对多、多对多

    一对一返回resultType <!-- 查询订单关联查询用户信息 resultType --> <select id="findOrderCustom" res ...

  10. mysql5.7 linux安装参考

    安装参考: https://www.cnblogs.com/ianduin/p/7679239.html http://blog.csdn.net/wb96a1007/article/details/ ...