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\n",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\n",ans[cnt]);
        }
          else printf("Evil John\n");
      }
      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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值