Meeting 加虚拟边
fences they were separated into different blocks. John's farm are divided into nn blocks labelled from 11 to nn.
Bessie lives in the first block while Elsie lives in the nn-th one. They have a map of the farm
which shows that it takes they titi minutes to travel from a block in EiEito another block
in EiEi where Ei (1≤i≤m)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.
InputThe first line contains an integer T (1≤T≤6)T (1≤T≤6), the number of test cases. Then TT test cases
follow.
The first line of input contains nn and mm. 2≤n≤1052≤n≤105. The following mm lines describe the sets Ei (1≤i≤m)Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)ti(1≤ti≤109) and Si (Si>0)Si (Si>0) firstly. Then SiSi integer follows which are the labels of blocks in EiEi. It is guaranteed that ∑mi=1Si≤106∑i=1mSi≤106.OutputFor 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
可以将给顶集合的元素连到一个虚拟结点上,求出最短路来再/2,这样避免了大量的重复加边,还避免了小数
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<memory>
#include<bitset>
#include<string>
#include<functional>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int MAXN = 5e5 ; #define INF 0x3f3f3f3f /*
连接虚拟结点
到该点的距离为L
求出最短路/2 避免小数!
*/
LL T, d, n, m, cnt;
struct edge
{
edge(LL _a,LL _b):to(_a),cost(_b){}
LL to, cost;
};
vector<edge>E[MAXN];
LL dist1[MAXN], dist2[MAXN];
bool vis[MAXN];
void addedge(LL f,LL to,LL dis)
{
E[f].push_back(edge(to, dis));
E[to].push_back(edge(f, dis));
}
void init()
{
for (LL i = ; i < MAXN; i++)
E[i].clear();
}
void spfa(LL beg, LL lowcost[])
{
queue<LL> q;
memset(vis, false, sizeof(vis));
for (int i = ; i <= n + m; i++)
lowcost[i] = INF;
lowcost[beg] = ;
vis[beg] = true;
q.push(beg);
while (!q.empty())
{
LL f = q.front();
q.pop();
vis[f] = false;
for (int i = ; i < E[f].size(); i++)
{
if (lowcost[E[f][i].to] > lowcost[f] + E[f][i].cost)
{
lowcost[E[f][i].to] = lowcost[f] + E[f][i].cost;
if (!vis[E[f][i].to])
{
vis[E[f][i].to] = true;
q.push(E[f][i].to);
}
}
}
}
}
int main()
{
scanf("%lld", &T);
for(LL cas = ;cas <= T; cas++)
{
init();
scanf("%lld%lld", &n, &m);
LL tmp, tt;
for (LL i = ; i <= m; i++)
{
scanf("%lld%lld", &d, &tmp);
while (tmp--)
{
scanf("%lld", &tt);
addedge(tt, n + i, d);
}
}
spfa(, dist1);
spfa(n , dist2);
LL ans = INF;
for (int i = ; i <= n; i++)
ans = min(ans, max(dist1[i], dist2[i]));
if (ans == INF)
printf("Case #%lld: Evil John\n", cas);
else
{
printf("Case #%lld: %lld\n", cas, ans / );
bool f = false;
for (int i = ; i <= n; i++)
{
if (max(dist1[i], dist2[i]) == ans)
{
if (!f)
printf("%d", i), f = true;
else
printf(" %d", i);
}
}
printf("\n");
}
}
}
Meeting 加虚拟边的更多相关文章
- HDU 5521 Meeting(虚拟节点+最短路)
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- Qt - 锁屏界面加虚拟小键盘
一.实现效果 鼠标点击"密码输入栏",弹出虚拟键盘,输入锁屏密码后,点击虚拟键盘外部区域,则会隐藏虚拟键盘,再点击登录,成功进入主界面. 二.虚拟键盘-程序设计 2.1 frmNu ...
- Wampserver-添加虚拟主机
鼠标左键点击,之后点击第一个 localhost(有一个小房子) 添加虚拟地址 具体添加 完成界面 注意:这个时候一定需要重启一个Wampserver64 如果没有重启直接进入4这个步骤,会发现进入的 ...
- ASP.NET 小白从零开始建站简易教程 (一)域名、虚拟主机、FTP上传文件
只考虑性价比,纯新手实验无备案.跟着步骤走半小时即可收获独立的个人网站一枚! 我的实验站 http://www.bearlab.site/ ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄ 目前总价花费86元(域名加虚 ...
- iis express添加虚拟目录
在调试WEB时,还是使用IIS EXPRESS比较方便, 在IIS中,选择网站,右击,添加虚拟目录或者应用程序,就能添加虚拟目录了.. 在IIS EXPRESS中,添加虚拟目录如下 1.右击IIS E ...
- 虚拟机上CentOS-6.9-x86_64系统安装教程
最近想学学Linux系统如何使用,于是想用VM安装虚拟机学习一下. linux系统比较多,我这里用的是CentOS-6.9-x86_64 一.下载系统 下载地址:https://www.centos. ...
- Oracle性能调优之虚拟索引用法简介
本博客记录一下Oracle虚拟索引的用法,虚拟索引是定义在数据字典中的伪索引,可以说是伪列,没有修改的索引字段的.虚拟索引的目的模拟索引,不会增加存储空间的使用,有了虚拟索引,开发者使用执行计划的时候 ...
- 通过LVS+Keepalived搭建高可用的负载均衡集群系统
1. 安装LVS软件 (1)安装前准备操作系统:统一采用Centos6.5版本,地址规划如下: 服务器名 IP地址 网关 虚拟设备名 虚拟ip Director Server 192.168 ...
- Hadoop集群(第1期)_CentOS安装配置
CentOS 是什么? CentOS是一个基于Red Hat 企业级 Linux 提供的可自由使用的源代码企业级的 Linux 发行版本.每个版本的 CentOS 都会获得七年的支持(通过安全更新方式 ...
随机推荐
- jQuery Ajax使用实例
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.js"></script> <scr ...
- SSM学习
一.https://www.cnblogs.com/zyw-205520/p/4771253.html 二.https://blog.csdn.net/dwhdome/article/details/ ...
- CF792C Divide by Three
思路: dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #include < ...
- javascript中闭包与作用域的理解
很多js的框架与插件编写都用到了闭包,所以,阅读和掌握闭包很有必要.最近学习vue框架时,经常会猜想很多功能的native js实现,很多都应用到了闭包,闭包除了目前已知的一些特性,如:可以保持局部变 ...
- ECMAScript6之箭头函数
2015年6月17日,ECMAScript 6发布正式版本,即ECMAScript 2015. 函数作为js语言中的一等公民.自然Es6中推出的箭头函数(=>)也是备受瞩目的.那我们接下来看下传 ...
- Objective-C 里面的类对象复用小结
OC 提供了单继承 (Inheritance), Category, Extension, Protocol 这几种基本的类与对象层面的复用机制,作一小结. 在这几个机制中,继承提供了纵向的复用,可以 ...
- (转)淘淘商城系列——使用maven tomcat插件启动web工程
http://blog.csdn.net/yerenyuan_pku/article/details/72672138 上文我们一起学习了怎样搭建maven工程,这篇文章我就来教大家一起学习怎样用to ...
- CAD参数绘制填充(com接口)
填充是CAD图纸中不可或缺的对象,在机械设计行业,常常需要将零部件剖开,以表现其内部的细节,而这些被剖开的截面会用填充来表示:在工程设计行业,一些特殊的材料或地形,也会用填充来表示. C#中实现代码说 ...
- caffe实现自己的层
http://blog.csdn.net/xizero00/article/details/52529341 将这篇博客所讲进行了实现 1.LayerParameter也在caffe.proto文件中 ...
- 进程的互斥运行:CreateMutex函数实现只运行一个程序实例
HANDLE hMutex=CreateMutex(NULL,TRUE,"HDZBUkeyDoctorTool"); if(hMutex) { if(ERROR_ALREADY_E ...