Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 4542 Accepted Submission(s): 1436
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.
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.
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.
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
3 4
Case #2: Evil John
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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<functional>
using namespace std;
#define LL long long
#define pli pair<long long,int>
#define mp make_pair
#define inf 0x7fffffffffffff
struct Edge{
int v,w,next;
}e[];
int first[];
int tot,n,m;
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
bool vis[];
LL d1[],d2[];
void dij(int s,LL d[]){
memset(vis,,sizeof(vis));
int tot_n=n+m+;
for(int i=;i<=tot_n;++i) d[i]=inf;
priority_queue<pli,vector<pli>,greater<pli> >q;
q.push(mp(,s));
d[s]=;
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(d[e[i].v]>d[u]+e[i].w){
d[e[i].v]=d[u]+e[i].w;
q.push(mp(d[e[i].v],e[i].v));
}
}
}
}
int main()
{
int t,i,j,k;
int cas=;
cin>>t;
while(t--){int ti,si,a;
memset(first,-,sizeof(first));
tot=;
cin>>n>>m;
for(i=;i<=m;++i){
scanf("%d%d",&ti,&si);
while(si--){
scanf("%d",&a);
add(a,n+i,ti);
add(n+i,a,ti);
}
}
printf("Case #%d: ",++cas);
dij(,d1);
dij(n,d2);
LL mint=inf;
for(i=;i<=n;++i){
mint=min(mint,max(d1[i],d2[i]));
}
if(mint==inf){
puts("Evil John");
}
else{
cout<<mint/<<endl;
for(i=;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf("%d",i);
break;
}
}
i++;
for(;i<=n;++i){
if(mint==max(d1[i],d2[i])){
printf(" %d",i);
}
}
puts("");
}
}
return ;
}