国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > HDU 4460 Friend Chains(map + spfa)

HDU 4460 Friend Chains(map + spfa)

来源:程序员人生   发布时间:2014-12-14 08:33:58 阅读次数:2815次

题目大意:给你1个朋友之间的关系让你判断所有人之间的朋友“链”的最大长度,如果大于7就不可以,否则输出最大值。

枚举n个点进行n次spfa。

Friend Chains

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


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 ⑴ instead.
 

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

Sample Output
2
 

Source
2012 Asia Hangzhou Regional Contest
 

#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <time.h> #include <stack> #include <map> #include <set> #define eps 1e⑻ ///#define LL long long #define LL __int64 #define INF 0x3f3f3f #define PI 3.1415926535898 #define mod 1000000007 using namespace std; const int maxn = 1010; map<string, int> mp; string str; string str1; struct node { int v; int w; int next; }f[maxn*maxn/2]; int head[maxn*maxn/2]; int cnt; void init() { cnt = 0; memset(head, ⑴, sizeof(head)); } void add(int u, int v, int w) { f[cnt].v = v; f[cnt].w = w; f[cnt].next = head[u]; head[u] = cnt++; f[cnt].v = u; f[cnt].w = w; f[cnt].next = head[v]; head[v] = cnt++; } int spfa(int x, int n) { bool vis[maxn]; int dis[maxn]; queue<int>que; for(int i = 1; i <= n; i++) { dis[i] = INF; vis[i] = false; } dis[x] = 0; vis[x] = true; que.push(x); while(!que.empty()) { int sx = que.front(); que.pop(); vis[sx] = false; for(int i = head[sx]; i != ⑴; i = f[i].next) { int v = f[i].v; if(dis[v] > dis[sx]+f[i].w) { dis[v] = dis[sx]+f[i].w; if(!vis[v]) { vis[v] = true; que.push(v); } } } } int Max = 0; for(int i = 1; i <= n; i++) { if(!dis[i]) continue; Max = max(Max, dis[i]); } return Max; } vector<int>g[maxn]; bool sta[maxn]; void dfs(int x, int fa) { sta[x] = 1; for(int i = head[x]; i != ⑴; i = f[i].next) { int sx = f[i].v; if(sta[sx] || sx == fa) continue; dfs(sx, x); } } int main() { int n; while(~scanf("%d",&n) && n) { for(int i = 1; i <= n; i++) { cin >>str; mp[str] = i; } int m; scanf("%d",&m); int x, y; init(); for(int i = 0; i < m; i++) { cin >>str>>str1; x = mp[str]; y = mp[str1]; add(x, y, 1); } memset(sta, false, sizeof(sta)); dfs(1, ⑴); int xflag = 0; for(int i = 1; i <= n; i++) { if(sta[i]) continue; xflag = 1; break; } if(xflag) { puts("⑴"); continue; } int flag = 0; int Max = 0; for(int i = 1; i <= n; i++) { int sx = spfa(i, n); if(sx >= 7) { flag = 1; break; } Max = max(Max, sx); } if(flag) { puts("⑴"); continue; } printf("%d ",Max); } }


生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生