HDU 4460 Friend Chains 第37届ACM/ICPC杭州赛区题目 (bfs求最短路,求两两之间最短路的最大值)
2012-11-08 20:19
676 查看
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15 Accepted Submission(s): 10
[align=left]Problem Description[/align]
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 .
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, print the minimum value k in one line.
If the value of k is infinite, then print -1 instead.
[align=left]Sample Input[/align]
3 XXX YYY ZZZ 2 XXX YYY YYY ZZZ 0
[align=left]Sample Output[/align]
2
[align=left]Source[/align]
2012 Asia Hangzhou Regional Contest
明显是求两两之间的最短路。
Floyed,O(n^3)明显是超时的。
做n次bfs求最短路。O(n^2).
写得速度不快,险过了~~~~
//============================================================================ // Name : HDU4460.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <map> #include <math.h> #include <queue> #include <vector> using namespace std; const int MAXN=1010; const int INF=0x3f3f3f3f; int dis[MAXN][MAXN]; bool used[MAXN]; vector<int>vec[MAXN]; queue<int>que; void dfs(int i) { memset(used,false,sizeof(used)); dis[i][i]=0; used[i]=true; que.push(i); while(!que.empty()) { int t=que.front(); que.pop(); int m=vec[t].size(); for(int j=0;j<m;j++) { int v=vec[t][j]; if(used[v])continue; dis[i][v]=dis[i][t]+1; que.push(v); used[v]=true; } } } map<string,int>mp; int main() { string str; string str2; int n,m; while(scanf("%d",&n)==1 && n) { mp.clear(); for(int i=0;i<n;i++) { cin>>str; mp[str]=i; } for(int i=0;i<n;i++) { dis[i][i]=0; for(int j=i+1;j<n;j++) dis[i][j]=dis[j][i]=INF; } scanf("%d",&m); for(int i=0;i<n;i++)vec[i].clear(); while(m--) { cin>>str>>str2; int t1=mp[str]; int t2=mp[str2]; vec[t1].push_back(t2); vec[t2].push_back(t1); } for(int i=0;i<n;i++)dfs(i); int ans=0; for(int i=0;i<n;i++) for(int j=i+1;j<n;j++) ans=max(ans,dis[i][j]); if(ans==INF)ans=-1; printf("%d\n",ans); } return 0; }
相关文章推荐
- hdu 4460 第37届ACM/ICPC杭州赛区H题 STL+bfs
- HDU 4455 Substrings 第37届ACM/ICPC 杭州赛区现场赛 C题 (DP)
- HDU 4454 Stealing a Cake 第37届ACM/ICPC 杭州赛区 B题(三分法)
- HDU 4461 The Power of Xiangqi 第37届ACM/ICPC杭州赛区I题 (水题)
- hdu 4584 building-bridge 暴力的题,怎么暴力怎么干。。2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- HDU 4417 Super Mario 第37届ACM/ICPC 杭州赛区网络赛1008题(树状数组或者线段树)
- hdu 4462 第37届ACM/ICPC 杭州赛区 J题
- HDU 4463 Outlets 第37届ACM/ICPC 杭州赛区K题 (裸最小生成树)
- hdu 4585 Shaolin 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- HDU 4455 Substrings 第37届ACM/ICPC 杭州赛区现场赛 C题 (DP)
- hdu 4461 第37届ACM/ICPC杭州赛区I题
- HDU 4462 Scaring the Birds 第37届ACM/ICPC 杭州赛区 J题 (简单题)
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
- HDU 4438 Hunters 第37届ACM/ICPC 天津赛区现场赛H题(超级水的题目)
- Substrings 第37届ACM/ICPC 杭州赛区现场赛C题(hdu 4455)
- hdu 4576 robot 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现-1001-robot
- hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树
- hdu 4585 Shaolin 2013 ACM-ICPC杭州赛区全国邀请赛——题目重现
- hdu 5899 oasis in desert(acm/icpc 沈阳赛区网络赛,Floyd+二分图判定+最大匹配,好难啊)
- HDU 5876 Sparse Graph(bfs求解补图中的单源最短路)——2016 ACM/ICPC Asia Regional Dalian Online