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 ;
}

HDU 4460 Friend Chains的更多相关文章

  1. HDU 4460 Friend Chains(map + spfa)

    Friend Chains Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  2. HDU 4460 Friend Chains --BFS

    题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...

  3. HDU 4460 Friend Chains (BFS,最长路径)

    题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...

  4. HDU 4460

    http://acm.hdu.edu.cn/showproblem.php?pid=4460 水题一道,oj时间卡的非常奇怪,把spfa的queue开成全局卡线过,别的全挂了,迪杰斯特拉的手写堆都超时 ...

  5. HDOJ 4460 Friend Chains 图的最长路

    类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点. 然后再做一次最短路 Friend Chains Time Limit: 2000/1000 MS (Java/Others)    ...

  6. hdu 4460 第37届ACM/ICPC杭州赛区H题 STL+bfs

    题意:一些小伙伴之间有朋友关系,比如a和b是朋友,b和c是朋友,a和c不是朋友,则a和c之间存在朋友链,且大小为2,给出一些关系,求出这些关系中最大的链是多少? 求最短路的最大距离 #include& ...

  7. HDU 3746:Cyclic Nacklace

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. hdu 3487 Play with Chain

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a c ...

  9. hdu 3746 Cyclic Nacklace(kmp最小循环节)

    Problem Description CC always becomes very depressed at the end of this month, he has checked his cr ...

随机推荐

  1. repeater+aspnetpager 组合分页

    页面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TF_Product. ...

  2. Log--检查各数据库日志的使用情况

    -- Recovery model, log reuse wait description, log file size,-- log usage size and compatibility lev ...

  3. C# 密封

    到目前位置所说的都是让类如何如何进行继承啊 ,重写啊,巴不得类有十多个继承,超级多的重写. 但是,今天我们来说说不允许继承和不允许重写! 这个不允许继承是包括类和方法. 这种情况好比: 爸爸有私房钱, ...

  4. 20165219 2017-2018-2 《Java程序设计》第8周学习总结

    20165219 2017-2018-2 <Java程序设计>第8周学习总结 教材学习内容总结 进程与线程 线程是比进程更小的单位:线程间可以共享进程中的某些内存单元 java的多线机制 ...

  5. 如何处理html中的内联元素之间水平空隙

    写HTML时把需要紧挨着的内联元素写在一行,设置其父容器的font-size为0,再设置内联元素的字体大小,例如: <!DOCTYPE html> <html lang=" ...

  6. zbar

    源码下载链接:http://sourceforge.net/projects/zbar

  7. 把display 属性改为block样式变化问题

    可能的值 值 描述 none 此元素不会被显示. block 此元素将显示为块级元素,此元素前后会带有换行符. inline 默认.此元素会被显示为内联元素,元素前后没有换行符. inline-blo ...

  8. 按钮重复点击问题 UIbutton

    .h #import <UIKit/UIKit.h> #import <objc/runtime.h> @interface UIControl (XY) @property ...

  9. day--39-MySQL的多表查询

    多表查询一:表的基本介绍 可以参考:https://www.cnblogs.com/cdf-opensource-007/p/6517627.html 建立一个员工表信息表和一个部门表,每个员工都对应 ...

  10. tornado 08 数据库-ORM-SQLAlchemy-表关系和简单登录注册

    tornado 08 数据库-ORM-SQLAlchemy-表关系和简单登录注册 引言 #在数据库,所谓表关系,只是人为认为的添加上去的表与表之间的关系,只是逻辑上认为的关系,实际上数据库里面的表之间 ...