HDU 5521 Meeting(Dijkstra+虚点建图)

HDU 5521 Meeting(Dijkstra+虚点建图)

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

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

题意

  有n个城市,两个人各居于1和n号节点,告诉你图,和路程花费时间,问两个人在哪个点相遇时间最小,输出最小时间和点的位置。

解题思路

  这题很容易就能想到利用Dijkstra算法,分别从1和n跑出到各个点的最短时间,但这题的输入是有重边的。开始的时候我强行建图,没有管重边,导致爆队列了,然后我又用最暴力的去重边法,果断TLE。后来才想到可以建立虚点,因为题目的输入是告诉你一个集合内,各个点到其他点的时间是固定的,所以我们大可以建立一个新点,我们可以把从这个点到实点的时间设为t,然后把实点到这个点的时间设为0(也可以把权值反过来),这样建出来的图也能满足条件。

  代码写的比较挫,但大意比较明确。

代码

#include<stdio.h>
#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 2e5+50;
const int inf = 0x3f3f3f3f;

struct node
{
    int x;
    long long val;
    node() {}
    node(int a,int b):x(a),val(b) {}
    friend bool operator<(node a,node b)
    {
        return a.val>b.val;
    }
};
vector<node> v[maxn];
vector<int> lin;
long long dbe[maxn],del[maxn],maxx[maxn];
int n,rea,m;

void dijkstra1()
{
    for(int i=1; i<=rea; i++) dbe[i]=inf;
    dbe[1]=0;
    priority_queue<node> q;
    q.push(node(1,0));
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        for(int i=0; i<v[x.x].size(); i++)
        {
            node y=v[x.x][i];
            if(dbe[y.x]>x.val+y.val)
            {
                dbe[y.x]=x.val+y.val;
                q.push(node(y.x,dbe[y.x]));
            }
        }
    }
}
void dijkstra2()
{
    for(int i=1; i<=rea; i++) del[i]=inf;
    del[n]=0;
    priority_queue<node> q;
    q.push(node(n,0));
    while(!q.empty())
    {
        node x=q.top();
        q.pop();
        for(int i=0; i<v[x.x].size(); i++)
        {
            node y=v[x.x][i];
            if(del[y.x]>x.val+y.val)
            {
                del[y.x]=x.val+y.val;
                q.push(node(y.x,del[y.x]));
            }
        }
    }
}
int main()
{
//    freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    for(int _=1; _<=t; _++)
    {
        for(int i=0; i<maxn; i++)//初始化的上界很重要
            v[i].clear();
        printf("Case #%d: ",_);
        scanf("%d%d",&n,&m);
        rea=n;
        while(m--)
        {
            int t,s,tmp;
            scanf("%d%d",&t,&s);
            lin.clear();
            rea++;
            while(s--)
            {
                scanf("%d",&tmp);
                if(!tmp) continue;
                lin.push_back(tmp);
                v[rea].push_back(node(tmp,t));
                v[tmp].push_back(node(rea,0));//虚点建图
            }
        }
        dijkstra1();
        dijkstra2();
        long long minn=inf;
        lin.clear();
        for(int i=1; i<=n; i++)
        {
            maxx[i]=max(dbe[i],del[i]);
            minn=min(maxx[i],minn);
        }
        for(int i=1; i<=n; i++)
        {
            if(minn==maxx[i]) lin.push_back(i);
        }
        if(minn==inf) printf("Evil John\n");
        else
        {
            printf("%d\n",minn);
            for(int i=0; i<lin.size(); i++)
                printf("%s%d",i==0?"":" ",lin[i]);
            printf("\n");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值