HDU 5521 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 (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.
 

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
解: 虚拟m个点(不属于1~n),也就是每个集合一个,让每个集合的点与相应的点建立双向关系;两遍spfa.(合理性自己画个图可简单了解)
原因:大大减少了建边数,不会超时;
说一下 邻接表的head最好不要放在结构体里,自己单独拿出来自成数组,否则赋值-1不方便,而且有些情况会超时;
#include <iostream>  
#include <algorithm>   
#include <string.h> 
#include <queue> 
using namespace std;  
const long long inf=1e18+10;
const long long maxx=1500000;
typedef long long ll;
int m,n,q;  
ll dis[maxx];
ll di[maxx];
ll sto[maxx];
ll cc[maxx];
int head[maxx];
bool vis[maxx]; 
struct  point{    
    int to;  
    int next; 
	int vaule; 
};  
point pt[2500100]; //注意边数与点数 (题目中有限制)
ll minn;
void add(int u,int v,int val)
{
	      pt[q].next=head[u];  
          pt[q].to=v;  
		  pt[q].vaule=val;
		  head[u]=q++;
} 
void SPFA(int st)  
{  
    for (int i=0;i<=m+n;i++)
    dis[i]=inf;
    queue<int>q;
    int w,v;
    memset(vis,false,sizeof(vis));
    while (!q.empty())q.pop();
    q.push(st);
    dis[st]=0;
    vis[st]=true;
    while (!q.empty())
    {
    	v=q.front();
    	q.pop();
    	vis[v]=false;
    	for (int i=head[v];i!=-1;i=pt[i].next)
    	{
    		w=pt[i].vaule;
    		if (w+dis[v]<dis[pt[i].to])
    		{
    			dis[pt[i].to]=w+dis[v];
    			if (!vis[pt[i].to])
    			{
    				vis[pt[i].to]=true;
    				q.push(pt[i].to);
				}
			}
		}   
    }

}  
void SPFA2(int st)  
{  
   for (int i=0;i<=m+n;i++)
    di[i]=inf;
    queue<int>q;
    int w,v;
    memset(vis,false,sizeof(vis));
    while (!q.empty())q.pop();
    q.push(st);
    di[st]=0;
    vis[st]=true;
    while (!q.empty())
    {
    	v=q.front();
    	q.pop();
    	vis[v]=false;
    	for (int i=head[v];i!=-1;i=pt[i].next)
    	{
    		w=pt[i].vaule;
    		if (w+di[v]<di[pt[i].to])
    		{
    			di[pt[i].to]=w+di[v];
    			if (!vis[pt[i].to])
    			{
    				vis[pt[i].to]=true;
    				q.push(pt[i].to);
				}
			}
		}   
    }

}  
int main()  
{  
    int u,v,sum,temp,val,t,cn,nu,ans=1,k,b;
    ll minn,mi;
	scanf("%d",&cn);
	while(cn--)
	{
		int flag=1;
	  mi=inf;
	  minn=0;
	  k=0;
      q=1; 
      scanf("%d %d",&n,&m);
       memset(head,-1,sizeof(head)); 
        for (int i=1;i<=m;i++) 
        {  
          scanf("%d %d",&t,&nu);
          for (int j=0;j<nu;j++)
          {
		       scanf("%d",&b);
		  	    {
		  	    	 add(b,n+i,t);
		  	    	 add(n+i,b,t);
				}
			}
        }  
       SPFA(1);
       SPFA2(n);
       for (int i=1;i<=n;i++)
       {
       	 if (dis[i]==inf||di[i]==inf)
       	 continue;
       	 minn=max(di[i],dis[i]);
       	 di[i]=dis[i]=minn;
       	 if (minn<mi) 
			{ 
			   mi=minn;
       	    }
	   }
	   printf("Case #%d: ",ans++);
	   if (mi==inf)
	   cout<<"Evil John"<<endl;
	   else
	   {
	     printf("%lld\n",mi/2);
	     for (int i=1;i<=n;i++)
	     {
	         if (mi==di[i])
	         {
	           if (flag)
	           {
	           printf("%d",i);
	           flag=0;
	           }
	           else
	           printf(" %d",i);
	         }
         }
         printf("\n");
       }
     }
    return 0;  
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值