HDU-4460 Friend Chains(BFS&权为1所有最短路的最大值)

题目:

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

题意:

多组输入,第一行给一个n然后输入n个结点(字符串,用map转为int),然后输入一个m代表m条路,然后输入m组数据代表两个点直接有一条路,所有路权值都为1,输出任意两个结点最短路之中的最大值,如果有两个结点之间没有路,那么输出-1。

分析:

对每个点bfs搜最短路,用dis[i][j]代表i结点到j结点的距离,刚开始全部初始化为inf,dis[i][i] = 0。对每个点搜完后对所有最短路遍历,记录最大值ans,如果ans为inf就输出-1,否则输出ans。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
const int inf = 0x3f3f3f3f;
int vis[maxn],dis[maxn][maxn],ans = 0;
vector<int> v[maxn];
void bfs(int u){
	dis[u][u] = 0;
	memset(vis,0,sizeof vis);
	queue<int> q;
	vis[u] = 1;
	q.push(u);
	while (!q.empty()){
		int tmp = q.front();
		q.pop();
		int len = v[tmp].size();
		for (int i = 0; i < len; i++){
			int to = v[tmp][i];
			if (!vis[to]){
				dis[u][to] = dis[u][tmp] + 1;
				vis[to] = 1;
				q.push(to);
			}
		}
	}
}
int main(void){
	int n,m;
	map<string,int> mp;
	char str1[15],str2[15];
	while (scanf("%d",&n),n){
		for (int i = 0; i < n; i++)v[i].clear();
		ans = 0;
		for (int i = 1; i <= n; i++){
			scanf("%s",str1);
			mp[str1] = i;
		}
		scanf("%d",&m);
		for (int i = 1; i <= m; i++){
			scanf("%s%s",str1,str2);
			v[mp[str1]].push_back(mp[str2]);
			v[mp[str2]].push_back(mp[str1]);
		}
		for (int i = 1; i <= n; i++){
			for (int j = i+1; j <= n; j++){
				dis[i][j] = dis[j][i] = inf;
			}
		}
		for (int i = 1; i <= n; i++)
		bfs(i);
		for (int i = 1; i <= n; i++){
			for (int j = i+1; j <= n; j++){
				if (dis[i][j] > ans) ans = dis[i][j];
			}
		}			
		printf("%d
",ans==inf?-1:ans);
	}
	return 0;
}

需要注意点:

此题用链式前向星TLE,需要改为vector存边,原因不详

此题还可以用类似求树的直径的做法求解(未尝试,有待确认),速度更快

原文地址:https://www.cnblogs.com/hznudreamer/p/12375753.html