日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

HDU 4460 Friend Chains(map + spfa)

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

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


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 国产在线一区二区三区 | 亚洲精品在线免费 | 成人欧美一区二区 | 欧美黄色免费片 | 国产精品乱码一区二区三区 | 一区二区精品在线 | 久久久影视| 黄色网址在线播放 | www.日韩视频 | 夜夜精品视频一区二区 | 日本视频免费看 | 激情一区二区三区 | 免费成人av在线 | 国产欧美日韩在线 | 免费福利在线观看 | 中文字幕第九页 | 欧美插插插 | 岛国午夜视频 | 污网站免费观看 | 亚洲欧美日韩国产综合 | 成人午夜影院 | 黄色在线观看视频网站 | 免费a网| 在线免费a视频 | 欧美在线观看一区 | 国产一页| 日韩精品中文字幕一区二区三区 | 婷婷久 | 国产一级在线 | 亚洲综合电影 | 午夜电影在线观看 | 精品电影一区二区三区 | 91久久国产综合久久91精品网站 | 国产一区二区三区高清 | 欧美日韩免费视频 | 欧美日韩第一区 | 成人在线视频观看 | 日韩av在线高清 | 日韩一区二区三区在线视频 | 久久精品一区 | 国产精品一区二区在线观看网站 |