HDU 5521:Meeting(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=5521
Meeting
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.
The second line contains the numbers of blocks where they meet. If there are multiple
optional blocks, output all of them in ascending order.
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.
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL;
#define N 1000005
#define M 2000005
const LL inf = 1e17;
/*
最短路
*/
struct node
{
int v, nxt;
LL w;
}edge[M]; struct nd
{
int p;
LL d;
nd(){}
nd(LL _d, int _to):d(_d), p(_to) {}
bool operator < (const nd &a) const {
return d > a.d;
}
}; int tot, n, m, nn, used[N], head[N];
LL da[N], db[N]; void add(int u, int v, LL w)
{
edge[tot].v = v;
edge[tot].w = w;
edge[tot].nxt = head[u];
head[u] = tot++;
} void Dijkstra(int s, LL d[])
{
priority_queue<nd> que;
while(!que.empty()) que.pop();
for(int i = ; i <= nn; i++) d[i] = inf;
memset(used, , sizeof(used));
d[s] = ;
que.push(nd(d[s], s));
while(!que.empty()) {
nd top = que.top(); que.pop();
int u = top.p;
if(used[u]) continue;
used[u] = ;
for(int k = head[u]; ~k; k = edge[k].nxt) {
int v = edge[k].v;
int w = edge[k].w;
if(d[u]+w < d[v]) {
d[v] = d[u] + w;
que.push(nd(d[v], v));
}
}
}
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
scanf("%d%d", &n, &m);
memset(head, -, sizeof(head));
tot = ;
nn = n;
for(int i = ; i < m; i++) {
LL dis;
int num, a;
//就是这里了,在外面建一个点,然后集合里面所有点都和它相连
nn++;
scanf("%I64d%d", &dis, &num);
for(int j = ; j < num; j++) {
scanf("%d", &a);
add(a, nn, dis);
add(nn, a, dis);
}
} Dijkstra(, da);
Dijkstra(n, db);
LL ans = inf; for(int i = ; i <= n; i++)
ans = min(ans, max(da[i], db[i])); printf("Case #%d: ", cas);
if(ans==inf) printf("Evil John\n");
else{
//因为求出的距离是两倍,所以要除以二
printf("%I64d\n", ans/);
bool f = ;
for(int i = ; i <= n; i++) {
if(max(da[i], db[i]) == ans){
if(f) printf(" ");
f = ;
printf("%d", i);
}
}
puts("");
}
}
return ;
}
HDU 5521:Meeting(最短路)的更多相关文章
- HDU 5521.Meeting 最短路模板题
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- HDU 5521 Meeting(虚拟节点+最短路)
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- hdu 5521 Meeting(最短路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...
- HDU 5521 Meeting【最短路】
今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...
- HDU 5521 Meeting (最短路,dijstra)
题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n,有M个点集,集合内的数表示任意两点的距离为dis ,现在问,如果两个人要见面, 需要最短距离是多少,有哪几个点能被当成见面点. 析:分别对 ...
- HDU 5521 [图论][最短路][建图灵感]
/* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...
- HDU 5521 Meeting
2015 ACM / ICPC 沈阳站现场赛 M题 最短路 设置N+M个节点,前N个节点是Block,后M个节点是Set,每一组Set中的点向该Set连边,从1和n开始分别求最短路.注意爆int. # ...
- HDU - 5521 Meeting (Dijkstra)
思路: 看了好久才看懂题意,文中给了n个点,有m个集合,每个集合有s个点,集合内的每两个点之间有一个权值为t的边,现在有两个人,要从1号点,和n号点,走到同一个顶点,问最少花费以及花费最少的点. 那就 ...
- hdu 5521 最短路
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- swagger ui 值类型形参加文字注释
例: @ApiImplicitParams({ @ApiImplicitParam(paramType = "query", name = "indexCondition ...
- 基于Linux C的socketEthereal程序和Package分析 (一个)
执行测试平台:CentOS 6.5发行版,内核版本号3.11 1. Linux抓包源程序 在OSI七层模型中.网卡工作在物理层和数据链路层的MAC子层. 进行网络通信时.源主机通过socket( ...
- 1-9 RHEL7-文件权限管理
本节所讲内容: 文件的基本权限:r w x (UGO+ACL) 文件的高级权限:suid sgid sticky 第1章 文件的基本权限 1.1 权限的作用 通过对文件设定权限可以达到以下三种访问限制 ...
- Angularjs html文本显示
<body ng-app="siteApp"> <div ng-controller="newsDetailController as vm" ...
- SignalR QuickStart
原文:SignalR QuickStart SignalR 是一个集成的客户端与服务器库,基于浏览器的客户端和基于 ASP.NET 的服务器组件可以借助它来进行双向多步对话. 换句话说,该对话可不受限 ...
- 微信小程序把玩(二)window配置
原文:微信小程序把玩(二)window配置 window用于设置小程序的状态栏.导航条.标题.窗口背景色.注意在app.json中配置的属性会被子window属性覆盖 只需在app.json配置即可
- UWP-电子音读出文字
原文:UWP-电子音读出文字 源码: https://github.com/lindexi/Markdown 代码 private async void speech(string str, Medi ...
- Delphi中无边框窗体应用程序使任务栏右键菜单有效的方法
最近在Delphi开发中用到了无边框窗体显示时,无法在任务栏使用右键弹出菜单的情况,经过整理,通过以下方法可以使右键菜单出现: procedure Tfrm_Base.InitSysMenu;var ...
- 基于ASP.NET的新闻管理系统(一)
1. 项目简介 1.1设计内容 (1)可以在首页查看各类新闻,可以点击新闻查看具体内容:可以查看不同类型的新闻,并了解热点新闻,可以在搜索框里输入要查找的内容. (2)在后台界面中,管理员可以修改密码 ...
- SYN4201型 同步分频钟
SYN4201型 同步分频钟 产品概述 SYN4201型同步分频钟是由西安同步电子科技有限公司精心设计.自行研发生产的一款高精度分频时钟,对输入的8路10MHz正弦信号分别进行同步分频处理,相应的输出 ...