#include"iostream"
#include"stdio.h"
#include"stdlib.h"
#include"queue"
#include"map"
#define INF 0x7fffffff
using namespace std;
typedef struct
{
int next;
int v;
int len;
}edge;
edge E[200020];
int n,NE;
int dist[20000],inqueue[20000],head[20000],hash[20000];
void Addedge(int a,int b,int len)
{
E[NE].next=head[a];
E[NE].v=b;
E[NE].len=len;
head[a]=NE++;
}
int relax(int from,int to,int len)
{
if(dist[from]!=INF&&dist[to]>dist[from]+len)
{
dist[to]=dist[from]+len;
if(dist[to]>hash[to])
hash[to]=dist[to];
return 1;}
return 0;
}
void SPFA(int begin)
{
int i,h,v,maxx;
for(i=0;i<=10000;i++)
{ inqueue[i]=0; dist[i]=INF;}
queue<int>q;
q.push(begin);
dist[begin]=1;
if(hash[begin]==0)
hash[begin]=1;
inqueue[begin]=1;
while(!q.empty())
{
h=q.front();
q.pop();
inqueue[h]=0;
for(i=head[h];i!=-1;i=E[i].next)
{
v=E[i].v;
if(relax(h,v,E[i].len)&&!inqueue[v])
{ inqueue[v]=1; q.push(v);}
}
}
}
int main()
{
int t,i,j,m,from,to,k,x,max_edge,minn,ans,maxx;
int pp[20000],s;
scanf("%d",&t);
while(t--)
{
NE=0;
scanf("%d%d",&n,&m);
for(i=0;i<=10000;i++)
{ head[i]=-1; hash[i]=0;}
for(i=1;i<=n;i++)
{
scanf("%d%d",&from,&k);
for(j=1;j<=k;j++)
{
scanf("%d",&to);
Addedge(from,to,1);
Addedge(to,from,1);
}
}
for(i=1;i<=m;i++)
{
scanf("%d",&k);
for(j=1;j<=k;j++)
{
scanf("%d",&s);
SPFA(s);
}
}
int maxx=INF;
for(i=1;i<=10000;i++)
if(hash[i]!=0&&hash[i]<maxx)
{maxx=hash[i]; ans=i;}
printf("%d %d\n",maxx,ans);
}
}
HDU 2377 Bus Pass
最新推荐文章于 2024-08-19 18:41:12 发布