1. 程式人生 > >5521  Meeting (最短路 ,技巧建圖)

5521  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;
}