多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > php教程 > HDU 4460 Friend Chains(map + spfa)

HDU 4460 Friend Chains(map + spfa)

來源:程序員人生   發布時間:2014-12-14 08:33:58 閱讀次數:2978次

題目大意:給你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); } }


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 亚洲三级小视频 | 免费观看欧美一级毛片 | 亚洲国产一级毛片 | 一区二区三区免费视频播放器 | 久久一品道 | 国产在线精品一区二区夜色 | 亚洲国产精品综合久久20 | 亚洲成综合人影院在院播放 | 亚洲男女一区二区三区出奶水了 | 视频在线免费 | 欧美性猛交xxxx乱大交高清 | 精品在线免费观看视频 | 成人在线观看免费爱爱 | 欧美日韩亚洲精品一区二区 | 国产成人免费永久播放视频平台 | 欧美性大战久久久久久久蜜桃 | 99heicom视频 | 国产精品久久亚洲不卡4k岛国 | 欧美五月 | 真实男女xx00动态视频120秒 | 亚洲精品国自产拍影院 | 中文字幕精品视频在线 | free性欧美人与牛 | 欧美国产综合视频在线观看 | 一级做a爰片性色毛片新版的 | 在线观看中文字幕码2022 | 欧洲一级毛片免费 | 日韩大片免费观看 | 午夜爱爱网 | 日韩一级在线 | 欧美videosfree性喷潮 | 黑人又大又粗好爽好猛视频 | 名优写真一区二区在线 | 日韩va| 欧美曰逼| 免费精品美女久久久久久久久久 | 久久色网站| 最近韩国中文字幕更新 | 免费人成网ww44kk44 | 五月天国产 | 午夜dj免费视频观看在线播放 |