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.
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 .
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.
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 JohnHintIn 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
由于每一个集合的点有很多,若集合两两之间连边,边数非常大,一开始这样就超时了……然后正确做法是对每一个集合再虚拟一个节点,然后每一个点到这个节点的距离都是t且是双向的,这样一来这些点对于虚拟节点来说就都是等价的了,可以减少很多边,然后起始点就是1,终点是n,但是中间经过的一些点是虚拟节点,可以减少耗时……最后的距离要除以2,因为虚拟节点本身就不存在,在某两个实际存在点之间来回本来就只能算一次,但是用了虚拟节点却会算两次……即a->b会变成a->c->b,多加了一次相同 边权
代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1100010,M=3000010;
struct edge
{
int to;
LL dx;
int pre;
}E[M];
int head[M],cnt;
LL d1[N],d2[N];
int ans[N];
inline LL MAX(const LL &a,const LL &b)
{
return b>a?b:a;
}
void add(int s,int t,LL d)
{
E[cnt].to=t;
E[cnt].dx=d;
E[cnt].pre=head[s];
head[s]=cnt++;
}
void spfa(const int &s,LL d[])
{
typedef pair<LL,int> pli;
d[s]=0;
priority_queue<pli>Q;
Q.push(pli(-d[s],s));
while (!Q.empty())
{
int now=Q.top().second;
Q.pop();
for (int i=head[now]; ~i; i=E[i].pre)
{
int v=E[i].to;
LL w=E[i].dx;
if(d[v]>d[now]+w)
{
d[v]=d[now]+w;
Q.push(pli(-d[v],v));
}
}
}
}
void init()
{
MM(head,-1);
cnt=0;
MM(d1,INF);
MM(d2,INF);
MM(ans,0);
}
int main(void)
{
int tcase,n,m,i,j,k,x,c,t;
scanf("%d",&tcase);
for (int q=1; q<=tcase; ++q)
{
init();
scanf("%d%d",&n,&m);
for (i=1; i<=m; ++i)
{
scanf("%d%d",&t,&c);
for (j=0; j<c; ++j)
{
scanf("%d",&x);
add(x,n+i,t);
add(n+i,x,t);
}
}
spfa(1,d1);
spfa(n,d2);
printf("Case #%d: ",q);
LL dx=INF;
for (i=1; i<=n; i++)
{
LL temp=MAX(d1[i],d2[i]);
if(temp<dx)
dx=temp;
}
if(dx==INF)
puts("Evil John");
else
{
printf("%I64d\n",dx>>1);
int cnt=0;
for (i=1; i<=n; i++)
if(MAX(d1[i],d2[i])==dx)
ans[cnt++]=i;
for (i=0; i<cnt; ++i)
printf("%d%s",ans[i],i==cnt-1?"\n":" ");
}
}
return 0;
}