Meeting(未建边的最短路)

本文探讨了Bessie和Elsie如何在被分割成不同区块的农场上找到相遇地点的问题。通过使用迪杰斯特拉算法解决复杂路径问题,作者详细介绍了计算时间和推荐相遇地点的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次接触这种题目,a了一天,终于a过了,赶紧上课去

http://acm.hdu.edu.cn/showproblem.php?pid=5521

Meeting

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


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.
 
熟悉的迪杰斯特拉代码
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;
const int N=100010;
const long long maxn=1LL<<60;
int n,m,s,t[N],a;
vector<vector<int> > G,se;
long long dis1[N],dis2[N];
bool used1[N],used2[N];
struct node{
    int v;
    long long w;
    node(int vv,long long ww):v(vv),w(ww){}
    bool operator <(const node &a)const {
        return w>a.w;
    }
};
void dijst(long long *dis,int s){
    priority_queue<node> q;
    memset(used1,0,sizeof(used1));
    memset(used2,0,sizeof(used2));
    for(int i=1;i<=n;i++)
        dis[i]=maxn;
    q.push(node(s,0));
    while(!q.empty()){
        node p=q.top();
        q.pop();
        int u=p.v;
        if(used1[u])continue;
        used1[u]=true;
        dis[u]=p.w;
        for(int i=0;i<G[u].size();i++){
            int x=G[u][i];
            if(used2[x])continue;
            used2[x]=true;
            int w=t[x];
            for(int j=0;j<se[x].size();j++){
                int v=se[x][j];
                if(used1[v]||dis[v]<dis[u]+w)continue;
                dis[v]=dis[u]+w;
                q.push(node(v,dis[v]));
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int cases=1;cases<=T;cases++){
        scanf("%d%d",&n,&m);
        G.clear();
        se.clear();
        G.resize(n+10);
        se.resize(m+10);
        for(int i=1;i<=m;i++){
            scanf("%d%d",&t[i],&s);
            for(int j=1;j<=s;j++){
                scanf("%d",&a);
                G[a].push_back(i);
                se[i].push_back(a);
            }
        }
        dijst(dis1,1);
        dijst(dis2,n);
        long long ans=maxn;
        for(int i=1;i<=n;i++){
            dis1[i]=max(dis1[i],dis2[i]);
            ans=min(dis1[i],ans);
        }
        if(ans==maxn){
            printf("Case #%d: Evil John\n",cases);
            continue;
        }
        int cnt=0;
        for(int i=1;i<=n;i++)
            if(dis1[i]==ans)
            dis2[cnt++]=i;
        printf("Case #%d: %I64d\n",cases,ans);
        printf("%I64d",dis2[0]);
        for(int i=1;i<cnt;i++)
            printf(" %I64d",dis2[i]);
        printf("\n");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值