HDU5521 双向最短路+优先队列优化

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 blocks labelled from to .
Bessie lives in the first block while Elsie lives in the -th one. They have a map of the farm
which shows that it takes they minutes to travel from a block in to another block
in where 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 , the number of test cases. Then test cases
follow.

The first line of input contains and . . The following lines describe the sets . Each line will contain two integers and firstly. Then integer follows which are the labels of blocks in . It is guaranteed that .
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

这个题普通的dijkstra会超时
最开始用普通的,我尝试着用vector+map存集合失败了…
然后每个集合加进去一个点,让集合中所有点连接一个点
这个思路是很有趣….就相当于互相之间的距离*2…这个要好好体会这是这个题核心的一部分
然后还是T了
就很迷
学会了优先队列优化
就是把对于每个点来说的当前对于起点最短的距离存进去
直接用优先队列跳出来作为下一次循环的起始点
如果前面标记过了就不要…
有点bfs的感觉…

#include<vector>
#include<iostream>
#include<cstdio>
#include<memory.h>
#include<algorithm>
#include<queue>
using namespace std;
vector<int>tu[1500000];
vector<int>juli[1500000];
bool yijing1[200000],yijingn[200001];
long long inf = 0x3f3f3f3f, daoyi[200000], daon[200000];
struct p
{
    long long chang, dian;
    bool operator < (const p&a)const {
        return chang > a.chang;
    }
};
int main()
{
#define int long long
    int T;
    cin >> T;
    int u = 0;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        memset(yijing1, 0, sizeof(yijing1));
        memset(yijingn, 0, sizeof(yijingn));
        memset(daoyi, 0x3f, sizeof(daoyi));
        memset(daon, 0x3f, sizeof(daon));
        for (int a = 1;a <= n + m;a++)tu[a].clear(), juli[a].clear();
        for (int a = 1;a <= m;a++)
        {
            int q, w;
            scanf("%lld%lld", &q, &w);
            for (int b = 1;b <= w;b++)
            {
                int ww;
                scanf("%lld", &ww);
                tu[ww].push_back(n + a);
                tu[n + a].push_back(ww);
                juli[ww].push_back(q);
                juli[n + a].push_back(q);
            }
        }
    //  yijing1[1] = yijingn[n]=1;
        daoyi[1] = daon[n] = 0;
        priority_queue<p>q1, qn;
        p zans1,zansn;
        zans1.chang = 0, zans1.dian = 1;
        zansn.chang = 0, zansn.dian = n;
        q1.push(zans1),qn.push(zansn);
        while (!q1.empty())
        {
            p zz = q1.top();
            q1.pop();
            if (yijing1[zz.dian])continue;
            yijing1[zz.dian] = 1;
            for (int a = 0;a < tu[zz.dian].size();a++)
            {
                if (yijing1[tu[zz.dian][a]])continue;
                if (daoyi[tu[zz.dian][a]] >= daoyi[zz.dian] + juli[zz.dian][a])
                {
                    daoyi[tu[zz.dian][a]] = daoyi[zz.dian] + juli[zz.dian][a];
                    p ii;
                    ii.dian = tu[zz.dian][a], ii.chang = daoyi[tu[zz.dian][a]];
                    q1.push(ii);
                }
            }
        }
        while (!qn.empty())
        {
            p zz = qn.top();
            qn.pop();
            if (yijingn[zz.dian])continue;
            yijingn[zz.dian] = 1;
            for (int a = 0;a < tu[zz.dian].size();a++)
            {
                if (yijingn[tu[zz.dian][a]])continue;
                if (daon[tu[zz.dian][a]] >= daon[zz.dian] + juli[zz.dian][a])
                {
                    daon[tu[zz.dian][a]] = daon[zz.dian] + juli[zz.dian][a];
                    p ii;
                    ii.dian = tu[zz.dian][a], ii.chang = daon[tu[zz.dian][a]];
                    qn.push(ii);
                }
            }
        }
        int gg = inf;
        for (int a = 1;a <= n;a++)
        {
            gg = min(gg, max(daoyi[a], daon[a]));
            //gg /= 2;
        }
        if (gg == inf)printf("Case #%lld: Evil John\n", ++u);
        else
        {
            printf("Case #%lld: %lld\n", ++u,gg/2);
            int t = 0;
            for (int a = 1;a <= n;a++)
            {
                if (gg == max(daoyi[a], daon[a]))
                {
                    if (t == 0)
                    {
                        printf("%lld",a);
                        t = 1;
                    }
                    else printf(" %lld",a);
                }
            }
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值