HDU 4460 Friend Chains(最短路)

本文探讨了六度分隔理论,并通过SPFA算法实现任意两点间的最短路径计算,以此来验证该理论的有效性。输入一组人员名单及其朋友关系,输出任意两人间最短的朋友链长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:任意点对之间的最短路


#include<bits/stdc++.h>
using namespace std;
const int maxn =1005;
#define INF 1e9
vector<int>e[maxn];
int d[maxn][maxn];
map<string,int>mp;
int vis[maxn];
void spfa(int x)
{
	memset(vis,0,sizeof(vis));
	d[x][x]=0;
	queue<int>q;
    vis[x]=1;
	q.push(x);
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		//vis[u]=0;
		for(int i = 0;i<e[u].size();i++)
		{
			int v = e[u][i];
			if(!vis[v])
			{
				d[x][v]=d[x][u]+1;
                q.push(v);
				vis[v]=1;
			}
		}
	}
}
int main()
{
	int n;
    while(scanf("%d",&n)!=EOF && n)
	{
        mp.clear();
		for(int i = 0;i<=n;i++)
			e[i].clear();
		int cnt = 1;
		for(int i = 1;i<=n;i++)
		{
			string temp;
			cin >> temp;
			mp[temp]=cnt++;
		}
		int m;
		for(int i = 1;i<=n;i++)
		{
			d[i][i]=0;
			for(int j = i+1;j<=n;j++)
				d[i][j]=d[j][i]=INF;
		}
		scanf("%d",&m);
		for(int i = 0;i<m;i++)
		{
            string s1,s2;
			cin >> s1 >> s2;
			e[mp[s1]].push_back(mp[s2]);
			e[mp[s2]].push_back(mp[s1]);
		}
		for(int i = 1;i<=n;i++)
			spfa(i);
		int ans = 0;
        for(int i = 1;i<=n;i++)
			for(int j = i+1;j<=n;j++)
				ans = max(ans,d[i][j]);
		if(ans==INF)
			ans=-1;
		printf("%d\n",ans);
	}
}


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
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值