2015 ICPC 沈阳站M题

本文介绍了一种在特定场景下优化最短路径算法的方法。针对大量节点和集合的情况,通过缩图减少边的数量,利用Dijkstra算法结合优先队列实现高效路径搜索。适用于大规模农场地图上两点间的最短相遇时间问题。

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

 

 

M - Meeting

Time Limit:6000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 5521

Appoint description: System Crawler  (2016-04-18)

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

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个点 n<=100000 ,m《=10000个集合,在同一个集合中的人意两个点的距离都相等,不同的集合时间不一定相同, 一个人从1 出发,一个人从n出发求 二人相遇的最时间

 思路:

  如果按普通的写法 则需要建立很多的边,边太多是存不下的,所以要缩图,缩图的方法 

  

 

 

这样就可以了 ,保证了集合内的点人意点都是time  哈

  但是 使用普通的SPFA  就会超时!!QAQ 

 

    需要是用 dijktra + 优先队列  

   套了个最短路优化模版 就可以了

 

#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define  pb push_back
#define  mp make_pair
#define  sz(x) ((int)(x).size())
using namespace std;
const int N = 1000100*2;
const long long  INF = 1e18;
int n, m;
long long  dis[N];
long long dis1[N];
long long dis2[N];
long long f[N];
bool vis[N];
struct Node
{
    long long d;
    int  e;
    bool operator < (const Node x) const
    {
        return x.d < d;
    }
    Node(long long  d, int e):d(d), e(e) {}
};
vector<pair<int,long long  > > V[N];
void dijkstra(int s)
{
    priority_queue<Node> q;
    fill(dis + 1, dis + n+2*m + 1, INF);
    fill(vis + 1, vis + n+2*m + 1, false);
    q.push(Node(0, s));
    dis[s] = 0;
    while(!q.empty())
    {
        Node deq = q.top();
        q.pop();
        if(vis[deq.e])
            continue;
        vis[deq.e] = true;
        for(int i = 0; i < sz(V[deq.e]); i++)
        {
            int e = V[deq.e][i].first;
            long long  w = V[deq.e][i].second;
            if(dis[deq.e] < dis[e] - w)
            {
                dis[e] = dis[deq.e] + w;
                q.push(Node(dis[e], e));
            }
        }
    }
}
void add_edge(int a,int b,long long c)
{
    V[a].push_back(make_pair(b, c));
}
long long max(long long a,long long b)
{
    if(a>b)
        return a;
    return b;
}
long long min(long long a,long long b)
{
    if(a>b)return b;
    return a;
}
int main()
{
    int T;
    scanf("%d",&T);
    int  CASE=1 ;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        int fc = n+1;
        for(int i = 1; i<=n+m*2+1; i++)
            V[i].clear();
        long long  time ;
        int  y;
        for(int i = 1; i<=m; i++)
        {
            scanf("%lld%d",&time,&y);
            int temp ;
            for(int j = 1; j<=y; j++)
            {
                scanf("%d",&temp);
                add_edge(temp,fc,0);
                add_edge(fc+1,temp,0);
            }
            add_edge(fc,fc+1,time);
            fc+=2;
        }

        dijkstra(1);
        memcpy(dis1,dis,sizeof(dis));
        dijkstra(n);
        memcpy(dis2,dis,sizeof(dis));

        long long minv = INF;
        for(int i =1 ; i<=n; i++)
        {
            f[i] = max(dis1[i],dis2[i]);
            minv = min(minv,f[i]);
        }
        printf("Case #%d: ",CASE++);
        if(minv>=INF)
        {
            printf("Evil John\n");
        }
        else
        {
            printf("%lld\n",minv);

            int flagc = 0;
            for(int i = 1; i<=n; i++)
            {
                if(f[i]==minv)
                {
                    if(!flagc)
                    {
                        printf("%d",i);
                        flagc = 1;
                    }
                    else
                        printf(" %d",i);
                }
            }
            printf("\n");
        }
    }
    return 0;
}

 

 

 

 

 

    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值