hdu 5521 Meeting(优先队列+dijkstra)(建图思想)

来源:互联网 发布:痘痘变硬了怎么办知乎 编辑:程序博客网 时间:2024/06/07 10:36

Meeting

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.



题意:给你n个点和m个集合,集合内的点可以相互到达,所花费的时间都为ti(1≤i≤m)。现在有两个人分别从点1和点n处出发,问两人最快的相遇时间是多少,并输出两人的相遇地点(可能有多个相遇地点)

思路:对每个集合建立源点,源点到集合中的点的权值和集合中的点到源点的权值都为w/2,这样才能使得集合中的点之间的权值为w

然后从1开始跑一遍最短路,从n开始跑一遍最短路,遍历找到最小值即可

代码:

#include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;const int maxn=1e6+10;struct node{    int u,dis;    node() {}    node(int U,int DIS)    {        u=U,dis=DIS;    }    bool operator < (const node&A)const    {        return dis>A.dis;    }};struct edge{    int v,w,next;} E[maxn<<1];int dis1[maxn],dis2[maxn],first[maxn];bool inq[maxn];int n,m,len;void Add_edge(int u,int v,int w){    E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++;}void Dijkstra(int st,int dis[]){    for(int i=1;i<maxn;++i)        dis[i]=inf;    memset(inq,false,sizeof(inq));    priority_queue<node>q;    q.push(node(st,0));    dis[st]=0;    while(!q.empty())    {        node p=q.top();        q.pop();        if(inq[p.u])            continue;        inq[p.u]=true;        for(int i=first[p.u]; ~i; i=E[i].next)        {            int v=E[i].v,w=E[i].w;            if(dis[v]>dis[p.u]+w)            {                dis[v]=dis[p.u]+w;                q.push(node(v,dis[v]));            }        }    }}int main(){    int t,Case=0;    scanf("%d",&t);    while(++Case<=t)    {        memset(first,-1,sizeof(first));        len=0;        scanf("%d%d",&n,&m);        int w,k,x;        for(int i=1; i<=m; ++i)        {            scanf("%d%d",&w,&k);            for(int j=1; j<=k; ++j)            {                scanf("%d",&x);                Add_edge(x,n+i,w);//权值为w,防止浮点型计算                Add_edge(n+i,x,w);//权值为w            }        }        Dijkstra(1,dis1);        Dijkstra(n,dis2);        int Minn=inf;        for(int i=1; i<=n; ++i)            Minn=min(max(dis1[i],dis2[i]),Minn);        printf("Case #%d: ",Case);        if(Minn==inf)            printf("Evil John\n");        else        {            printf("%.0lf\n",Minn*1.0/2.0);//因为权值为w,所以最后要除以2            bool flag=false;            for(int i=1; i<=n; ++i)                if(max(dis1[i],dis2[i])==Minn)                    if(flag)                        printf(" %d",i);                    else                        printf("%d",i),flag=true;            puts("");        }    }    return 0;}
原创粉丝点击