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.
思路:
对于每一个set建立一个虚拟原点,每个set中的点只与虚拟远点建立无向边,边权值为这个set的t/2。
这样就可以保证在建边较少的情况下使set内部两点之间的距离为t。
从1跑一边dijkstra,得到到达每个点的最小值。
从n跑一边dijkstra,得到到达每个点的最小值。
枚举给个点的dist取一遍最大值,将该值存储在mx中,表示如果在这个点见面,需要多少分钟。
假设在所有点中见面所需要的最小值res,那么所有
m
x
[
i
]
=
=
r
e
s
mx[i]==res
mx[i]==res的点,都可以作为见面的点,打印出i即可。
代码:
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<double,int>PDI;
const int N=1e7+10;
int e[N],ne[N],h[N],idx=0;
double w[N];
double dist1[N],dist2[N];
bool st[N];
int n,m;
int mx[N];
void add(int a,int b,double c)
{
w[idx]=c;
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
void dijkstra(int u,double dist[])
{
for(int i=1;i<N;i++)
{
dist[i]=0x3f3f3f3f;
}
memset(st,false,sizeof st);
priority_queue<PDI, vector<PDI>, greater<PDI> >q;
q.push({ 0,u });
dist[u] = 0;
while (!q.empty())
{
PDI t = q.top();
q.pop();
int pos = t.second;
double dis = t.first;
if (st[pos])
{
continue;
}
st[pos] = true;
for (int i = h[pos]; i != -1; i = ne[i])
{
int j = e[i];
if (dist[j] > dis + w[i])
{
dist[j] = dis + w[i];
q.push({ dist[j],j });
}
}
}
}
inline int read()
{
int X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
}
int main()
{
int T;
cin>>T;
for(int p=1;p<=T;p++)
{
idx=0;
memset(h,-1,sizeof h);
n=read();
m=read();
for(int i=1;i<=m;i++)
{
int t,s;
t=read();
s=read();
double va=(t*1.0)/2;
int e1=i+n;
for(int j=0;j<s;j++)
{
int k;
k=read();
add(e1,k,va);
add(k,e1,va);
}
}
dijkstra(1,dist1);
dijkstra(n,dist2);
int res=0x3f3f3f3f;
for(int i=1;i<=n;i++)
{
mx[i]=max(dist1[i],dist2[i]);
res=min(res,mx[i]);
}
cout<<"Case #"<<p<<": ";
if(res==0x3f3f3f3f)
{
cout<<"Evil John"<<endl;
continue;
}
cout<<res<<endl;
bool flag=true;
for(int i=1;i<=n;i++)
{
if(res==mx[i])
{
if(!flag)
{
cout<<' ';
}
flag=false;
cout<<i;
}
}
cout<<endl;
}
}