HDU-5521 Meeting(Dijkstra)

Meeting

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


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 (1im)  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 (1T6) , the number of test cases. Then  T  test cases
follow.

The first line of input contains  n  and  m 2n105 . The following  m  lines describe the sets  Ei (1im) . Each line will contain two integers  ti(1ti109)  and  Si (Si>0)  firstly. Then  Si  integer follows which are the labels of blocks in  Ei . It is guaranteed that  mi=1Si106 .
 

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

传送门:http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?pid=1006&ojid=0&cid=11108&hide=0

题意:有N个点,两个人,其中一个人住在点1,另一个人住在点n 有M个点集,集合内的数表示任意两点的距离为t ,现在问如果两个人要见面,需要最短距离是多少,有哪几个点能被当成见面点

题解:可以知道在最短路上每个点集只会被遍历一次,如果被走第二次那么肯定不会是最短路直接跳出(这里可以节省大量时间避免TLE),因为2个人可以同时从1和n出发,所以可以分别从1,n开始走最短路,对于每个点,取两人到达该点的最长距离,最短路就是这些距离的最小值

#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
#include<set>
#include<queue>
#include<functional>
using namespace std;
typedef long long  LL;
typedef pair<LL,int> PII;
const int maxn = 1e5 +5;
vector<int>vec[maxn*10];
vector<int>edge[maxn];
bool vis[maxn*10];
int a[maxn];
LL t[maxn*10];
void dij(int st,LL d[]){
    d[st]=0;
    memset(vis,0,sizeof(vis));
    priority_queue<PII,vector<PII>,greater<PII> >q;
    q.push(PII(d[st],st));
    while(!q.empty()){
        PII p=q.top();q.pop();
        if(d[p.second]<p.first) continue;
        int u=p.second,m=edge[u].size();
        for(int i=0;i<m;i++){
            if(vis[edge[u][i]]) continue;
            vis[edge[u][i]]=1;
            int E=edge[u][i];
            int mm=vec[E].size();
            for(int j=0;j<mm;j++){
                int v=vec[E][j];
                if(d[v]>d[u]+t[E]){
                    d[v]=d[u]+t[E];
                    q.push(PII(d[v],v));
                }
            }
        }
    }
}
LL d[maxn],D[maxn];
int main(){
    int T,n,m;
   // freopen("in.txt","r",stdin);
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++) vec[i].clear();
        for(int i=1;i<=n;i++) edge[i].clear();
        memset(d,0x3f,sizeof(d));
        memset(D,0x3f,sizeof(D));
        for(int i=1;i<=m;i++){
            int s;
            scanf("%I64d%d",&t[i],&s);
            for(int j=0;j<s;j++){
                scanf("%d",&a[j]);
                edge[a[j]].push_back(i);
                vec[i].push_back(a[j]);
            }
        }
        dij(1,d);
        dij(n,D);
        LL ans=d[0];
        for(int i=1;i<=n;i++) ans=min(ans,max(d[i],D[i]));
        printf("Case #%d: ",cas);
        if(ans<d[0]) {
            printf("%I64d\n",ans);
            int flag=0;
            for(int i=1;i<=n;i++) if(max(d[i],D[i])==ans){
                if(flag) printf(" %d",i);
                else {printf("%d",i);flag=1;}
            }
            printf("\n");
        }
        else printf("Evil John\n");
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值