Meeting

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

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
 
思路:任意取一个block举例建图。设block中有n个节点,,有题意可知,block是一个无向完全图。若按传统的方法建图,则无向完全图中有n个节点, n*(n-1)条边。我们换一种方式建图,在block中增加两个节点u,v。再加入一条有向边(u,v),然后将block中的所有节点xi,加入两条有向边(xi,u)、(v,xi)。最终得到的是一幅有n+2个节点2*n+1个有向边的有向图,其等价于按传统方法构建的完全图。
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int MAXN = ;
const int INF = 0x3f3f3f3f;
struct Edge{
int to, w, net;
}es[];
int head[MAXN], tot;
int n, m;
void addedge(int u, int v, int w)
{
es[tot].to = v;
es[tot].w = w;
es[tot].net = head[u];
head[u] = tot++;
}
void spfa(int src, int d[], bool vis[], int n)
{
for(int i = ; i <= n; i++)
{
d[i] = INF;
vis[i] =false;
}
d[src] = ;
queue<int> que;
que.push(src);
while(!que.empty())
{
int u = que.front(); que.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = es[i].net)
{
Edge e = es[i];
if(d[e.to] > d[u] + e.w)
{
d[e.to] = d[u] + e.w;
if(!vis[e.to])
{
vis[e.to] = true;
que.push(e.to);
}
}
}
}
}
int d[][MAXN];
bool vis[MAXN];
int vec[MAXN], len;
int main()
{
int T;
scanf("%d", &T);
for(int cas = ; cas <= T; cas++)
{
memset(head, -, sizeof(head));
tot = ;
scanf("%d %d", &n ,&m);
int newN = n;
for(int i = ; i < m; i++)
{
int w, s;
scanf("%d %d", &w, &s);
//巧妙构图
int u = ++newN;
int v = ++newN;
addedge(u, v, w);
for(int j = ; j < s; j++)
{
int x;
scanf("%d", &x);
addedge(x, u, );
addedge(v, x, );
}
} spfa(, d[], vis, newN);
spfa(n, d[], vis, newN); int mn = INF;
for(int i = ; i <= n; i++)
{
int mx = max(d[][i], d[][i]);
if(mn > mx)
{
mn = mx;
}
}
if(mn == INF)
{
printf("Case #%d: Evil John\n", cas);
continue;
}
len = ;
for(int i = ; i <= n; i++)
{
if(max(d[][i], d[][i]) == mn)
{
vec[len++] = i;
}
}
printf("Case #%d: %d\n", cas, mn);
for(int i = ; i < len -; i++)
{
printf("%d ", vec[i]);
}
printf("%d\n", vec[len-]);
}
return ;
}

HDOJ5521(巧妙构建完全图)的更多相关文章

  1. 斯坦福第十一课:机器学习系统的设计(Machine Learning System Design)

    11.1  首先要做什么 11.2  误差分析 11.3  类偏斜的误差度量 11.4  查全率和查准率之间的权衡 11.5  机器学习的数据 11.1  首先要做什么 在接下来的视频中,我将谈到机器 ...

  2. Machine Learning - 第6周(Advice for Applying Machine Learning、Machine Learning System Design)

    In Week 6, you will be learning about systematically improving your learning algorithm. The videos f ...

  3. UVALive 4872 Underground Cables 最小生成树

    题目链接: 题目 Underground Cables Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %ll ...

  4. 技巧:Linux 动态库与静态库制作及使用详解

    技巧:Linux 动态库与静态库制作及使用详解 标准库的三种连接方式及静态库制作与使用方法 Linux 应用开发通常要考虑三个问题,即:1)在 Linux 应用程序开发过程中遇到过标准库链接在不同 L ...

  5. 【转载】NeurIPS 2018 | 腾讯AI Lab详解3大热点:模型压缩、机器学习及最优化算法

    原文:NeurIPS 2018 | 腾讯AI Lab详解3大热点:模型压缩.机器学习及最优化算法 导读 AI领域顶会NeurIPS正在加拿大蒙特利尔举办.本文针对实验室关注的几个研究热点,模型压缩.自 ...

  6. Ng第十一课:机器学习系统的设计(Machine Learning System Design)

    11.1  首先要做什么 11.2  误差分析 11.3  类偏斜的误差度量 11.4  查全率和查准率之间的权衡 11.5  机器学习的数据 11.1  首先要做什么 在接下来的视频将谈到机器学习系 ...

  7. 吴恩达-coursera-机器学习-week6

    十.应用机器学习的建议(Advice for Applying Machine Learning) 10.1 决定下一步做什么 10.2 评估一个假设 10.3 模型选择和交叉验证集 10.4 诊断偏 ...

  8. C基础 工程中常用的排序

    引言 - 从最简单的插入排序开始 很久很久以前, 也许都曾学过那些常用的排序算法. 那时候觉得计算机算法还是有点像数学. 可是脑海里常思考同类问题, 那有什么用呢(屌丝实践派对装逼学院派的深情鄙视). ...

  9. [CDH] Redis: Remote Dictionary Server

    基本概念 一.安装 Redis: Remote Dictionary Server 远程字典服务 使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种 ...

随机推荐

  1. Java进阶4表达式中的陷阱

    Java进阶4表达式中的陷阱 20131103 表达式是Java中最基本的组成单元,各种表达式是Java程序员最司空见惯的内容,Java中的表达式并不是十分的复杂,但是也有一些陷阱.例如当程序中使用算 ...

  2. 设置tableview 右侧 索引字体的大小

  3. Falsk项目cookie中的 csrf_token 和表单中的 csrf_token实现

    Flask中请求体的请求开启CSRF保护可以按以下配置 from flask_wtf.csrf import CSRFProtect app.config.from_object(Config) CS ...

  4. .net 枚举(Enum)使用总结

    在实际问题中,有些变量的取值被限定在一个有限的范围内.例如,一个星期内只有七天,一年只有十二个月,性别只有男跟女等等.如果把这些量说明为整型.字符型或其它类型显然是不妥当的.为此,C#提供了一种称为“ ...

  5. PostgreSQL基于时间点故障恢复PITR( point-in-time recovery )

    PostgreSQL在使用过程中经常会发生一些失误的操作,但往往是可以弥补的.但是如果真遇到了无法挽回的误操作,只能寄希望于有备份了. 接下来的故障恢复也是基于有备份的情况,没有备份的情况,目前还没有 ...

  6. Android敏捷开发、CI(持续集成)探究

    比较老的几篇文章,依旧有学习价值 http://blog.csdn.net/baodinglaolang/article/details/9530695 http://blog.csdn.net/ba ...

  7. C# Thrift 实战开发 从PLC到Thrift再到客户端集成开发

    About Thrift: 本文并不是说明Thrift设计及原理的,直接拿Thrift来开发一个Demo程序,如果想要了解Thrift的细节,可以访问官方网站:https://thrift.apach ...

  8. IOS开发 基础框架(Fondation Framework)的线程安全

    有一种误解,认为基础框架(Foundation framework)是线程安全的,而Application Kit是非线程安全的.不幸的是,这是一个总的概括,从而造成一点误导.每个框架都包含了线程安全 ...

  9. SAPUI5使用了哪些开源技术

    我们知道SAP UI5已经开源了,共享给了Apache开源组织后的名字叫Open UI5,虽然从API的长度上看,Open UI5比SAP UI5要短,但是两者的核心并没有多大区别,SAP UI5多了 ...

  10. 【剑指offer】06从尾到头打印链表,C++实现

    本文是原创文章,转载请注明出处! 0.前言 # 本文为牛客网<剑指offer>刷题笔记 1.题目 # 输入一个链表,从尾到头打印链表每个节点的值 2.思路 # 不改变链表结构的情况下,首先 ...