HDU 4460 Friend Chains(map + spfa)
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 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
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
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=;
int n,m,maxn;
map<string,int> mp;
vector<int> s[];
int dis[];
bool vis[];
void spfa(int st)
{
for(int i=;i<=n;i++) dis[i]=inf,vis[i]=;
queue<int> Q;
Q.push(st);
vis[st]=;
dis[st]=;
while(!Q.empty())
{
int u=Q.front();
vis[u]=;
Q.pop();
for(int i=;i<s[u].size();i++)
{
if (dis[s[u][i]]<=dis[u]+) continue;
dis[s[u][i]]=dis[u]+;
maxn=dis[s[u][i]]>maxn?dis[s[u][i]]:maxn;
if (!vis[s[u][i]])
{
vis[s[u][i]]=;
Q.push(s[u][i]);
}
}
}
return;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)
{
char ch[];
scanf("%s",&ch);
mp[ch]=i;
s[i].clear();
} scanf("%d",&m);
for(int i=;i<=m;i++)
{
char ch1[],ch2[];
scanf("%s %s",&ch1,&ch2);
s[mp[ch1]].push_back(mp[ch2]);
s[mp[ch2]].push_back(mp[ch1]);
}
int ans=;
for(int i=;i<=n;i++)
{
maxn=;
spfa(i);
if (maxn==) ans=-;//本来想直接退出,输出-1,但是考虑到现在以i为起点的最短路径计算出来的并不一定是最长的长度,可能是最短路径中的一个中间点
if (ans>= && maxn>ans) ans=maxn;
} printf("%d\n",ans);
}
return ;
}
HDU 4460 Friend Chains(map + spfa)的更多相关文章
- HDU 4460 Friend Chains --BFS
题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...
- HDU 4460 Friend Chains (BFS,最长路径)
题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...
- HDU 4460 Friend Chains
Problem Description For a group of people, there is an idea that everyone is equals to or less than ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
- HDU 4460
http://acm.hdu.edu.cn/showproblem.php?pid=4460 水题一道,oj时间卡的非常奇怪,把spfa的queue开成全局卡线过,别的全挂了,迪杰斯特拉的手写堆都超时 ...
- HDOJ 4460 Friend Chains 图的最长路
类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点. 然后再做一次最短路 Friend Chains Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1874 畅通工程续 SPFA || dijkstra||floyd
http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目大意: 给你一些点,让你求S到T的最短路径. 我只是来练习一下SPFA的 dijkstra+邻接矩阵 ...
- hdu 2112 (最短路+map)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- POJ 3481 & HDU 1908 Double Queue (map运用)
题目链接: PKU:http://poj.org/problem?id=3481 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=1908 Descript ...
随机推荐
- mysql字段更改操作命令
1) 加索引 mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]); 例子: mysql> alter table employee ...
- CentOS 6下的VPN搭建
PPTP 全称为 Point to Point Tunneling Protocol — 点到点隧道协议,是VPN协议中的一种.虚拟专用网(VPN)被定义为通过一个公用网络(通常是因特网)建立一个临时 ...
- 【Python爬虫实战--3】html写正则表达式
以下是要爬虫的html内容: <div class="article block untagged mb15" id='qiushi_tag_113452216'> & ...
- STM32F407的串口采用DMA收发数据
源:STM32F407的串口采用DMA收发数据
- 关于PS的一些总结
1.设计给的图,单独用里边的个别图层 打开图 — 新建一个图层(ctrl+n) — (点开上面的窗口排列-垂直排列,左下边下边自动选择改成图层)—选择移动工具,选中要移动的图层,拉到新建文件夹中. ...
- iOS UIApplication 里面各const实际用意
//后台通知:屏幕操作通知等等 UIKIT_EXTERN NSString *const UIApplicationDidEnterBackgroundNotification NS_AV ...
- jquery操作HTML5 的data-*的用法实例分享
.mm{width:256px; height:200px;} .mm[data-name='张含韵']{background:url(http://image.zhangxinxu.com/imag ...
- 第十三节,基本数据类型,数字int字符串str
基本数据类型 数字 int 字符串 str 布尔值 bool 列表 list 元组 tuple 字典 dict 数据类型关系图 查看一个对象的类 如:如查看对象变量a是什么类 用到函 ...
- 二分法习题HDU2199
AC代码 : #include<iostream>#include<cmath>using namespace std;double y;double f(double n){ ...
- Ubuntu14.04+cuda 7.5+cudnn_v4+tensorflow安装
系统环境:Ubuntu14.04 64位.Windows7 64位 双系统 CUDA 版本: 7.5 总结一下,我的安装建议是: 一定要下一份CUDA官方的安装文档,按照它的步骤一步步慢慢来,不可偷懒 ...