题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521

给n个点,m个块。块内点到点之间话费的时间ti。两个人分别从点1和点n出发,问两人是否可以相遇,并求出相遇最短时间和路径,路径按照字典序输出。

这题的难点在于处理块内的点到块外点的关系。我们可以添加一个“集合点”,此点到集合内各点距离为w,点到此集合的距离为0建图。

从1和n分别做一次最短路,找到每一个距离最长的点(即为相遇点),记下长度再枚举两个结果,看一共多少个相遇点

 #include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; typedef long long ll;
typedef pair<int, int> pii;
typedef struct E {
int w;
int v;
E() {}
E(int vv, int ww) : v(vv), w(ww) {}
}E; const int inf = 0x7f7f7f7f;
const int maxn = ; priority_queue<pii, vector<pii>, greater<pii> > pq;
vector<E> e[maxn];
vector<int> path;
ll d[][maxn];
int n, m, u, v, w, k; template<int cho>
void dijkstra(int s) {
memset(d[cho], inf, sizeof(d[cho]));
while(!pq.empty()) pq.pop();
d[cho][s] = ;
pq.push(pii(, s));
while(!pq.empty()) {
pii cur = pq.top(); pq.pop();
w = cur.first;
v = cur.second;
if(d[cho][v] < w) continue;
for(int i = ; i < e[v].size(); i++) {
if(d[cho][e[v][i].v] > d[cho][v] + e[v][i].w) {
d[cho][e[v][i].v] = d[cho][v] + e[v][i].w;
pq.push(pii(d[cho][e[v][i].v], e[v][i].v));
}
}
}
} inline bool scan_d(int &num) {
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
} int main() {
// freopen("in", "r", stdin);
int T, _ = ;
scan_d(T);
while(T--) {
for(int i = ; i < maxn; i++) e[i].clear();
path.clear();
scan_d(n); scan_d(m);
for(int i = ; i <= m; i++) {
scan_d(w); scan_d(k);
while(k--) {
scanf("%d", &v);
e[n+i].push_back(E(v, w));
e[v].push_back(E(n+i, ));
}
}
dijkstra<>(); dijkstra<>(n);
ll cur = inf;
for(int i = ; i <= n; i++) {
cur = min(cur, max(d[][i], d[][i]));
}
for(int i = ; i <= n; i++) {
if(cur == max(d[][i], d[][i])) {
path.push_back(i);
}
}
printf("Case #%d: ", _++);
if(cur == inf) {
printf("Evil John\n");
continue;
}
printf("%I64d\n", cur);
for(int i = ; i < path.size(); i++) {
printf("%d", path[i]);
if(i == path.size() - ) printf("\n");
else printf(" ");
}
}
return ;
}

[HDOJ5521]Meeting(最短路)的更多相关文章

  1. hdu-5521 Meeting(最短路)

    题目链接: Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  2. HDU 5521.Meeting 最短路模板题

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  3. 2015沈阳区域赛Meeting(最短路 + 建图)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

  4. 2015沈阳站-Meeting 最短路

    http://acm.hdu.edu.cn/showproblem.php?pid=5521 题目大意:A,B两个人分别在1和n区.给出区之间有联系的图以及到达所需时间.求两个人见面最短时间以及在哪个 ...

  5. [hdu5521 Meeting]最短路

    题意:有N个点,给定M个集合,集合Si里面的点两两之间的距离都为Ti,集合里面的所有点数之和<=1e6.有两个人分别在1和N处,求1个点使得两个人到这一点距离的最大值最小 思路:这题是裸的最短路 ...

  6. 【HDOJ5521】Meeting(最短路)

    题意:有n个点,m个点集,每个点集中有e[i]个点,同一点集的点互相之间到达需要t[i]单位的时间,求min(max(dis(1,i),dis(i,n))),i属于[1,n] 输出最小值并増序输出所有 ...

  7. HDU 5521 Meeting(虚拟节点+最短路)

    Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total ...

  8. URAL 1085 Meeting(最短路)

    Meeting Time limit: 2.0 secondMemory limit: 64 MB K friends has decided to meet in order to celebrat ...

  9. HDU 5521:Meeting(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=5521 Meeting Problem Description   Bessie and her friend E ...

随机推荐

  1. 【BZOJ】【1901】【Zju2112】 Dynamic Rankings

    再填个坑. 动态维护区间第K大(带单点修改) 首先裸的区间第K大我们是用的[前缀和]思想,实现O(n)预处理,O(1)找树查询,那么如果是动态的呢?我们可以利用树状数组(BIT)的思想,进行O(log ...

  2. 2012 Asia Chengdu Regional Contest

    Browsing History http://acm.hdu.edu.cn/showproblem.php?pid=4464 签到 #include<cstdio> #include&l ...

  3. mvc从xheditor编辑器中获取内容时存在潜在危险

    xmfdsh在使用xheditor提交要发布的文章等内容的时候出现了如下的错误: 从客户端(Content="<p style="text-align...")中检 ...

  4. asp.net中实现群发邮件功能

    前段时间在帮老师开发的网站中需要用到一个群发邮件的功能,而自己之前学习cms系统的时候用的we7的群发邮件功能也有一些问题,于是乎便自己去网上查了一下资料,自己总结了一下,并且封装成了一个类,亲测有用 ...

  5. html之cellspacing && cellpadding讲解

    单元格间距(表格间距)(cellspacing) -- 代表表格边框与单元格补白的距离,也是单元格补白之间的距离 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于 ...

  6. Struct2、Hibernate3、Spring3框架搭建实战(转)

    采用目前最新的struts-2.3.1.2.hibernate3.6.10.Final.spring-framework-3.1.1.RELEASE开发包,以及eclipse-jee-indigo-S ...

  7. 寒假222_Topcoder SRM648

    序号1: A,随便模拟好了,最后30秒发现一个都比的错误,无奈输错格式. 2: B,问你出去两个点,以及所产生的边 问你:产生多的联通快 加答案加1. 数据小,随便写暴力 3: copy思路. 我们先 ...

  8. LoaderManager使用详解(一)---没有Loader之前的世界

    来源: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html 感谢作者Alex ...

  9. HDU 2709 Sumsets(递推)

    Sumsets http://acm.hdu.edu.cn/showproblem.php?pid=2709 Problem Description Farmer John commanded his ...

  10. editplus bat语法高亮

    editplus bat语法高亮 今天需要在Windows上写批处理,因为没写过,避免关键字错误,就需要语法高亮了,editplus默认没有bat语法文件,赶紧解决. 1:到 http://www.e ...