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 ...
随机推荐
- tomcat下获取当前路径的url中含有空格解决方法
参考博文(http://www.360doc.com/content/11/1009/17/4602013_154657565.shtml) web项目发布到Tomcat之后,如果tomcat是安装在 ...
- if语句—交互程序二
参考:<笨方法学Python>—习题31 用了两个嵌套的if语句! # coding: utf-8 print u"欢迎来到玩家国度, 你需要根据提示完成闯关!" # ...
- 不合法语句 self.contentView.frame.origin.x = x;
下面的写法是错误的: CGFloat x = self.contentView.frame.origin.x; x = _lastDownX + translation.x; self.content ...
- KVM 命令行启动第一台虚拟机
KVM创建第一台虚拟机 1 创建一个镜像 [root@kvm ~]# qemu-img create -f raw /opt/CentOS6.-x86_64.raw 5G Formatting [ro ...
- Linux 格式化分区 报错Could not stat --- No such file or directory 和 partprobe 命令
分区的过程正常: [root@db1 /]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes 255 heads, 63 sectors/tr ...
- 第十六节,基本数据类型,字典dict
字典 常用操作: 索引 新增 删除 键.值.键值对 循环 长度 字典由键值对组成,由一个键(名称)和 一个值组成,与列表和元组不同的是里面的元素是有键的(名称) 所以当要打印字典时,打印字典变量加[键 ...
- java 多线程 一个博客
http://blog.csdn.net/a352193394/article/category/2563875 Java多线程之~~~线程安全容器的非阻塞容器 在并发编程中,会经常遇到使用容器.但是 ...
- OpenCV成长之路:图像滤波
http://ronny.blog.51cto.com/8801997/1394138 OpenCV成长之路:图像滤波 2014-04-11 14:28:44 标签:opencv 边缘检测 sobel ...
- OpenCV成长之路:直线、轮廓的提取与描述
http://ronny.blog.51cto.com/8801997/1394139 OpenCV成长之路:直线.轮廓的提取与描述 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...
- hdu_4824_Disk Schedule(dp)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4824 题意:中文,不解释 题解:双调欧几里德旅行商问题,具体可看dp双调欧几里德旅行商,这里注意的是起 ...