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 ...
随机推荐
- MM理论
最初的MM理论,即由美国的Modigliani和Miller(简称MM)教授于1958年6月份发表于<美国经济评论>的“资本结构.公司财务与资本”一文中所阐述的基本思想.该理论认为,在不考 ...
- 5 centos 6.10 三节点安装apache hadoop 2.9.1
Hadoop 版本: apache hadoop 2.9.1JDK 版本: Oracle JDK1.8集群规划master(1): NN, RM, DN, NM, JHSslave1(2): DN, ...
- 【HANA系列】SAP HANA DB 和SAP HANA studio version查看
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[HANA系列]SAP HANA DB 和SAP ...
- 007/Docker(一)
Docker 属于 Linux 容器的一种封装,提供简单易用的容器使用接口. Docker 将应用程序与该程序的依赖,打包在一个文件里面.运行这个文件,就会生成一个虚拟容器.程序在这个虚拟容器里运行, ...
- 前端 CSS 一些标签默认有padding
一个html body标签 默认有 margin外边距属性 比如ul标签,有默认的padding-left值. 那么我们一般在做站的时候,是要清除页面标签中默认的padding和margin.以便于我 ...
- data plugin for vs2019
Reporting Service projects for VS 2019https://marketplace.visualstudio.com/items?itemName=ProBITools ...
- SpringBoot使用RestTemplate基础认证
SpringBoot使用RestTempate SpringBoot使用RestTemplate摘要认证 SpringBoot使用RestTemplate基础认证 SpringBoot使用RestTe ...
- java基础常问面试题
1.面向对象和面向过程的区别 面向过程 :面向过程性能比面向对象高. 因为类调用时需要实例化,开销比较大,比较消耗资源,所以当性能是最重要的考量因素的时候,比如单片机.嵌入式开发.Linux/Unix ...
- String.indexOf()的使用方法
String.indexOf()的用途: 返回此字符串中第一个出现的指定的子字符串,如果没有找到则返回-1 源码如下: /** * Returns the index within this stri ...
- git把本地代码上传(更新)到github上
# 初始化目录为本地仓库 git init # 添加所有文件到暂存去 git add . # 提交所有文件 git commit -m "init" # 添加远程仓库地址 git ...