hdoj 5521 Meeting 【优先队列 dijkstra】

本文深入探讨了信息技术领域的核心内容,包括前端开发、后端开发、移动开发、游戏开发等细分技术领域,同时覆盖了大数据开发、开发工具、嵌入式硬件、音视频基础、AI音视频处理等前沿技术。通过详细分析各技术要点及实际应用案例,旨在为读者提供全面且深入的技术洞察。

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



Meeting

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


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.
 



题意:给定n个点、m个块,给你组成每个块的点,已知块中任意两点间互达需要花费的时间为定值。现在一人从点1出发,一人从点n出发,问你两人相遇的所需的最短时间,不能相遇输出Evil John,否则输出相遇所需的最短时间和相遇点,若有多个相遇点,按字典序输出。


思路:点 连所有 它从属的块,块存储它所有的点。从1和n各跑一次最短路,然后枚举相遇点,更新答案。


用SPFA写了一发,TLE o(╯□╰)o 改了下又WA了,太弱了。

不得不膜拜大牛了,最后发现是用优先队列 dijkstraAC的。 (⊙o⊙)哦



AC代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 100000+10
#define MAXM 50000000
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while(a--)
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
struct Node{
    int d, node;
    bool friend operator < (Node a, Node b){
        return a.d > b.d;
    }
};
vector<int> B[MAXN];
vector<int> G[MAXN];
int n, m;
int T[MAXN];
void getMap()
{
    Ri(n), Ri(m);
    for(int i = 1; i <= n; i++)
        G[i].clear();
    for(int i = 1; i <= m; i++)
    {
        int num;
        Ri(T[i]), Ri(num);
        B[i].clear();
        while(num--)
        {
            int a; Ri(a);
            G[a].push_back(i);
            B[i].push_back(a);
        }
    }
}
int dist[2][MAXN];
bool vis[MAXN];
bool block[MAXN];
void SPFA(int s, int op)
{
    priority_queue<Node> Q;
    CLR(dist[op], INF); CLR(vis, false); CLR(block, false);
    dist[op][s] = 0; Q.push((Node){dist[op][s], s});
    while(!Q.empty())
    {
        int u = Q.top().node;
        Q.pop();
        if(block[u]) continue;
        block[u] = true;
        for(int i = 0; i < G[u].size(); i++)
        {
            int k = G[u][i];
            if(vis[k]) continue;
            vis[k] = true;
            for(int j = 0; j < B[k].size(); j++)
            {
                int v = B[k][j];
                if(dist[op][v] > dist[op][u] + T[k])
                {
                    dist[op][v] = dist[op][u] + T[k];
                    Q.push((Node){dist[op][v], v});
                }
            }
        }
    }
}
vector<int> P;
int kcase = 1;
void solve()
{
    getMap();
    SPFA(1, 0); SPFA(n, 1);
    int ans = INF;
    for(int i = 1; i <= n; i++)
    {
        int need = max(dist[0][i], dist[1][i]);
        if(ans > need)
        {
            ans = need;
            P.clear();
        }
        if(ans == need)
            P.push_back(i);
    }
    printf("Case #%d: ", kcase++);
    if(ans == INF)
        printf("Evil John\n");
    else
    {
        printf("%d\n", ans);
        int top = P.size()-1;
        for(int i = 0; i < top; i++)
            printf("%d ", P[i]);
        printf("%d\n", P[top]);
    }
}
int main()
{
    int t; Ri(t);
    W(t){
        solve();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值