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. 桥梁(Bridge)模式

    桥梁(Bridge)模式:桥梁模式是一个非常有用的模式,也是比较复杂的一个模式.熟悉这个模式对于理解面向对象的设计原则,包括"开-闭"原则(OCP)以及组合/聚合复用原则(CARP ...

  2. VS2010-安装包制作过程图解

    最近做了winform相关程序,开始总结制作安装包过程. 1.首先在打开 VS2010    =>新建=>项目 2.创建一个安装项目  Setup1 在“目标计算机上的文件系统”下我们看见 ...

  3. C# 密封

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

  4. 《Java多线程编程实战指南+设计模式篇》笔记

    线程的监视:工具:jvisualvm.exe 命令:jstack PID 原子性: volatile关键字: 显示锁:人为实现的程序员可控制的锁,包括synchronized和Lock下的实现类: 线 ...

  5. 649. Dota2 Senate

    In the world of Dota2, there are two parties: the Radiant and the Dire. The Dota2 senate consists of ...

  6. python3入门之字典

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 本节主要介绍字典,字典也成映射,时python中唯一内建的映射类型.更多详细请点击readmore.下面附有之前的文章: python入门之字符串 ...

  7. 神奇的Form表单

    今天坐标单上传,提交的按钮使用了<button>,发现不论怎么写ajax和设置form表单,都会刷新页面,百思不得解,然后偶然间把<button>变成<input typ ...

  8. JS 命名规范

    JS的命名规则和规范 规则 - 必须遵守的,不遵守会报错 由字母.数字.下划线.$符号组成,不能以数字开头 不能是关键字和保留字,例如:for.while. 区分大小写 规范 - 建议遵守的,不遵守不 ...

  9. EA添加时序图

    在项目浏览器的空白处右击 http://blog.csdn.net/craftsman1970/article/details/70877530 不同于大部分面向对象或者UML的书籍,在讨论完类图/对 ...

  10. springMVC上传功能(单文件和多文件上传)

    单文件和多文件上传 首先在xxx-select.xml里面配置上传的大小和编码 <bean id="multipartResolver" class="org.sp ...