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 ...
随机推荐
- servlet过滤器Filter使用之DelegatingFilterProxy类
正常情况下,我们需要添加一个过滤器,需要实现javax.servlet.Filter接口,再在web.xml中配置filter,如下: package cc.eabour.webapp.securit ...
- 微信企业号 发送信息 shell
微信企业号发送信息shell #可作为shell函数模块调用,用于微信通知.jenkins发版微信通知等等 # 微信API官方文档 https://work.weixin.qq.com/api/doc ...
- 关于staticmethod() 函数
说实话,我就不知这个是干什么的. 菜鸟教程写的无需实例化, 自己可以调用自己. 在同一个类面我使用到了 因为一个类了, 我可能会方法间互相调用. 类中间使用.不加这个,就会报错.无法识别这个 orig ...
- JSONPath解析json
JSONPath - 用于JSON的XPath 用来解析多层嵌套的json数据;JsonPath 是一种信息抽取类库,是从JSON文档中抽取指定信息的工具. 考虑到接下来计划开发一个自动化测试平台,在 ...
- css 实现渐变
background:-ms-radial-gradient(circle,rgba(0, 0, 0, 0.2),rgba(0,0,0,0)); /* IE 10*/ background: -web ...
- MVC 源码系列之控制器执行(一)
控制器的执行 之前说了Controller的激活,现在看一个激活Controller之后控制器内部的主要实现. public interface IController { void Execute( ...
- unity编辑器Hierarchy添加图标
效果 素材 using UnityEditor; using UnityEngine; using System.Collections.Generic; [InitializeOnLoad] cla ...
- java Iterator Iterable Collection AbstractCollection Map关系
java.lang Interface Iterable<T> 实现该接口就可以使用for-each循环. java.util Interface Iterator<E> ...
- XmlNode.PrependChild 方法
using System; using System.IO; using System.Xml; public class Sample { public static void Main() { X ...
- Struts2的核心配置文件
Struts2的详细配置: 配置的是struts2的核心配置文件:,在struts2的核心配置文件中主要有三个标签需要进行配置:package,action,result. 1. 配置package标 ...