题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n,有M个点集,集合内的数表示任意两点的距离为dis ,现在问,如果两个人要见面,

需要最短距离是多少,有哪几个点能被当成见面点。

析:分别对1和n进行最短路操作,这个题最让人别扭的就是边太多,如果你直接全部都存下来,那么一定会MLE,所以一定要优化边。所以我们要把每一行的边都记下来,

而不是两两都记下,然后把每一行的编号记下来,最后要查询时,就行编号去定位到哪一行,这样就不会超内存。这里会了,其他的就很简单了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){ return a < b ? b : a; }
inline LL Min(LL a, LL b){ return a > b ? b : a; }
vector<int> G[maxn];
vector<int> b[maxn];
LL d1[maxn];
LL d2[maxn];
int t[maxn];
bool vis[maxn]; void dijstra(int s, LL* d){
priority_queue<P, vector<P>, greater<P> > pq;
pq.push(P(0, s));
memset(vis, false, sizeof vis);
fill(d, d+n+1, LNF);
d[s] = 0; while(!pq.empty()){
P p = pq.top(); pq.pop();
int u = p.second;
if(d[u] < p.first) continue;
for(int i = 0; i < b[u].size(); ++i){//从编号找出在哪行
int v = b[u][i];
if(vis[v]) continue;
vis[v] = true;
for(int j = 0; j < G[v].size(); ++j){
int uv = G[v][j], w = t[v];
if(d[uv] > d[u] + w){
d[uv] = d[u] + w;
pq.push(P(d[uv], uv));
}
}
}
}
} int main(){
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; ++i) G[i].clear(), b[i].clear();
for(int i = 1; i <= m; ++i){
int k;
scanf("%d %d", &t[i], &k);
for(int j = 0; j < k; ++j){
int v;
scanf("%d", &v);
G[i].push_back(v);//存边
b[v].push_back(i);//记下行号
}
} dijstra(1, d1);
dijstra(n, d2);
printf("Case #%d: ", kase);
LL ans = LNF;
for(int i = 1; i <= n; ++i)
ans = Min(ans, Max(d1[i], d2[i]));
if(ans == LNF) puts("Evil John");
else{
printf("%I64d\n", ans);
int cnt = 0;
for(int i = 1; i <= n; ++i)
if(ans == Max(d1[i], d2[i])){
if(cnt) putchar(' ');
printf("%d", i);
++cnt;
}
printf("\n");
}
}
return 0;
}

  

HDU 5521 Meeting (最短路,dijstra)的更多相关文章

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

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

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

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

  3. hdu 5521 Meeting(最短路)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 题意:有1-n共n个点,给出m个块(完全图),并知道块内各点之间互相到达花费时间均为ti.已知两 ...

  4. HDU 5521 Meeting【最短路】

    今天旁观了Angry_Newbie的模拟区域赛(2015shenyang) 倒着看最先看的M题,很明显的最短路问题,在我看懂的时候他们已经开始敲B了. 后来听说D过了很多人.. D题一看是个博弈,给了 ...

  5. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  6. HDU 5521 Meeting

    2015 ACM / ICPC 沈阳站现场赛 M题 最短路 设置N+M个节点,前N个节点是Block,后M个节点是Set,每一组Set中的点向该Set连边,从1和n开始分别求最短路.注意爆int. # ...

  7. HDU - 5521 Meeting (Dijkstra)

    思路: 看了好久才看懂题意,文中给了n个点,有m个集合,每个集合有s个点,集合内的每两个点之间有一个权值为t的边,现在有两个人,要从1号点,和n号点,走到同一个顶点,问最少花费以及花费最少的点. 那就 ...

  8. HDU 5521:Meeting(最短路)

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

  9. hdu 5521 最短路

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

随机推荐

  1. OK335xS psplash 进度条工作原理 hacking

    #!/bin/sh # # rc This file is responsible for starting/stopping # services when the runlevel changes ...

  2. Java [Leetcode 238]Product of Array Except Self

    题目描述: Given an array of n integers where n > 1, nums, return an array output such that output[i]  ...

  3. Mysql读写分离(mysql-proxy)

    MySQL-Proxy是一个处于你的client端和MySQL server端之间的简单程序,它可以监测.分析或改变它们的通信.它使用灵活,没有限制,常见的用途包括:负载平衡,故障.查询分析,查询过滤 ...

  4. Oracle行列互换 横表和纵表

    /* 在实际使用sql工作中总会碰到将某一列的值放到标题中显示.就是总说的行列转换或者互换. 比如有如下数据: ID NAME KECHENG CHENGJI -- ---------- ------ ...

  5. C# 使用C/S模式操作小票机打印

    此方式适用于市场上大多数的小票机 佳博.POS58 等,不适用于有些标签打印机 比如斑马打印机等 直接贴代码: private FileStream fs = null; [DllImport(&qu ...

  6. site

    http://blog.csdn.net/zgmzyr/article/details/7657126

  7. java web 学习十四(JSP原理)

    一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...

  8. 白盒测试之gtest第一个demo

    认识gtest工具后,关于它的使用,下面将用一个demo程序演示一下gtest的用法以及成果展示. 一.需要测试的C++代码: #include "myfunction.h" // ...

  9. CSS计算样式的获取

    一般来说我们获取CSS的样式的时候会优先采用Elment.style.cssName 这种方法,这种方法类似于对象设置get,set属性获取,例如Elment.style.cssName是获取,Elm ...

  10. Python 学习笔记(四)正则、闭合、生成器

    (一)正则表达式 基本规则: ^ 匹配字符串开始位置. $ 匹配字符串结束位置. \b 匹配一个单词边界. \d 匹配一个数字. \D 匹配一个任意的非数字字符. x? 匹配可选的x字符.换句话说,就 ...