HDU5521Meeting 【Dijkstra】

Meeting

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2581    Accepted Submission(s): 821


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.



Source
2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)


Recommend
wange2014

显然的最短路问题
如果直接构图 ,集合Ei中的每个点都连一条边到其他点
可能会有 1012 条边
如果对集合Ei , 建一个虚拟节点Vi Vi到没个Ei内的点花费为ti/2
那Ei内任意一个点到其他点就是 ti的花费
跑2遍Dijkstra即可

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include <string.h>
#include<math.h>

using namespace std;
#define ll long long
#define pii pair<int,int>

const int inf=1e9+7;
const int N = 2e5+5;
const int M = 1e6+5;
char CH;
int X;
inline int read(){
    while(!isdigit(CH=getchar()));
    X=CH-'0';
    while(isdigit(CH=getchar())){
        X=X*10+CH-'0';
    }
    return X;
}

struct Edge{
    int to,w,next;
}edge[N*2+M*2];
int head[N+M];

inline void addEdge(int k,int u,int v,int w){
    edge[k].to=v;
    edge[k].w=w;
    edge[k].next=head[u];
    head[u]=k;
}

int dis1[N*2+M*2];
int dis2[N*2+M*2];
bool used[N*2+M*2];
void Dijkstra(int*dis,int st,int n){
    fill(dis,dis+n+1,inf);
    fill(used,used+n+1,false);
    priority_queue<pii,vector<pii>,greater<pii> >que;
    que.push(make_pair(0,st));
    dis[st]=0;
    used[st]=true;
    while(!que.empty()){
        pii f=que.top();
        que.pop();
        int u=f.second;
        int disNow=f.first;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int to=edge[i].to;
            int w=edge[i].w;
            if(used[to]==false&&dis[to]>dis[u]+w){
                dis[to]=dis[u]+w;
                used[to]=true;
                que.push(make_pair(dis[to],to));
            }
        }
    }
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    int T=read();
    for(int t=1;t<=T;++t){
        int n=read(),m=read();
        fill(head,head+n+m+1,-1);
        int numEdge=0;
        for(int i=1;i<=m;++i){
            int t=read(),s=read();
            for(int j=1;j<=s;++j){
                int to=read();
                addEdge(numEdge++,n+i,to,t);
                addEdge(numEdge++,to,n+i,t);
            }
        }
        Dijkstra(dis1,1,n+m);
        Dijkstra(dis2,n,n+m);
        vector<int>ans;
        int minAns=inf;
        for(int i=1;i<=n;++i){
            minAns=min(minAns,max(dis1[i],dis2[i]));
        }
        if(minAns<inf){
            for(int i=1;i<=n;++i){
                if(minAns==max(dis1[i],dis2[i])){
                    ans.push_back(i);
                }
            }

            printf("Case #%d: %d\n",t,minAns/2);
            for(int i=0;i<ans.size()-1;++i){
                printf("%d ",ans[i]);
            }
            printf("%d",ans.back());
            putchar('\n');
        }
        else{
            printf("Case #%d: %s\n",t,"Evil John");
        }

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值