hdu4460(BFS)

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1860    Accepted Submission(s): 648


Problem Description
For a group of people, there is an idea that everyone is equals to or less than 6 steps away from any other person in the group, by way of introduction. So that a chain of "a friend of a friend" can be made to connect any 2 persons and it contains no more than 7 persons.
For example, if XXX is YYY’s friend and YYY is ZZZ’s friend, but XXX is not ZZZ's friend, then there is a friend chain of length 2 between XXX and ZZZ. The length of a friend chain is one less than the number of persons in the chain.
Note that if XXX is YYY’s friend, then YYY is XXX’s friend. Give the group of people and the friend relationship between them. You want to know the minimum value k, which for any two persons in the group, there is a friend chain connecting them and the chain's length is no more than k .
 

Input
There are multiple cases.
For each case, there is an integer N (2<= N <= 1000) which represents the number of people in the group.
Each of the next N lines contains a string which represents the name of one people. The string consists of alphabet letters and the length of it is no more than 10.
Then there is a number M (0<= M <= 10000) which represents the number of friend relationships in the group.
Each of the next M lines contains two names which are separated by a space ,and they are friends.
Input ends with N = 0.
 

Output
For each case, print the minimum value k in one line.
If the value of k is infinite, then print -1 instead.
 

Sample Input
  
  
3 XXX YYY ZZZ 2 XXX YYY YYY ZZZ 0
 

Sample Output
  
  
2
 

Source
        本题要求通过k朋友过渡认识有所人的最小的k.
        可以想到本题是求所有顶点间的最小距离的最大值。 2<=N<=1000,time limite=1000Ms,即说明一般情况下时间复杂度不应超过O(N^2),故基本排除Floyd算法。我们知道BFS的时间复杂度为O(N),我们可以利用变形的BFS求得单源最短路径,然后求1-n的所有顶点的单源最短路径,时间复杂度为O(N^2)。中间有些剪枝,用库函数的queue队列超时,得自己写数组模仿队列。还有可以将map容器写成线段树。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
//#include<queue>
using namespace std;

const int MAXN=1000+100;
const int INF = 0x3f3f3f3f;


int head[MAXN];
int dis[MAXN];
bool visited[MAXN];
int qq[MAXN];
struct node
{
	int to;
	int next;
}Edg[20000+100];
int n,m,tot;
map<string,int>g;

void init()
{
	tot=0;
	memset(head,-1,sizeof(head));
}

void add(int a,int b)
{
	Edg[tot].to=b;
	Edg[tot].next=head[a];
	head[a]=tot++;
}

int BFS(int s)
{
	//queue<int>qq;
	//qq.push(s);
	int front,rear;
	front=rear=0;	
	qq[front++]=s;
	int now,i,to;
	for(i=1;i<=n;i++)
	{
		if(i!=s)dis[i]=INF;
		else dis[i]=0;
	}
	visited[s]=true;
	while(rear<front)
	{
		//now=qq.front();
		//qq.pop();
		now=qq[rear++];
		for(i=head[now];i!=-1;i=Edg[i].next)
		{
			to=Edg[i].to;
			if(to==now||visited[to])continue;
			dis[to]=dis[now]+1;
			//qq.push(to);
			qq[front++]=to;
			visited[to]=true;
		}
	}
	int ret=-1;
	for(i=1;i<=n;i++)
	{
		ret=max(ret,dis[i]);
		if(ret==INF)return ret;
	}
	return ret;
}

void Solve()
{
	int i,ans=-1;
	for(i=1;i<=n;i++)
	{
		memset(visited,0,sizeof(visited));
		ans=max(ans,BFS(i));
		if(ans==INF)break;
	}
	if(ans==INF)
		printf("-1\n");
	else printf("%d\n",ans);
}

int main()
{
	int i;
	string st,ed;
	while(scanf("%d",&n),n)
	{
		g.clear();
		for(i=1;i<=n;i++)
		{
			cin>>st;
			g[st]=i;
		}
		scanf("%d",&m);
		init();
		for(i=1;i<=m;i++)
		{
			cin>>st>>ed;
			add(g[st],g[ed]);
			add(g[ed],g[st]);
		}
		Solve();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值