[HDU 5521] 2015ACM/ICPC亚洲区沈阳站 Meeting 最短路

Meeting
Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1190 Accepted Submission(s): 374

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

题意
有N个点,两个人,其中一个人住在点1,另一个人住在点n
有M个点集,集合内的数表示任意两点的距离为dis
现在问,如果两个人要见面,需要最短距离是多少,有哪几个点能被当成见面点

思路
求最短路时注意一个集合松弛后只松弛一次即可;

代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
int T;
vector<int> be[100010];
vector<int> lin[100010];
long long d1[100010];
bool used[100010];
long long d2[100010];
long long inf=99999999999999;
int n,m;
bool vis[100010];
long long dis[100005];
long long num[100005];

struct node{
    int y;long long step;
    node(int yy,long long  st)
    {
        y=yy;
        step=st;
    }node(){}
    bool operator < (const node &a) const{
        return step> a.step;
    }
};
priority_queue<node>  q;
void di(int s,long long *d)
{
    for(int i=1;i<=n;i++) d[i]=inf;
    d[s]=0;
    memset(vis,0,sizeof(vis));
    memset(used,0,sizeof(used));
    q.push(node(s,0));
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        int y=now.y;

        for(int i=0;i<be[y].size();i++)
        {
            int v=be[y][i];
            if(used[v]) continue;
            used[v]=1;
            for(int j=0;j<lin[v].size();j++)
            {
                int vv=lin[v][j];
                if(vv==y) continue;
                if(d[vv]>d[y]+dis[v])
                {
                    d[vv]=d[y]+dis[v];
                    q.push(node(vv,d[vv]));
                }
            }
        }

    }

}
int main()
{
    int aa;
    scanf("%d",&T);
    for(int ii=1;ii<=T;ii++)
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        be[i].clear();
        for(int i=1;i<=m;i++)
        {
            lin[i].clear();
            scanf("%lld%lld",&dis[i],&num[i]);
            for(int j=1;j<=num[i];j++)
            {
                scanf("%d",&aa);
                be[aa].push_back(i);
                lin[i].push_back(aa);
            }                       
        }
        di(1,d1);
        di(n,d2);
        long long ans=inf;
        for(int i=1;i<=n;i++)
        {
            ans=min(ans,max(d1[i],d2[i]));
        }
        if(ans==inf) printf("Case #%d: Evil John\n", ii);
        else 
        {
                printf("Case #%d: %lld\n", ii, ans);
                bool flag = false;
                for (int i=1;i<=n;i++) 
                    if (ans==max(d1[i],d2[i]))  
            {
                        if (flag) printf(" ");
                         printf("%d", i);
                         flag = true;
                    }   
            printf("\n");
             }
    }
}

你可能感兴趣的:(ACM)