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 ...
随机推荐
- webapi中使用Route标签
Prior to Web API 2, the Web API project templates generated code like this: protected void Applicati ...
- COM与.NET程序集导出和部署COM组件
为了分布式和多客户端调用我们还需要将写好的COM组件发布到一台服务器上.这里我们将组件部署到操作系统的COM+应用程序中去.如果没此需要就可以导出后,在C++环境中调用COM了. 第一步:导出COM组 ...
- linux内核驱动——从helloworld开始
学习编程第一个都是学习hello world程序,学习内核驱动自然也不例外,我也是!本文整理了网上的一些资料以及加上自己的一些心得体会,希望对初学者有帮助,可别小看这个简单的hello world,本 ...
- HDU 5543 Pick The Sticks
背包变形.与普通的背包问题不同的是:允许有两个物品可以花费减半. 因此加一维即可,dp[i][j][k]表示前i个物品,有j个花费减半了,总花费为k的情况下的最优解. #pragma comment( ...
- 数据结构——求单向链表的倒数第K个节点
首先,对于链表来说,我们不能像数组一样直接访问,所以我们想到要求倒数第K个节点首先要知道最后一个节点. 然后从最后一个节点往前数K个. 最后得到想要的值. 但是这是不对的,为什么呢?因为题目给出的是单 ...
- STM32 一通道单次转换
之前弄过ADC连续转换,还配了DMA传输,项目上不希望这样做,因为有其他设备用到DMA传输,就会不停的抢占优先级,效率低. 按照需求改成ADC单次转换,非连续,用的时候只需调下函数执行一次转换即可. ...
- 第三十六节,os系统级别操作模块
在使用os模块时需要先 import os 引入模块 os.getcwd()模块函数 功能:获取当前工作目录,即当前python脚本工作的目录路径[无参] 使用方法:os.getcwd() 格式如:a ...
- 面向对象的特性-利用prototype为类创建静态成员
—————————————————————————— <script type="text/javascript"> //用function模拟一 ...
- java 正则
ava - 正则表达式 - Pattern - Matcher 2013-08-21 14:35 3325人阅读 评论(0) 收藏 举报 分类: JavaSE(30) 版权声明:本文为博主原创文章 ...
- Linux系统手动安装rpm包依赖关系分析(以Kernel升级为例)
有在Linux系统中安装软件的经历的人都知道,在Linux系统中手动安装软件不想在Windows下安装软件那么方便,直接双击,然后下一步下一步就可以把软件成功的装入到系统中,而在Linux系统中,安装 ...