Meeting
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 nn blocks labelled from 11 to nn.
Bessie lives in the first block while Elsie lives in the nn-th one. They have a map of the farm
which shows that it takes they titi minutes to travel from a block in EiEito another block
in EiEi where Ei (1≤i≤m)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)T (1≤T≤6), the number of test cases. Then TT test cases
follow.
The first line of input contains nn and mm. 2≤n≤1052≤n≤105. The following mmlines describe the sets Ei (1≤i≤m)Ei (1≤i≤m). Each line will contain two integers ti(1≤ti≤109)ti(1≤ti≤109) and Si (Si>0)Si (Si>0) firstly. Then SiSi integer follows which are the labels of blocks in EiEi. It is guaranteed that ∑mi=1Si≤106∑i=1mSi≤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.
题意:给你一张无向图,边权是时间,一个人处于1点,一个人处于n点,问两个人相遇最少需要多少时间。
思路:从1出发,跑一遍最短路,从n出发跑一遍最短路,结果就是 两个时间的max 的 min . 主要是建图,用两个for 来建图显然不行。这里我也是看的题解技巧,采用的 对于一个块,建立一个虚拟的点,、这个虚拟点与这个块内所有点都连接双向边。直接用这个图跑最短路,最后的结果minn / 2 ,就是答案。
AC代码:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define INT(t) int t; scanf("%d",&t)
#define LLI(t) LL t; scanf("%I64d",&t)
using namespace std;
const int maxn = 1e5 + 10;
const LL inf = 0x3f3f3f3f3f3f3f3f;
LL dis[maxn * 2],dis1[maxn * 2];
vector<pair<int,int> >E[maxn * 20];
void dijk(int t,LL disx[]){
rep(i,0,maxn * 2) disx[i] = inf;
disx[t] = 0LL;
priority_queue<pair<LL,int> > Q;
Q.push(MP(-disx[t],t));
while(!Q.empty()){
int now = Q.top().second;
Q.pop();
for(int i = 0;i < E[now].size();i ++){
LL tmpx = (LL) E[now][i].second;
int v = E[now][i].first;
if(disx[v] > disx[now] + tmpx){
disx[v] = disx[now] + tmpx;
Q.push(MP(-disx[v],v));
}
}
}
}
int main()
{
int t; scanf("%d",&t);
int cas = 1;
while(t --){
rep(i,0,maxn * 20) E[i].clear();
int n,m; scanf("%d%d",&n,&m);
rep(i,1,m + 1){
int t,k; scanf("%d%d",&t,&k);
rep(j,0,k){
int tmp; scanf("%d",&tmp);
E[i + n].pb(MP(tmp,t));
E[tmp].pb(MP(i + n,t));
}
}
dijk(1,dis); dijk(n,dis1);
LL minn = inf;
rep(i,1,n + 1){
minn = min(max(dis[i],dis1[i]),minn);
}
printf("Case #%d: ",cas ++);
if(minn == inf) puts("Evil John");
else {
printf("%lld\n",minn / 2);
int f = 0;
rep(i,1,n + 1){
if(minn == max(dis[i] , dis1[i])){
if(!f){ printf("%d",i); f = 1;}
else printf(" %d",i);
}
}
printf("\n");
}
}
return 0;
}