题目:

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. 洛谷 - P4008 - 文本编辑器 - 无旋Treap

    https://www.luogu.org/problem/P4008 无旋Treap也可以维护序列. 千万要注意要先判断p节点存在才进行Show操作,不然输出一个'\0'(或者RecBin里面的东西 ...

  2. 2018-8-27-C#-powshell-调用

    title author date CreateTime categories C# powshell 调用 lindexi 2018-8-27 16:20:4 +0800 2018-06-18 20 ...

  3. 微信支付签名算法JavaScript版,参数名ASCII码从小到大排序;0,A,B,a,b;

    // 支付md5加密获取sign paysignjs: function (jsonobj) { var signstr = this.obj2str(jsonobj) signstr = signs ...

  4. 08-求解Ax=b:可解性和解的结构

    一.增广矩阵 假设我们要求解方程$Ax=b$,其中矩阵$A$和$b$如下所示: $A = \left[\begin{array}{llll}{1} & {2} & {2} & ...

  5. js 一个不得不注意的浏览器兼容性问题 进制转换

    写几行JS代码 var num = '022'; alert(num+' '+parseInt(num)+' '+parseInt(num,10)); 不同的浏览器将会得到不同的结果在谷歌浏览器下的结 ...

  6. Nginx优化_自定义报错页面

    自定义返回给客户端的404错误页面 1. 优化前,客户端使用浏览器访问不存在的页面,会提示404文件未找到 client]# firefox http://192.168.4.5/xxxxx      ...

  7. OGG-00303

    解决方案:因为Defgen版本不同,注释数据定义文件对应的行 *Database type:ORACLE,用*在行首注释即可

  8. JAVA 关于File的使用

    File中常用方法 创建 createNewFile() 在指定位置创建一个空文件,成功就返回true,如果已存在就不创建然后返回false mkdir() 在指定位置创建目录,这只会创建最后一级目录 ...

  9. uboot移植之迷雾解码

    按照蜗窝科技的步骤执行 一.有关硬件描述的填空题 1)CPU上电后,从哪种设备(       BOOTROM         )的哪个地址(        0x0000_0000       )开始执 ...

  10. Robomongo可视化命令

    ## 更新命令示例 db.getCollection('collection_name').update(    {'_id':ObjectId('id string')},     {$set:{' ...