【HDU5521 2015沈阳赛区M】【拆点最短路 dijkstra+heap】Meeting 集合内距离相同


Meeting

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


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 (1im)  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 (1T6) , the number of test cases. Then   T  test cases
follow.

The first line of input contains   n  and   m .   2n105 . The following   m  lines describe the sets   Ei (1im) . Each line will contain two integers   ti(1ti109)  and   Si (Si>0)  firstly. Then   Si  integer follows which are the labels of blocks in   Ei . It is guaranteed that   mi=1Si106 .
 

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
 

 
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T> inline void gmax(T &a,T b){if(b>a)a=b;}
template <class T> inline void gmin(T &a,T b){if(b<a)a=b;}
const int N=1e5+2e6+10,M=3e6+10,Z=1e9+7,ms63=1061109567;
int casenum,casei;
int id;
int first[N],w[M],c[M],nxt[M];
LL f[N],F[N];
bool e[N];
struct node
{
	int x;LL v;
	node(){}
	node(int x_,LL v_){x=x_;v=v_;}
	bool operator < (const node& b)const{return v>b.v;}
};
void ins(int x,int y,int z)
{
	id++;
	w[id]=y;
	c[id]=z;
	nxt[id]=first[x];
	first[x]=id;
}
priority_queue<node>q;
void inq(int x,LL dis)
{
	if(dis>=f[x])return;
	f[x]=dis;
	q.push(node(x,dis));
}
void dijkstra(int st)
{
	MS(f,63);
	MS(e,0);
	inq(st,0);
	q.push(node(st,0));
	while(!q.empty())
	{
		int x=q.top().x;q.pop();
		if(e[x])continue;e[x]=1;
		for(int z=first[x];z;z=nxt[z])inq(w[z],f[x]+c[z]);
	}
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;casei++)
	{
		MS(first,0);id=1;
		int n,m;
		scanf("%d%d",&n,&m);
		for(int i=1;i<=m;i++)
		{
			int dis,g,x;
			scanf("%d%d",&dis,&g);
			int in=n+i;
			int out=n+m+i;
			ins(in,out,dis);
			while(g--)
			{
				scanf("%d",&x);
				ins(x,in,0);
				ins(out,x,0);
			}
		}
		dijkstra(1);
		MC(F,f);
		dijkstra(n);
		LL ans=1e18;
		int ed;
		for(int i=1;i<=n;i++)
		{
			gmax(F[i],f[i]);
			if(F[i]<=ans)
			{
				ans=F[i];
				ed=i;
			}
		}
		if(ans==1e18)printf("Case #%d: Evil John\n",casei);
		else
		{
			printf("Case #%d: %lld\n",casei,ans);
			for(int i=1;i<ed;i++)if(F[i]==ans)printf("%d ",i);
			printf("%d\n",ed);
		}
	}
	return 0;
}
/*
【trick&&吐槽】
点数N=n+2*最大集合数=1e5+2e6
边数M=3*最大集合数=3e6

【题意】
两个人A,B想要见面。
有n([2,1e5])个点,A初始在1点,B初始在n点。
有m个集合关系,第i个集合有Si个点,这些点两两之间移动所花费的时间都为dis([1,1e9]),有∑Si<=1e6。
让你输出A和B在哪些点碰面,使得他们能在最早时间相遇。
输出这个最早相遇时间以及所有满足的点。

【类型】
最短路

【分析】
这题很显然是一个最短路模型。然而如果暴力建边,边数可达1e12条,爆炸。
问题是怎么处理集合关系。比较好想,我读完题的瞬间就想到了做法——拆点。
对每个集合构造两个点,入点和出点,之间连一条边权为dis的边,
把集合内的每个点向这个集合的入点连边,边权为0,
把集合的出点向这个集合内的每个点连边,边权为0。
这样就实现了,对于每个集合,利用2Si+1条边,改变其内任意两个点的边权都为dis。
然后分别以1和n为起点,跑最短路,然后扫描一下即可。

【时间复杂度&&优化】
O(mlogm)

【数据】
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

output
Case #1: 3
3 4
Case #2: Evil John

*/


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值