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

求最短路径的最长路,直接上floyd果断超时。改用bfs,卡过。

#include <stdio.h>
#include <map>
#include <string>
#include <queue>
#include <iostream>
#define inf 0x3f3f3f3f
#define MAXN 1005
using namespace std; int cnt;
map< string,int > M;
int head[MAXN];
bool visited[MAXN];
int dist[MAXN][MAXN];
struct EdgeNode{
int to;
int next;
}edges[MAXN*]; void addedge(int u, int v){
edges[cnt].to=v;
edges[cnt].next=head[u];
head[u]=cnt++;
} void bfs(int u){
queue<int> Q;
Q.push(u);
dist[u][u]=;
memset(visited,,sizeof(visited));
visited[u]=;
while( !Q.empty() ){
int now=Q.front();
Q.pop();
for(int i=head[now]; i!=-; i=edges[i].next){
int to=edges[i].to;
if(!visited[to]){
dist[u][to]=dist[u][now]+;
Q.push(to);
visited[to]=;
}
}
}
} int main(int argc, char *argv[])
{
int n,k;
string a,b;
while(scanf("%d",&n)!=EOF && n){
M.clear();
memset(head,-,sizeof(head));
for(int i=; i<=n; i++){
for(int j=i+; j<=n; j++){
dist[i][j]=dist[j][i]=inf;
}
}
for(int i=; i<=n; i++){
string name;
cin>>name;
M[name]=i;
}
cnt=;
scanf("%d",&k);
while(k--){
cin>>a>>b;
addedge(M[a],M[b]);
addedge(M[b],M[a]);
}
for(int i=; i<=n; i++)bfs(i);
int ans=;
for(int i=; i<=n; i++){
for(int j=i+; j<=n; j++){
if(dist[i][j]>ans)
ans=dist[i][j];
}
}
if(ans==inf)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}

最新文章

  1. [入门级] visual studio 2010 mvc4开发,用ibatis作为数据库访问媒介(一)
  2. DotNet程序集解析
  3. Java并发编程底层实现原理 - volatile
  4. extjs4_msg
  5. Bootstrap 表格和按钮
  6. python发布文件(windows)
  7. laravel Authentication and Security
  8. c#泛型方法重载
  9. iis10 HTTP 错误 500.19 - Internal Server Error
  10. Algorithm --&gt; 小于N的正整数含有1的个数
  11. Spark集群术语
  12. spring-security权限管理学习目标
  13. Python练习三
  14. 如何用MarsEdit快速插入源代码
  15. 〖Linux〗Shell十进制数值转换十六进制
  16. C#.net实现图片上传功能
  17. USACO 2016 February Contest, Gold解题报告
  18. WPF:理解TileBrush(ImageBrush,DrawingBrush和VisualBrush)
  19. (数据科学学习手札22)主成分分析法在Python与R中的基本功能实现
  20. 设计高效SQL: 一种视觉的方法

热门文章

  1. ComicEnhancerPro 系列教程十七:二值化图像去毛刺
  2. IIS将http强转为https(重定向和重写)
  3. CentOS 网络操作
  4. 《C#多线程编程实战》2.5 AutoResetEvent
  5. NSTimeZone时区
  6. The method identifyUser(Arrays.asList(&quot;group001&quot;), String, new HashMap&lt;&gt;()) is undefined for the type AipFace
  7. 洛谷P4557 [JSOI2018]战争(闵可夫斯基和+凸包)
  8. P3235 [HNOI2014]江南乐
  9. nRF51822外设应用[2]:GPIOTE的应用-按键检测
  10. CBoard 视图中的拖拽实现