hdu5521 Meeting

传送门

题目

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 (1im) 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 (1T6), the number of test cases. Then T test casesfollow.
The first line of input contains n and m. 2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109) and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
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 multipleoptional blocks, output all of them in ascending order.

题目大意

现在给出一个图,其中分成点分成一个集合一个集合的,每一集合中的点互相之间的距离都是相同的,也给出来了;现在要求两个人分别从1和n出发,问最短多长时间才能遇到,且给出这些可能的相遇点;

分析

这个题主要在构图,思路比较好想。将所有点和它所属的集合依次连边,原本的点无点权,集合所代表的点权即为每一集合中的点互相之间的距离。然后以第1个点和第n个点分别为起点计算出点权最短路即可,最终每点的相遇所需时间即为两次计算的值中较大的那一个。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
vector<int>v[200100];
priority_queue<pair<int,int> >q;
int vis[210000],d1[210000],d2[210000],val[210000];
int ans[210000];
int main()
{     int n,m,i,j,k,t,p,s,x;
      scanf("%d",&t);
      for(p=1;p<=t;p++){
        scanf("%d%d",&n,&m);
        for(i=1;i<=n+m;i++)v[i].clear();
        memset(vis,0,sizeof(vis));
        memset(d1,0x3f,sizeof(d1));
        memset(d2,0x3f,sizeof(d2));
        memset(val,0,sizeof(val));
        for(i=1;i<=m;i++){
            scanf("%d%d",&val[i+n],&s);
            for(j=1;j<=s;j++){
                scanf("%d",&x);
                v[x].push_back(i+n);
                v[i+n].push_back(x);
            }
        }
        d1[1]=0;
        q.push(make_pair(0,1));
        while(!q.empty()){
          x=q.top().second;
          q.pop();
          if(vis[x])continue;
          vis[x]=1;
          for(i=0;i<v[x].size();i++){
              int y=v[x][i];
              if(d1[x]+val[y]<d1[y]){
                d1[y]=d1[x]+val[y];
                q.push(make_pair(-d1[y],y));
            }
          }
        }
        memset(vis,0,sizeof(vis));
        d2[n]=0;
        q.push(make_pair(0,n));
        while(!q.empty()){
          x=q.top().second;
          q.pop();
          if(vis[x])continue;
          vis[x]=1;
          for(i=0;i<v[x].size();i++){
              int y=v[x][i];
              if(d2[x]+val[y]<d2[y]){
                d2[y]=d2[x]+val[y];
                q.push(make_pair(-d2[y],y));
            }
          }
        }
        int pl,minn=0x3f3f3f3f;
        for(i=1;i<=n;i++)
           if(max(d1[i],d2[i])<minn){
                minn=max(d1[i],d2[i]);
                pl=i;
           }
        int cnt=0;
        printf("Case #%d: ",p);
        if(minn<0x3f3f3f3f){
          printf("%d
",minn);
          for(i=1;i<=n;i++)
             if(max(d1[i],d2[i])==minn)
               ans[++cnt]=i;
          for(i=1;i<cnt;i++)
             printf("%d ",ans[i]);
          printf("%d
",ans[cnt]);
        }
          else printf("Evil John
");
      }
      return 0;
}
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 (1im) 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 (1T6), the number of test cases. Then T test cases
follow.

The first line of input contains n and m. 2n105. The following m lines describe the sets Ei (1im). Each line will contain two integers ti(1ti109) and Si (Si>0) firstly. Then Si integer follows which are the labels of blocks in Ei. It is guaranteed that mi=1Si106.
 
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.
原文地址:https://www.cnblogs.com/yzxverygood/p/9200776.html