hdu 5521 最短路

Meeting

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

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.

/*
hdu 5521 最短路

problem:
给你n个点的图,甲在1,乙在n. 它们同时移动,问相聚时的最小花费. 然后是m个点集,点集内的任意两点之间的移动花费为ti

solve:
因为是同时移动. 所以分别对1和n求一个最短路. 然后每个节点取两个最短路中的最大值就能得到花费.
最开始想的是建立所有边,但是边的数量会太多.
所有走到一个节点时,将其所在的所有点集处理一遍. 而且只需要处理一次即可,已经维护了一个最短状态.

hhh-2016-08-30 19:56:48
*/
#pragma comment(linker,"/STACK:124000000,124000000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <vector>
#include <math.h>
#include <queue>
#include <set>
#include <map>
#define lson  i<<1
#define rson  i<<1|1
#define ll long long
#define clr(a,b) memset(a,b,sizeof(a))
#define scanfi(a) scanf("%d",&a)
#define scanfs(a) scanf("%s",a)
#define scanfl(a) scanf("%I64d",&a)
#define key_val ch[ch[root][1]][0]
#define inf 0x3f3f3f3f3f3f3f3f
using namespace std;
const ll mod = 1e9+7;
const int maxn = 100010;

struct qnode
{
    int v,c;
    qnode(int _v = 0 ,int _c =0) : v(_v),c(_c){}
    bool operator <(const qnode &a)const
    {
        return c > a.c;
    }
};

bool vis[maxn];
bool tvis[maxn];
ll dis1[maxn], dis2[maxn],cost[maxn];
int anspos[maxn];
int num[maxn];
vector<int> pos[maxn];
vector<int> have[maxn];
int T,n,m;
void dijkstra(int start,ll dis[])
{
    for(int i= 1;i <= n;i++)
    {
        vis[i] = tvis[i] = 0;
        dis[i] = inf;
    }
    priority_queue<qnode> q;
    q.push(qnode(start,0));
    dis[start] = 0;
    qnode t;
    while(!q.empty())
    {
        t = q.top();
        q.pop();
        int u = t.v;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = 0 ;i < pos[u].size();i++)
        {
            int t = pos[u][i];
//            if(tvis[t]) continue;
//            tvis[t] = 1;
            for(int j = 0;j<have[t].size();j++)
            {
                int v = have[t][j];
                if(v == u)
                    continue;
                if(dis[v] > dis[u] + cost[t])
                {
                    dis[v] = dis[u] + cost[t];
                    q.push(qnode(v,dis[v]));
                }
            }
        }
    }
}

int main()
{
    int x;
//    freopen("in.txt","r",stdin);
    scanfi(T);
    int cas = 1;
    while(T--)
    {
        scanfi(n),scanfi(m);
        for(int i = 0 ;i <= n;i++)
           pos[i].clear();
        for(int i = 0;i <= m;i++)
            have[i].clear();
        for(int i = 1;i <= m;i++)
        {
            scanf("%I64d%d",&cost[i],&num[i]);
            for(int j = 0;j < num[i];j++)
            {
                scanfi(x);
                pos[x].push_back(i);
                have[i].push_back(x);
            }
        }
        dijkstra(1,dis1);
        dijkstra(n,dis2);
        int cnt = 0;
        ll ans = inf;
        for(int i = 1;i <= n;i++)
        {
            ll t = max(dis1[i],dis2[i]);
            if(t != inf){
                ans = min(ans,t);
            }
        }
        for(int i =1; i <= n;i++)
        {
            if(max(dis1[i],dis2[i]) == ans)
            anspos[cnt++] = i;
        }
        printf("Case #%d: ",cas++);
       if(ans != inf)
       {
           printf("%I64d\n",ans);
           for(int i = 0;i < cnt;i++)
           {
               printf("%d%c",anspos[i],i == cnt-1 ? ‘\n‘:‘ ‘);
           }
       }
       else
        printf("Evil John\n");
    }
    return 0;
}

  

时间: 2024-12-24 18:45:11

hdu 5521 最短路的相关文章

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

ACM: HDU 2544 最短路-Dijkstra算法

HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你可以帮助他们吗? Input 输入包括多组数据.每组数据第一行是两个整数N.M(N<=100,M<

hdu 2112 (最短路+map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14515    Accepted Submission(s): 3405 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发

HDU 2544 最短路(我的dijkstra算法模板、SPAFA算法模板)

思路:这道题是基础的最短路径算法,可以拿来试一下自己对3种方法的理解 dijkstra主要是从第一个点开始枚举,每次枚举出当当前最小的路径,然后再以那最小的路径点为起点,求出它到其它未标记点的最短距离 bellman-ford 算法则是假设有向网中有n 个顶点.且不存在负权值回路,从顶点v1 和到顶点v2 如果存在最短路径,则此路径最多有n-1 条边.这是因为如果路径上的边数超过了n-1 条时,必然会重复经过一个顶点,形成回路:而如果这个回路的权值总和为非负时,完全可以去掉这个回路,使得v1到v

hdu 4849 最短路 西安邀请赛 Wow! Such City!

http://acm.hdu.edu.cn/showproblem.php?pid=4849 会有很多奇怪的Wa的题,当初在西安就不知道为什么wa,昨晚做了,因为一些Sb错误也wa了很久,这会儿怎么写都会AC.... 收获: 1.还是基本都构思好在去敲代码,因为当时没过,昨晚心里有阴影,敲得很慢,而且最开始各种取模以防止漏掉,太保守了......以后一定先估算是不是需要取模防止TLE,当然时间够的话还是适当多取个模防止莫名其妙的错误.. 2.如果出错,注意参数是不是对的,最开始写好之后,因为m和

hdu 2544 最短路 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目意思:给出 n 个路口和 m 条路,每一条路需要 c 分钟走过.问从路口 1 到路口 n 需要的最短时间是多少. 这题是最短路的入门题,从理解d-i--j---k(wg自创的,呵呵)到默打到修改,搞左两日终于好了,哈哈哈~~~太感动了. 第一次错是 少了Dijkstra()函数中的 for (j = 1; j <= n; j++) . 第二次错是把vis[k=j]=1 写在了 if (!v

hdu 1595 最短路

题意是要我们求出最短路中一条路坏的情况下最大的时间: 我们先将最短路求出并记录路径,然后枚举最短路中每一条路坏的情况,求出最大的时间.. <span style="font-size:24px;">#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<cmath> using namespace st

HDU 2544 最短路 (最短路)

最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 31955    Accepted Submission(s): 13845 Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最

hdu 2544 最短路 (dijkstra,floyd)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目大意:找到两点间最短的距离值. 代码一:(dijkstra算法) 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int n,Min,node[105],visit[105],map[105][105]; 5 void set() 6 { 7 for (int i=1; i<=n; i