HDU | Friend Chains-4460 SPFA
题目:
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
自己造的测试样例
答案:源码来自: https://www.xuebuyuan.com/3265778.html
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
const int inf = 0x3f3f3f3f; int N, Q, D[maxn];
vector<int> g[maxn];
map<string, int> idx;
queue<int> que; int bfs(int s)
{
memset(D, inf, sizeof(D));//direction数组初始化每一个值都赋为inf
int ret = D[s] = ;
que.push(s);//把开始搜索的这个人入队 while (!que.empty())
{
int u = que.front();
que.pop(); for (int i = ; i < g[u].size(); i++)
{
//有可能u有多个朋友,所以这里设定为一个循环
int v = g[u][i];//提取出u的朋友的下标
if (D[v] > D[u] + ) //边权都是为1的
{
//跟新direction数组
D[v] = D[u] + ;
que.push(v);
}
}
} //取得最短路中的最大值返回他
for (int i = ; i <= N; i++)
{
cout<<D[i]<<",";
ret = max(ret, D[i]);
}
cout<<endl;
return ret;
} int main()
{
while (scanf("%d", &N) == && N)
{
idx.clear();
string name, a, b; for (int i = ; i <= N; i++)
{
g[i].clear();
cin >> name;
idx[name] = i;//记录它在数组中的位置
} scanf("%d", &Q);
for (int i = ; i < Q; i++)
{
cin >> a >> b;
g[idx[a]].push_back(idx[b]);//记录他朋友在数组中的位置
g[idx[b]].push_back(idx[a]);//双向记录
} int ans = ;
//对每一个i都进行了bfs;有几个人就(从他)bfs几次
for (int i = ; i <= N; i++)
{
cout<<i<<">>";
ans = max(ans, bfs(i));
}
printf("%d\n", ans == inf ? - : ans);
}
return ;
}
WAcode:
一开始是打算在大佬的代码下改改的,后来发现根本不行,这种结构在处理环的适合相当麻烦
#include<iostream>
#include<queue>
#include<string>
#include<string.h>
#include<algorithm>
#define MAX_LENGTH 6 using namespace std;
typedef struct f
{
string name;
int index;
int my_friend_index[];
int my_index;
}Friends;
Friends x[];
int n,m;
int length;
int Bfs(Friends x)
{
queue<Friends> q;
q.push(x);
Friends temp;
while(!q.empty())
{
temp=q.front();
q.pop();
for(int i=;i<temp.index;++i)
{
if(CHRCK(temp.my_friend_index[i],temp.my_index))
{
//如果朋友链没到底,就将下一个人入队
++length;
q.push(x[temp.my_friend_index]);
}
if(x[temp.my_friend_index].my_friend_index==temp.my_index) return length; }
}
return -;
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
if(!n)break;
memset(x,,sizeof(Friends)*);
for(int i=;i<=n;++i)
{
cin>>x[i].name;
x[i].my_index=i;//记录下我的地址
}
//输入人名
cin>>m;
string one="",two="";
int j,k;
for(int i=;i<m;++i)
{
cin>>one>>two;//输入两个人名
for(j=;j<=n;++j)
{
if(one==x[j].name)
{
break;
}
}
for(k=;k<=n;++k)
{
if(two==x[k].name)
{
break;
}
}
x[j].my_friend_index[x[j].index]=k;//记录朋友在数组中的地址
++(x[j].index);
x[k].my_friend_index[x[k].index]=j;//双向记录
++(x[k].index);//有可能有多个朋友
}
int result=-;
for(int j=;j<=n;++i)
{
result=max(result,Bfs(x[i]);
}
cout<<result<<endl;
} return ;
}
HDU | Friend Chains-4460 SPFA的更多相关文章
- HDU 4460 Friend Chains(map + spfa)
Friend Chains Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total ...
- HDU - 2680 最短路 spfa 模板
题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
- HDU——2112HDU Today(SPFA+简单Hash或map+前向星)
HDU Today Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [HDU 1535]Invitation Cards[SPFA反向思维]
题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...
- HDU 3790 最短路径问题 (SPFA)
转载请注明出处:http://blog.csdn.net/a1dark 分析:比一般最短路多了一个花费.多加一个判断即可.用的SPFA.这道题让我搞清楚了以前定义INF为啥爆的问题.受益颇多. #in ...
- HDU 1535 Invitation Cards(SPFA,及其优化)
题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...
- HDU 2544 最短路 SPFA 邻接表 模板
Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以如今他们想 ...
- hdu 1535 Invitation Cards(SPFA)
Invitation Cards Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/65536K (Java/Other) T ...
- hdu 2544 最短路 (spfa)
最短路 Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
随机推荐
- spring map获取同类型的bean
今天看博客怎么减少if else 方法, 才发现spring 还有很多功能我没有用到,以后真的得花时间学学spring,今天学到的东西如下: 1.定义一个接口 store public interfa ...
- SoapUI常用的参数化方法
本篇文章来讲一下SoapUI在使用中常用的参数化方法字段参数化和使用DataSource调用Excel中的 数据给接口参数化,其中DataSource功能SoapUI开源版没有,大家可以使用破解版, ...
- Linux中 xargs 命令用法
xargs命令: xargs命令是给其他命令传递参数的一个过滤器,也是组合多个命令的一个工具. 1.它擅长将标准输入数据转换成命令行参数,xargs能够处理管道或者stdin并将其转换成特定命令的命令 ...
- [Web 前端] 005 html 常用标签补充
少废话,上例子 1. 正常的字 <br> <tt>小一点的字体</tt> <br> <small>变小</small> < ...
- Git-第二篇廖雪峰Git教程学习笔记(1)基本命令,版本回退
1.安装Git-2.16.2-64-bit.exe后,设置用户名,用户邮箱 #--global参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地 ...
- Trie字典树详解
今天上午省选字符串......只会KMP.连hash都不会的我被大佬虐惨了......于是我要发奋图强学习字符串,学习字符串当然就要从Trie树这种可爱的数据结构开始啦!!! 一.什么是Trie树?? ...
- Linux查看当前目录下所有文件中包含map的行记录
find yaochi_e.prm |xargs grep -ri "map" grep -n "map" *.prm|grep -v "\-\-ma ...
- python之流程控制升级
python之流程控制:if elif else while for 一.流程控制之if: 1.1为什要有if判断:让计算机能像人一样代替人类工作,那么计算机应该有对于事务的读错,真假,是否可行的 ...
- Ubuntu环境下SSH服务安装、SSH远程登录以及SSH数据传输
https://www.cnblogs.com/asyang1/p/9467646.html SSH 为 Secure Shell 的缩写,为建立在应用层基础上的安全通信协议. 一.检查SSH服务是否 ...
- CreateMutex函数 (转)
CreateMutex函数 该函数找出当前系统是否已经存在指定进程的实例.如果没有则创建一个互斥体. CreateMutex()函数可用来创建一个有名或无名的互斥量对象,其函数原型为: HANDLE ...