HDU 4460 Friend Chains

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4460


Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5167    Accepted Submission(s): 1658



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

 

Recommend


思路:仔细阅读题意,可知是求各点到其他点的最短路的最大值。如果最大值为无穷大,输出-1,否则输出最大值。比赛时一开始用了O(n^2)的Dijkstra算法试试,T了。然后就想用O(mlogn)的Dijkstra算法试试,但是因为好久没写图论,结果照着lrj的模板敲了一下,误以为加边时,自动处理了,结果就因为少加一条反向边,GG了,555555。正解应该不是这个算法,这个算是飘过吧。详见代码。


附上AC代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int inf = 0x3f3f3f3f;
struct edge{
	int from, to, dis;
	edge(){}
	edge(int u, int v, int d): from(u), to(v), dis(d) {}
};
struct heap_node{
	int d, u;
	bool operator < (const heap_node & p) const {
		return d > p.d;
	}
};
struct Dijkstra{
	int n, m;
	vector<edge> edges;
	vector<int> g[maxn];
	bool vis[maxn];
	int d[maxn], p[maxn];

	void init(int n){
		this->n = n;
		for (int i=0; i<n; ++i)
			g[i].clear();
		edges.clear();
	}

	void add_edge(int from, int to, int dis){
		edges.push_back(edge(from, to, dis));
		m = edges.size();
		g[from].push_back(m-1);
	}

	void dijkstra(int s){
		priority_queue<heap_node> q;
		for (int i=0; i<n; ++i)
			d[i] = inf;
		d[s] = 0;
		memset(vis, false, sizeof(vis));
		q.push((heap_node){0, s});
		while (!q.empty()){
			heap_node x = q.top();
			q.pop();
			int u = x.u;
			if (vis[u])
				continue;
			vis[u] = true;
			for (int i=0; i<g[u].size(); ++i){
				edge & e = edges[g[u][i]];
				if (d[e.to] > d[u]+e.dis){
					d[e.to] = d[u]+e.dis;
					p[e.to] = g[u][i];
					q.push((heap_node){d[e.to], e.to});
				}
			}
		}
	}
} chain;
int n, m;
map<string, int> name;
char str1[15], str2[15];

int main(){
	while (~scanf("%d", &n) && n){
		chain.init(n);
		name.clear();
		for (int i=0; i<n; ++i){
			scanf("%s", str1);
			name[str1] = i;
		}
		scanf("%d", &m);
		for (int i=0; i<m; ++i){
			scanf("%s%s", str1, str2);
			int u=name[str1], v=name[str2];
			chain.add_edge(u, v, 1);
			chain.add_edge(v, u, 1);
		}
		int ans = 0;
		for (int i=0; i<n; ++i){
			chain.dijkstra(i);
			for (int j=0; j<n; ++j)
				ans = max(ans, chain.d[j]);
			if (ans == inf)
				break;
		}
		printf("%d\n", ans==inf ? -1 : ans);
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值