Friend Chains

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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
/*
题意:给出一个无向图,若联通则输出任意一对点之间最短路径中的最大值,
若有孤立一点,则输出-1。
*/
#include <iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int inf=10000;
int n,m,maxn;
map<string,int> mp;
vector<int> s[1005];
int dis[1005];
bool vis[1005];
void spfa(int st)
{for(int i=1;i<=n;i++) dis[i]=inf,vis[i]=0;queue<int> Q;Q.push(st);vis[st]=1;dis[st]=0;while(!Q.empty()){int u=Q.front();vis[u]=0;Q.pop();for(int i=0;i<s[u].size();i++){if (dis[s[u][i]]<=dis[u]+1) continue;dis[s[u][i]]=dis[u]+1;maxn=dis[s[u][i]]>maxn?dis[s[u][i]]:maxn;if (!vis[s[u][i]]){vis[s[u][i]]=1;Q.push(s[u][i]);}}}return;
}
int main()
{while(scanf("%d",&n) && n){for(int i=1;i<=n;i++){char ch[15];scanf("%s",&ch);mp[ch]=i;s[i].clear();}scanf("%d",&m);for(int i=1;i<=m;i++){char ch1[15],ch2[15];scanf("%s %s",&ch1,&ch2);s[mp[ch1]].push_back(mp[ch2]);s[mp[ch2]].push_back(mp[ch1]);}int ans=0;for(int i=1;i<=n;i++){maxn=0;spfa(i);if (maxn==0) ans=-1;//本来想直接退出,输出-1,但是考虑到现在以i为起点的最短路径计算出来的并不一定是最长的长度,可能是最短路径中的一个中间点if (ans>=0 && maxn>ans) ans=maxn;}printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/stepping/p/5744762.html

HDU 4460 Friend Chains(map + spfa)相关推荐

  1. hdu 4460 friend chains spfa 最短路里面的最长路

    题意不再赘述... 连接http://acm.hdu.edu.cn/showproblem.php?pid=4460 此题直接郁闷致死.... 比赛的时候用的是floyd然后直接超时...当时闪过sp ...

  2. ACM学习历程—HDU 2112 HDU Today(map spfa 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

  3. HDU 2112 HDU Today (dijkstar + map)

    HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. hdu 1263 水果 (嵌套 map)

    水果 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...

  5. 最短路算法详解(Dijkstra/SPFA/Floyd)

    转自:http://blog.csdn.net/murmured/article/details/19281031 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短 ...

  6. WaWa的奇妙冒险(第三周集训自闭现场)

    第三周集训自闭现场(a* ida* dbfs真的好难) (一)wlacm例题记录 A-有重复元素的排列问题 (水题,但卡我到自闭) Input Output Sample Input Sample O ...

  7. Spring Boot 整合 Shiro

    虽然,直接用Spring Security和SpringBoot 进行"全家桶式"的合作是最好不过的,但现实总是欺负我们这些没办法决定架构类型的娃子. Apache Shiro 也 ...

  8. c语言随机产生三个大写字母,C语言编写的随机产生四则运算测试题

    题目:编写一个四则运算测试题的程序,要求每道题都要随机产生 解题思路: 1.编写测试题,且为30道,就要用到循环函数,因此想到用for()函数 2.随机产生两个数,就想到用rand()函数. 注:1. ...

  9. 超级账本hyperledger fabric第五集:共识排序及源码阅读

    一.共识机制 达成共识需要3个阶段,交易背书,交易排序,交易验证 交易背书:模拟的 交易排序:确定交易顺序,最终将排序好的交易打包区块分发 交易验证:区块存储前要进行一下交易验证 二.orderer节 ...

最新文章

  1. ACM: 畅通工程-并查集-解题报告
  2. Winform中通过NPOI导出Excel的三种方式(HSSFWorkbook,XSSFWorkbook,SXSSFWorkbook)附代码下载
  3. C#中数据库事务、存储过程基本用法
  4. @MapperScan和@ComponentScan使用问题
  5. 惠新宸php教程_百度PHP高级顾问惠新宸:PHP在百度的发展历程
  6. 【Elasticsearch】关于 Analyzers 的一切,第一部分
  7. navicat的字符集和排序规则
  8. win10子系统编译android,基于win10子系统ijkplayer全量编译(支持所有格式)流程
  9. vue cli脚手架项目利用webpack给生产环境和发布环境配置不同的接口地址或者不同的变量值。...
  10. EMNLP'21 Oral | 拓展你的视野!UCLA提出:地区多样性视觉常识推理
  11. 130242014021-田富钊-实验一
  12. Mysql 计划任务
  13. 如何使用String获取字符串中某一个字符后面的所有字符?
  14. handlersocket php,handlersocket安装配置
  15. 计算机英语单词怎么读语音,英语单词发音
  16. 理科女生计算机与会计学选择,我是一理科女生、在报考学校时候、选择会计还是会计电算化比较好呢?还有会计与审计、会计于统计核算....
  17. 已知销售额怎么计算成本_计算成本根据销售额怎么样推算出成本,举例, – 手机爱问...
  18. 阅读笔记0001之聊聊数据分析现状
  19. Qt编写地图综合应用55-海量点位标注
  20. python取出数组大于某值_Python替换NumPy数组中大于某个值的所有元素实例

热门文章

  1. 精读《Prisma 的使用》
  2. python批量添加qq好友_python实现QQ批量登录功能
  3. JMeter——》调整界面比例、字体大小
  4. Git连接GitHub仓库,同步上传图片及CSDN外链图片转存失败解决方案
  5. 小米路由器 不显示 连接设备连接到服务器,小米路由器隐藏网络后怎么连接
  6. 2019,向着迷茫的远方前行
  7. MPLAB X IDE安装与MPLAB XC8 Compiler环境配置
  8. float和double的精度
  9. SpringBoot 日志文件
  10. 【计算机毕业设计】java 微信小程序商城系统的设计与实现