题目:

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的更多相关文章

  1. HDU 4460 Friend Chains(map + spfa)

    Friend Chains Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  2. HDU - 2680 最短路 spfa 模板

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目大意,就是一个人可以从多个起点开始出发,看到终点的最短路是多少..只有可以运用和hdu2066 ...

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

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

  4. HDU——2112HDU Today(SPFA+简单Hash或map+前向星)

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

  5. [HDU 1535]Invitation Cards[SPFA反向思维]

    题意: (欧洲人自己写的题面就是不一样啊...各种吐槽...果断还是看晕了) 有向图, 有个源叫CCS, 求从CCS到其他所有点的最短路之和, 以及从其他所有点到CCS的最短路之和. 思路: 返回的时 ...

  6. HDU 3790 最短路径问题 (SPFA)

    转载请注明出处:http://blog.csdn.net/a1dark 分析:比一般最短路多了一个花费.多加一个判断即可.用的SPFA.这道题让我搞清楚了以前定义INF为啥爆的问题.受益颇多. #in ...

  7. HDU 1535 Invitation Cards(SPFA,及其优化)

    题意: 有编号1-P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费. 有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1. ...

  8. HDU 2544 最短路 SPFA 邻接表 模板

    Problem Description 在每年的校赛里,全部进入决赛的同学都会获得一件非常美丽的t-shirt.可是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以如今他们想 ...

  9. hdu 1535 Invitation Cards(SPFA)

    Invitation Cards Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) T ...

  10. hdu 2544 最短路 (spfa)

    最短路 Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissio ...

随机推荐

  1. KETTLE——(二)数据抽取

    过了个春节,好长时间没有更新了,今天接着写第二部分——数据抽取. 进入界面以后会发现左侧菜单有两个东西:转换和作业:简单说一下,转换是单次的转换,不可重复,但可重复利用:作业是汇聚了其他操作和多次(可 ...

  2. selenium验证码处理之cookie登录

    在实际测试中会经常见到登录操作需要验证码验证登录 常见验证有以下几种: 验证码登录 图片识别   图片滑块识别验证   4.简单验证码计算 针对上面的登录验证解决办法有以下几种: 1.让开发去掉验证码 ...

  3. js获取select下拉框中的值

    现在有一id为userType的下拉框,怎么获取选中的值: 用户类型: <select name="type" id="userType"> < ...

  4. css的继承之width属性(容易忽略)

    众所周知,css的三大特性分别是 继承性,层叠性,和优先级. 那么这里就详细说一下css中width的继承性及其特殊情况. 继承性概念详解:css的继承性指的被包在内部的标签拥有外部标签的样式性,子元 ...

  5. MySQL-第十四篇事务管理

    1.什么是事务 事务是由一步或者几步数据库操作序列组成的逻辑执行单元,这系列操作要么全部执行,要么全部放弃执行. 2.事务具备的4个特性: 1>原子性(Atomicity):事务是应用中最小的执 ...

  6. Javascript之谈对象

    谈谈如何理解对象 使用预定义对象只是面向对象语言的能力的一部分,ECMAScript 真正强大之处在于能够创建自己专用的类和对象.面向对象的语言有一个标志,那就是它们都有类的概念,而通过类可以创建任意 ...

  7. Oracle数据库SQL语句的分类

    1986年10月,美国国家标准协会对SQL进行规范后,以此作为关系式数据库管理系统的标准语言,1987年在国际标准组织的支持下成为国际标准.不过各种通行的数据库系统其实在实践过程中都对SQL规范的作了 ...

  8. python爬虫相关安装与应用

    1.mysql数据库用于存储大量数据. 2.Navicat for MySQL以图形和表格等形式管理数据库工具. 3.编程语言python3与环境配置 4.pythcharm集成开发环境(社区版)不需 ...

  9. 【题解】Shortest Cycle

    原题链接:CF1205B   题目大意   给定\(n\)个整数\(a_1,a_2,a_3, \dots ,a_n\),若\(i \neq j\)且\(a_i \land a_j \neq 0\),则 ...

  10. 2019 Multi-University Training Contest 1 - 1001 - Blank - dp

    http://acm.hdu.edu.cn/showproblem.php?pid=6578 不会做,看题解. 设dp[i][j][k][l]表示4种颜色出现的最后的位置分别是i,j,k,l的方法数, ...