Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1358    Accepted Submission(s): 435

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
Hint

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.

 
Source

题目链接:HDU 5521

题意:给定点数n和集合个数m,然后给你m个集合,每一个集合有si个点,两两之间的到达时间都是ti,一个人在1,一个人在n,求两人同时出发的相遇的最短时间

由于每一个集合的点有很多,若集合两两之间连边,边数非常大,一开始这样就超时了……然后正确做法是对每一个集合再虚拟一个节点(范围是[n+1,n+m]),给每一个集合内的点连边权为ti的双向边到本集合对应虚拟节点,集合内其他点不连边,这样就可以通过虚拟的节点来到达其他地方从而减少边数(方法真是太巧妙了),然后求相遇的最短时间,显然现在无法得知到底选哪个地点作为见面地点,那就对1跑一遍单源最短路,对n跑一边单源最短路,然后统计1~n中每一个点的可能最短时间(一个人早到一个人晚到,显然用max取时间长的那个数),然后选出1~n中的最短时间mndx,再遍历一下看哪些点的最短时间为mndx并记录输出,最后记得把最短时间除以2,因为连的边是ti,进入虚拟节点又出来会多算一次

点数为1e5,题目中说了所有集合大小之和不会超过1e6,每一个集合都有2*|Si|条边,那就是2*1e6条边因此N可设为1e5+10,M可设为2*1e6+10。


以上是以前的解法,昨天计蒜客被惨虐之后仔细看了一下D题发现其实跟这道题是同一个原理,这题是群内的点之间两两距离为ti,那不妨把Block点拆成入口和出口,然后这样连边:

<u, Block入口, 0>,<Block出口, u , 0>,<Block入口, Block出口, ti>

然后从S和T各跑一遍SPFA然后记录下Max更新答案即可,也不用像上面的解法一样除以2了,点数最差情况下一个点作为Block,应该是3e5,边数应该为2sum{Si}+m,应该是2e6+1e5

代码:

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 3e5 + 7;
const int M = 2e6 + 1e5 + 7;
struct edge
{
int to, nxt;
LL w;
edge() {}
edge(int _to, int _nxt, LL _w): to(_to), nxt(_nxt), w(_w) {}
};
edge E[M];
int head[N], tot;
int vis[N];
LL ds[N], de[N], Mindist[N]; void init()
{
CLR(head, -1);
tot = 0;
}
inline void add(int s, int t, LL d)
{
E[tot] = edge(t, head[s], d);
head[s] = tot++;
}
void spfa(int s, int flag, LL d[])
{
CLR(vis, 0);
if (flag)
CLR(ds, INF), ds[s] = 0;
else
CLR(de, INF), de[s] = 0;
queue<int>Q;
Q.push(s);
while (!Q.empty())
{
int u = Q.front();
Q.pop();
vis[u] = 0;
for (int i = head[u]; ~i; i = E[i].nxt)
{
int v = E[i].to;
if (d[v] > d[u] + E[i].w)
{
d[v] = d[u] + E[i].w;
if (!vis[v])
{
vis[v] = 1;
Q.push(v);
}
}
}
}
}
int main(void)
{
int tcase, n, m, si, u, i;
scanf("%d", &tcase);
for (int q = 1; q <= tcase; ++q)
{
init();
scanf("%d%d", &n, &m);
LL ti;
for (i = 1; i <= m; ++i)
{
scanf("%I64d%d", &ti, &si);
add(i, i + m, ti); //m
while (si--)
{
scanf("%d", &u);
add(u + (m << 1), i, 0LL); //si
add(i + m, u + (m << 1), 0LL); //si
}
}
spfa(1 + (m << 1), 1, ds);
spfa(n + (m << 1), 0, de);
LL ans = 0x3f3f3f3f3f3f3f3f;
printf("Case #%d: ", q);
vector<int>pos;
for (i = 2 * m + 1; i <= 2 * m + n; ++i)
{
Mindist[i] = max<LL>(ds[i], de[i]);
if (Mindist[i] < ans)
ans = Mindist[i];
}
if (ans == 0x3f3f3f3f3f3f3f3f)
puts("Evil John");
else
{
printf("%I64d\n", ans);
for (i = 2 * m + 1; i <= 2 * m + n; ++i)
if (Mindist[i] == ans)
pos.push_back(i - (m << 1));
int sz = pos.size();
for (i = 0; i < sz; ++i)
printf("%d%c", pos[i], " \n"[i == sz - 1]);
}
}
return 0;
}

HDU 5521 Meeting(虚拟节点+最短路)的更多相关文章

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

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

  2. hdu 5521 Meeting(最短路)

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

  3. HDU 5521 Meeting【最短路】

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

  4. HDU 5521 Meeting (最短路,dijstra)

    题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n,有M个点集,集合内的数表示任意两点的距离为dis ,现在问,如果两个人要见面, 需要最短距离是多少,有哪几个点能被当成见面点. 析:分别对 ...

  5. Ural 1741 Communication Fiend(隐式图+虚拟节点最短路)

    1741. Communication Fiend Time limit: 1.0 second Memory limit: 64 MB Kolya has returned from a summe ...

  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. PW试验-----verilog

    PWM,脉冲宽度调制.顾名思义,是通过调制脉冲的宽度,即占空比,来实现的.可是使占空比逐渐由最小增加到最大,也可以使占空比由最大减少到最小来实现不同的现象.若用LED灯来显示现象,则可以称作:LED呼 ...

  2. 现在不能使用foxmail同步qq记事本功能,可能是对字数的大小有限制

    那么在经过了两个星期的时间完成的主要功能就是幻灯片和站点管理,在之后还有更多的任务要做,本来从时间的安排上不太合理,在这个月底要基本完成,主要是其他的组员也有比较重的任务,那么就是需要随时有一个状态, ...

  3. 添加Java文档注释

    一.在Eclipse中add javadoc comment的快捷键为: 快捷键为:ALT + SHIFT +J 二.Window-->Preferences-->General--> ...

  4. php中base64_decode与base64_encode加密解密函数

    php中base64_decode与base64_encode加密解密函数,实例分析了base64加密解密函数的具体用法,具有一定的实用价值,需要的朋友可以参考下 本文实例讲述了php中base64_ ...

  5. 启动mysql出现了error the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)

    原来是我的mysql日志太多,所以去/data/log/mysql目录(这个目录是从/etc/my.cnf中的log-error确定的)下删除了 rm -rf mysql_binary_log.*的日 ...

  6. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. Python实践:模块自动重载

    一.概述 二.思路 三.实现 四.测试 1.开启自动重载(终端1) 2.修改模块(终端2) 3.查看实时输出(终端1) 五.参考源码 一.概述 开发Web程序时,通常会采用本地服务器进行调试,但如果代 ...

  8. 简单几何(线段相交) POJ 1410 Intersection

    题目传送门 题意:一个矩形和一条线段,问是否有相交 分析:考虑各种情况.坑点:给出的矩形的两个端点是无序的,还有线段完全在矩形内也算相交 /****************************** ...

  9. Noi2011 : 智能车比赛

    假设S在T左边,那么只能往右或者上下走 f[i]表示S到i点的最短路 f[i]=min(f[j]+dis(i,j)(i能看到j)) 判断i能看到j就维护一个上凸壳和一个下凸壳 时间复杂度$O(n^2) ...

  10. CentOS6.4 安装LVS-RRD监控LVS

    1.安装依赖包 yum install -y php httpd bc rrdtool 启动apache (我看网上的一些文档说不能用80端口,但我用80端口试了一下也好使,如果出现不好使的情况就改一 ...