HDOJ5521(巧妙构建完全图)
Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2415 Accepted Submission(s): 765
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.
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.
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.
3 4
#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(巧妙构建完全图)的更多相关文章
- 斯坦福第十一课:机器学习系统的设计(Machine Learning System Design)
11.1 首先要做什么 11.2 误差分析 11.3 类偏斜的误差度量 11.4 查全率和查准率之间的权衡 11.5 机器学习的数据 11.1 首先要做什么 在接下来的视频中,我将谈到机器 ...
- 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 ...
- UVALive 4872 Underground Cables 最小生成树
题目链接: 题目 Underground Cables Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %ll ...
- 技巧:Linux 动态库与静态库制作及使用详解
技巧:Linux 动态库与静态库制作及使用详解 标准库的三种连接方式及静态库制作与使用方法 Linux 应用开发通常要考虑三个问题,即:1)在 Linux 应用程序开发过程中遇到过标准库链接在不同 L ...
- 【转载】NeurIPS 2018 | 腾讯AI Lab详解3大热点:模型压缩、机器学习及最优化算法
原文:NeurIPS 2018 | 腾讯AI Lab详解3大热点:模型压缩.机器学习及最优化算法 导读 AI领域顶会NeurIPS正在加拿大蒙特利尔举办.本文针对实验室关注的几个研究热点,模型压缩.自 ...
- Ng第十一课:机器学习系统的设计(Machine Learning System Design)
11.1 首先要做什么 11.2 误差分析 11.3 类偏斜的误差度量 11.4 查全率和查准率之间的权衡 11.5 机器学习的数据 11.1 首先要做什么 在接下来的视频将谈到机器学习系 ...
- 吴恩达-coursera-机器学习-week6
十.应用机器学习的建议(Advice for Applying Machine Learning) 10.1 决定下一步做什么 10.2 评估一个假设 10.3 模型选择和交叉验证集 10.4 诊断偏 ...
- C基础 工程中常用的排序
引言 - 从最简单的插入排序开始 很久很久以前, 也许都曾学过那些常用的排序算法. 那时候觉得计算机算法还是有点像数学. 可是脑海里常思考同类问题, 那有什么用呢(屌丝实践派对装逼学院派的深情鄙视). ...
- [CDH] Redis: Remote Dictionary Server
基本概念 一.安装 Redis: Remote Dictionary Server 远程字典服务 使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种 ...
随机推荐
- SQL SERVER 导入、导出数据到Exce(使用OpenRowset,、OpenDataSource函数)以及访问远程数据库(openrowset/opendatasource/openquery)
启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务不安 ...
- C++面向对象高级编程(八)模板
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...
- c# 实体处理工具类
using System; using System.Collections; using System.Collections.Generic; using System.ComponentMode ...
- flowable FormEngine和FormEngineConfiguration
FormEngineConfiguration 继承自 AbstractEngineConfiguration. 一.获得实例 FormEngineConfiguration提供了7个公开的静态方法: ...
- 查看PHP以字母"E"开头的常量
1.E_ALL <?php echo E_ALL; ?> 32767 2.E_COMPILE_ERROR <?php echo E_COMPILE_ERROR; ?> 64 3 ...
- iOS开发之解决CocoaPods中“.h”头文件找不到的问题,简单粗暴的方法
如果是拖进工程中的framework或者第三方文件,如果找不到,删除了重新添加或者修改search path地址,如果不知道怎么修改,在工程文件夹中,找到对应的文件,然后将文件拖到修改文件地址的位置, ...
- Android内存优化(三)避免可控的内存泄漏
相关文章 Android性能优化系列 Java虚拟机系列 前言 内存泄漏向来都是内存优化的重点,它如同幽灵一般存于我们的应用当中,有时它不会现身,但一旦现身就会让你头疼不已.因此,如何避免.发现和解决 ...
- 前端构建工具-fis3使用入门
FIS3 是面向前端的工程构建工具.解决前端工程中性能优化.资源加载(异步.同步.按需.预加载.依赖管理.合并.内嵌).模块化开发.自动化工具.开发规范.代码部署等问题. 官网地址是: https:/ ...
- Openstack认证过程
01.登陆界面或命令行通过RESTful API向Keystone获取认证信息: 02.Keystone通过用户请求认证信息,并生成auth-token返回给对应的认证请求: 03.界面或命令行通过R ...
- 01-名字管理系统.py
#-*- coding:utf-8 -*- #1.打印功能提示 print("-"*50) print("名字管理系统 V8.6") print("1 ...