2015 ICPC 沈阳站M题

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;
}
时间: 2024-11-07 09:48:12

2015 ICPC 沈阳站M题的相关文章

HDU6228&amp;&amp;icpc沈阳站思维签到题

http://acm.hdu.edu.cn/showproblem.php?pid=6228 题意:给一颗无根树 给定k种颜色 问用这k种颜色染成的节点形成子树的公共边最大; 题解:刚开始想的通过两遍dfs找最长链的起始和终止位置 然后以起始为根跑dfs统计每个点子树节点个数 然后通过压缩成一个链 从左和从右分别找到两边大于等于k的位置 但是wa了  至今不知道WA在哪 然后想了一下别的思路  一个边能被当作公共边 就必须这个边的两个节点能贡献的子树个数>=k并且(n-子树个数)>=k 然后统

2016 ICPC青岛站---k题 Finding Hotels(K-D树)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations.

2016 ICPC大连站---F题 Detachment

题意:输入一个x,将x拆分成一些小的数(这些数不能相同,即x=a1+a2+......   ai!=aj when i!=j),然后这些数相乘得到一个成积(s=a1*a2*......),求最大的乘积s: 思路:考虑最简单的做法便是贪心,很明显将一个数分的越小,这个乘积越大,那么对于给的x 先找2+3+4+....+n<=x 找到最大的n  如果和小于x ,那么将n右移一个数(n->n+1)  如果和还小于x继续将n-1右移......知道和x相等时,输出s   这样做时间复杂度很高,那么得优

hdu5512 Pagodas(2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学) )

Pagodas Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 14 Accepted Submission(s): 13 Problem Description n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai M

2017ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)HDU6225.Little Boxes-大数加法

整理代码... Little Boxes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 2304    Accepted Submission(s): 818 Problem Description Little boxes on the hillside.Little boxes made of ticky-tacky.Littl

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

2016年acm icpc 青岛站(中国石油大学(华东))总结

2016年acm icpc 青岛站(中国石油大学(华东))总结 2016年11月11日,在这个传说中所谓的单身节,我们出发了,做了整整一天的车来到了青岛.不得不提一下,作为一个身处华中沿海地区的童鞋,这是我人生第一次坐了这么久的高铁(真的无聊,无聊,无聊),然后到了青岛是晚上了(挤公交车真的是一件很刺激的事情). 海风那个吹, 海水那个涌, 冻得萌新瑟瑟发抖. 也许是知道我们要来挖石油的缘故吧, 老天爷特意给了我们三天晴天. 晚上也没啥事,就这么洗洗睡了,不对还有传说中学长带领学弟剁手呢! 第二

2015北京网络赛A题The Cats&#39; Feeding Spots

题意:给你一百个点,找个以这些点为中心的最小的圆,使得这个圆恰好包含了n个点,而且这个圆的边界上并没有点 解题思路:暴力枚举每个点,求出每个点到其他点的距离,取第n大的点,判断一下. 1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #include<iostream> 5 #include<memory.h> 6 using namespace std; 7 const i

HDU 5521 Meeting(好题,最短路,2015ACM/ICPC亚洲区沈阳站)

1 /** 2 题意:有n个点,m个集合 3 集合内的点 两两 到的时间为 ti; 4 有两个人 在1,n; 输出最小相遇时间,以及哪些点(可以走走停停); 5 6 思路:****将集合抽象为点.访问过的集合不再访问. 7 Set集合抽象的点. 8 Belong属于哪个集合. 9 */ 10 #include<bits/stdc++.h> 11 using namespace std; 12 typedef long long ll; 13 const ll INF = 0x7f7f7f7f;