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. URAL 1557 Network Attack 图论,连通性,tarjain,dfs建树,分类讨论 难度:2

    http://acm.timus.ru/problem.aspx?space=1&num=1557 1557. Network Attack Time limit: 2.0 secondMem ...

  2. qt QTableWidget&&QTableView 导出数据到excel

    通常情况下,我们在开发过程中比较常用的方法是将表格的数据到处到excel文件.我也在这个点上头疼了很长时间,不过功夫不负苦心人,最终还是勉强达到效果,为了后面再次用到时不手忙脚乱现在将方法寄存在此,如 ...

  3. 那如何判断页面是在移动端还是PC端打开的

    网上有很多方法,写的或难或简单,实际上一行代码就够了. window.location.href = /Android|webOS|iPhone|iPod|BlackBerry/i.test(navi ...

  4. eureka-6-为Eureka Server 及Dashboard 添加用户认证

    Eureka Server 默认是允许匿名访问的你,当然也可以加认证权限 添加步骤: 1:在pom.xml文件中添加spring-boot-start-starter-security 的依赖.该依赖 ...

  5. iOS使用Instruments的工具

    使用Instruments的工具 iOSXcodeInstrumentsInstruments是一个官方提供的强大的性能调试工具集. 1.Blank(空模板):创建一个空的模板,可以从Library库 ...

  6. Javascript-- jQuery动画篇(2)

    动画效果 前面的 hide/show,slide in/out 其实也具有动画效果,本篇介绍使用 animate()实现自定义动画效果. 基本语法如下: $(selector).animate({pa ...

  7. cscope使用记录

    在看c的源码过程中,仅仅使用ctags不够用,加入cscope会好一点,关于vim的配置就不多说了,在这里主要是记录常用的几个东西: 在代码的最顶层执行: cscope -Rbkq 打开vim: cs ...

  8. DataTable和实体类通过反射相互转换

    using System.Runtime.Serialization; using System.Data; using System.Reflection; using System.Collect ...

  9. 在servlet中的中文乱码,相对路径和绝对路径

    默认情况下在servlet中的中文是显示不出来的,解决问题就是加resp.setContentType("text/html;charset=gbk"); 而且这句加的话必须写在P ...

  10. (转)JAVA正则表达式语法大全

    [正则表达式]文本框输入内容控制 整数或者小数:^[0-9]+\.{0,1}[0-9]{0,2}$ 只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n ...