HDU 5521.Meeting 最短路模板题

Meeting

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

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

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.

Source

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521

题意:有m个集合,每个集合里面的任意两点均有一条距离为ei的无向边,求1和n到其他点的最短距离中最大值的最小值。

思路:最短路模板题。每个集合作为作为一个点,对应的点到集合的距离为ei,最后答案/2。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<bitset>
#include<queue>
#include<stack>
#include<map>
#include<vector>
using namespace std;
#define eps 0.0000001
typedef long long ll;
typedef pair<int,int> P;
const int maxn=2e5+100,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
const ll INF=1e18+7;
struct edge
{
    int from,to;
    ll w;
};
vector<edge>G[maxn];
priority_queue<P,vector<P>,greater<P> >q;
ll dist[2][maxn];
void addedge(int u,int v,ll w)
{
    G[u].push_back((edge)
    {
        u,v,w
    });
    G[v].push_back((edge)
    {
        v,u,w
    });
}
void dij(int t,int s)
{
    dist[t][s]=0LL;
    q.push(P(dist[t][s],s));
    while(!q.empty())
    {
        P p=q.top();
        q.pop();
        int u=p.second;
        for(int i=0; i<G[u].size(); i++)
        {
            edge e=G[u][i];
            if(dist[t][e.to]>dist[t][u]+e.w)
            {
                dist[t][e.to]=dist[t][u]+e.w;
                q.push(P(dist[t][e.to],e.to));
            }
        }
    }
}
void init(int n)
{
    for(int i=0; i<=2*n+10; i++) G[i].clear();
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int Case=1; Case<=T; Case++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1; i<=m; i++)
        {
            int val;
            scanf("%lld",&val);
            int t;
            scanf("%d",&t);
            while(t--)
            {
                int s;
                scanf("%d",&s);
                addedge(s,n+i,val);
            }
        }
        for(int i=0; i<=2*n+10; i++) dist[0][i]=dist[1][i]=INF;
        dij(0,1);
        dij(1,n);
        ll ans=INF;
        for(int i=1; i<=n; i++)
        {
            //printf("%lld %lld\n",dist[0][i],dist[1][i]);
            ans=min(ans,max(dist[0][i],dist[1][i]));
        }
        printf("Case #%d: ",Case);
        if(ans>=INF) puts("Evil John");
        else
        {
            printf("%lld\n",ans/2);
            int cou=0;
            for(int i=1; i<=n; i++)
            {
                if(!cou&&max(dist[0][i],dist[1][i])==ans) printf("%d",i),cou++;
                else if(cou&&max(dist[0][i],dist[1][i])==ans) printf(" %d",i),cou++;
            }
            printf("\n");
        }
        init(n);
    }
    return 0;
}

最短路模板题

时间: 2024-12-29 07:58:46

HDU 5521.Meeting 最短路模板题的相关文章

poj1511/zoj2008 Invitation Cards(最短路模板题)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Invitation Cards Time Limit: 5 Seconds      Memory Limit: 65536 KB In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fa

HDU 4085 斯坦纳树模板题

Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 971    Accepted Submission(s): 416 Problem Description You may all know the famous story "Three monks". Recently they find som

hdu 2085 数塔 -- dp模板题

数塔 Problem Description 在讲述DP算法的时候,一个经典的例子就是数塔问题,它是这样描述的: 有如下所示的数塔,要求从顶层走到底层,若每一步只能走到相邻的结点,则经过的结点的数字之和最大是多少? 已经告诉你了,这是个DP的题目,你能AC吗? Input 输入数据首先包括一个整数C,表示测试实例的个数,每个测试实例的第一行是一个整数N(1 <= N <= 100),表示数塔的高度,接下来用N行数字表示数塔,其中第i行有个i个整数,且所有的整数均在区间[0,99]内. Outp

[poj2449]Remmarguts&#39; Date(K短路模板题,A*算法)

解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> #include<queue> using namespace std; typedef long long ll; const int N=1e3+10; const

HDU 5521 Meeting(虚拟节点+最短路)

Meeting Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 1358    Accepted Submission(s): 435 Problem Description Bessie and her friend Elsie decide to have a meeting. However, after Farmer Jo

HDU 2222(AC自动机模板题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题思路: AC自动机模板题. 一开始使用LRJ的坑爹静态模板,不支持重复的模式串. 在做AC自动机+DP的时候,扒了zcwwzdjn大神的动态优化(失配指向root)写法,以及借鉴了网上的AC自动机模板, 搞出了这么一个支持重复串的模板. #include "cstdio" #include

hdu1874 最短路模板题

之所以做了第二道模板题还要写是因为发现了一些自己的问题 用的是dij 最简单的松弛 需要注意的地方是松弛的时候 判断dis[i]<dis[w]+tance[w][i]时 还要再判断 vis[i] 要保证这个点没有成为过最小点 即这个点不会是已经被松弛过的点 输入的时候要注意 可能会有重边的输入 每次输入的时候进行一次判断 如果输入的是较大值 就不用更换了 关于memset的使用 它只能用来设置0与-1 别的值会出现莫名的错误 #include<stdio.h> #include<s

Til the Cows Come Home (最短路模板题)

个人心得:模板题,不过还是找到了很多问题,真的是头痛,为什么用dijkstra算法book[1]=1就错了..... 纠结中.... Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so s

hdu 2844 Coins 多重背包模板题 ,二进制优化。据说是楼教主的男人八题之一

Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8052    Accepted Submission(s): 3291 Problem Description Whuacmers use coins.They have coins of value A1,A2,A3...An Silverland dollar. One