Meeting

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

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.

 
Source
 
题意:有m个集合,每个集合里面的任意两点均有一条距离为ei的无向边,求1和n到其他点的最短距离中最大值的最小值。
思路:最短路模板题。每个集合作为作为一个点,对应的点到集合的距离为ei,最后答案/2。
代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<set>
  7. #include<bitset>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<vector>
  12. using namespace std;
  13. #define eps 0.0000001
  14. typedef long long ll;
  15. typedef pair<int,int> P;
  16. const int maxn=2e5+,maxm=1e5+,inf=0x3f3f3f3f,mod=1e9+;
  17. const ll INF=1e18+;
  18. struct edge
  19. {
  20. int from,to;
  21. ll w;
  22. };
  23. vector<edge>G[maxn];
  24. priority_queue<P,vector<P>,greater<P> >q;
  25. ll dist[][maxn];
  26. void addedge(int u,int v,ll w)
  27. {
  28. G[u].push_back((edge)
  29. {
  30. u,v,w
  31. });
  32. G[v].push_back((edge)
  33. {
  34. v,u,w
  35. });
  36. }
  37. void dij(int t,int s)
  38. {
  39. dist[t][s]=0LL;
  40. q.push(P(dist[t][s],s));
  41. while(!q.empty())
  42. {
  43. P p=q.top();
  44. q.pop();
  45. int u=p.second;
  46. for(int i=; i<G[u].size(); i++)
  47. {
  48. edge e=G[u][i];
  49. if(dist[t][e.to]>dist[t][u]+e.w)
  50. {
  51. dist[t][e.to]=dist[t][u]+e.w;
  52. q.push(P(dist[t][e.to],e.to));
  53. }
  54. }
  55. }
  56. }
  57. void init(int n)
  58. {
  59. for(int i=; i<=*n+; i++) G[i].clear();
  60. }
  61. int main()
  62. {
  63. int T;
  64. scanf("%d",&T);
  65. for(int Case=; Case<=T; Case++)
  66. {
  67. int n,m;
  68. scanf("%d%d",&n,&m);
  69. for(int i=; i<=m; i++)
  70. {
  71. int val;
  72. scanf("%lld",&val);
  73. int t;
  74. scanf("%d",&t);
  75. while(t--)
  76. {
  77. int s;
  78. scanf("%d",&s);
  79. addedge(s,n+i,val);
  80. }
  81. }
  82. for(int i=; i<=*n+; i++) dist[][i]=dist[][i]=INF;
  83. dij(,);
  84. dij(,n);
  85. ll ans=INF;
  86. for(int i=; i<=n; i++)
  87. {
  88. //printf("%lld %lld\n",dist[0][i],dist[1][i]);
  89. ans=min(ans,max(dist[][i],dist[][i]));
  90. }
  91. printf("Case #%d: ",Case);
  92. if(ans>=INF) puts("Evil John");
  93. else
  94. {
  95. printf("%lld\n",ans/);
  96. int cou=;
  97. for(int i=; i<=n; i++)
  98. {
  99. if(!cou&&max(dist[][i],dist[][i])==ans) printf("%d",i),cou++;
  100. else if(cou&&max(dist[][i],dist[][i])==ans) printf(" %d",i),cou++;
  101. }
  102. printf("\n");
  103. }
  104. init(n);
  105. }
  106. return ;
  107. }

最短路模板题

HDU 5521.Meeting 最短路模板题的更多相关文章

  1. poj1511/zoj2008 Invitation Cards(最短路模板题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds    ...

  2. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

  3. 牛客小白月赛6 I 公交线路 最短路 模板题

    链接:https://www.nowcoder.com/acm/contest/136/I来源:牛客网 题目描述 P市有n个公交站,之间连接着m条道路.P市计划新开设一条公交线路,该线路从城市的东站( ...

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

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

  5. HDU 2544 最短路(模板题)

    求1到N的最短路径,模板题,以1为源点,用dijkstra算法(可以用优先级队列优化) #include <iostream> #include <algorithm> #in ...

  6. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  7. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  8. [USACO07FEB]银牛派对Silver Cow Party---最短路模板题

    银牛排队 对于我这种蒟蒻来说,还是不要跑一次单元最短路.跑两次好写呀(- ̄▽ ̄)- 而题目中是有向图.如果如果按照题意进行最短路的话.就会出现一个单终点最短路和一个单起点最短路 对于单起点自然就是套模 ...

  9. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

随机推荐

  1. web--webstorm的一些常用快捷键

    Webstorm的一些常用快捷键   下面是Webstorm的一些常用快捷键: Alt+回车 导入包,自动修正 1. ctrl + shift + n: 打开工程中的文件,目的是打开当前工程下任意目录 ...

  2. c# 数据结构 ArrayList

    数据结构 描述数据之间的关系 行为:添加数据,删除数据,插入数据,查找数据,修改数据 追加数据:向这个结构的末尾添加一个数据 删除数据:在这个结构中删除你指定的数据 插入数据:向这个结构中某一个位置插 ...

  3. 记一次sshd启动报错,Failed to start OpenSSH server daemon.

    sshd -t [root@mysql5-slave proj]# sshd -t @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  4. Android 最简单的MVP案例;

    随手撸个发出来: V:界面层 //界面层需要实现P.View方法,然后重写P.View中的方法:M层给的数据就在这些个方法的参数中: // 还要获取到P.Provide的实例,使用P.Provide去 ...

  5. JAVA中循环删除list中元素的方法总结(同上篇)

    印象中循环删除list中的元素使用for循环的方式是有问题的,但是可以使用增强的for循环,然后今天在使用时发现报错了,然后去科普了一下,再然后发现这是一个误区.下面就来讲一讲..伸手党可直接跳至文末 ...

  6. 26.python常用端口号

    MySQL默认端口 3306 Redis默认端口 6379 MongoDB默认端口 27017 django端口 8000 flask端口 5000 pyspider服务端口 5000(由flask开 ...

  7. 买二手iphone的建议

    手机到手后一定要在第一时间把“按键开关.指纹解锁.指南针.照相机.话筒.听筒.手电筒.定位.WiFi”都测一遍. 环境有无wifi:imei:×××××序列号:××××× 外观和零件:1外观,1是否粗 ...

  8. python 列表复制给另一个列表,改值两个列表均会改变(备忘)

    http://blog.csdn.net/lc_lc2000/article/details/53135839 本意是使A = B,B为一个列表,结果在后续对A的操作中,导致B中的值也改变了,才回忆起 ...

  9. python 数据分析库介绍

    1 引言 高效处理数据的python工具: 与外界进行交互: 读写各种文件格式和数据库 准备: 对数据进行清理.修整.整合.规范化.重塑.切片切换.变形等处理以便进行分析 转换: 对数据集做一些数学和 ...

  10. Balls(扔鸡蛋问题)

    4554 BallsThe classic Two Glass Balls brain-teaser is often posed as:“Given two identical glass sphe ...