首页 > 其他分享 >1022 Meeting(uvalive 可能会交不上) 分层图 最短路

1022 Meeting(uvalive 可能会交不上) 分层图 最短路

时间:2022-08-20 17:33:50浏览次数:57  
标签:blocks 1022 uvalive they 会交不上 Ei line include block

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 ∑m i=1 Si ≤ 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. 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. 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 

分析

给若干个集合,集合内的节点之间的距离是t

问从1到n 双向奔赴的最短距离。

基本思路比较好想:直接用 1 和 n 为起点跑两遍dijstrra。

难点是建图。每个集合节点互相连边的话要n^2 条边

直接和集合连一条长度为t的边,集合向节点连长度为0的边就可以了。

 

#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;
}

 

标签:blocks,1022,uvalive,they,会交不上,Ei,line,include,block
From: https://www.cnblogs.com/er007/p/16608219.html

相关文章