题目:

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

题意:

多组输入,第一行给一个n然后输入n个结点(字符串,用map转为int),然后输入一个m代表m条路,然后输入m组数据代表两个点直接有一条路,所有路权值都为1,输出任意两个结点最短路之中的最大值,如果有两个结点之间没有路,那么输出-1。

分析:

对每个点bfs搜最短路,用dis[i][j]代表i结点到j结点的距离,刚开始全部初始化为inf,dis[i][i] = 0。对每个点搜完后对所有最短路遍历,记录最大值ans,如果ans为inf就输出-1,否则输出ans。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e3+5;
const int inf = 0x3f3f3f3f;
int vis[maxn],dis[maxn][maxn],ans = 0;
vector<int> v[maxn];
void bfs(int u){
dis[u][u] = 0;
memset(vis,0,sizeof vis);
queue<int> q;
vis[u] = 1;
q.push(u);
while (!q.empty()){
int tmp = q.front();
q.pop();
int len = v[tmp].size();
for (int i = 0; i < len; i++){
int to = v[tmp][i];
if (!vis[to]){
dis[u][to] = dis[u][tmp] + 1;
vis[to] = 1;
q.push(to);
}
}
}
}
int main(void){
int n,m;
map<string,int> mp;
char str1[15],str2[15];
while (scanf("%d",&n),n){
for (int i = 0; i < n; i++)v[i].clear();
ans = 0;
for (int i = 1; i <= n; i++){
scanf("%s",str1);
mp[str1] = i;
}
scanf("%d",&m);
for (int i = 1; i <= m; i++){
scanf("%s%s",str1,str2);
v[mp[str1]].push_back(mp[str2]);
v[mp[str2]].push_back(mp[str1]);
}
for (int i = 1; i <= n; i++){
for (int j = i+1; j <= n; j++){
dis[i][j] = dis[j][i] = inf;
}
}
for (int i = 1; i <= n; i++)
bfs(i);
for (int i = 1; i <= n; i++){
for (int j = i+1; j <= n; j++){
if (dis[i][j] > ans) ans = dis[i][j];
}
}
printf("%d\n",ans==inf?-1:ans);
}
return 0;
}

需要注意点:

此题用链式前向星TLE,需要改为vector存边,原因不详

此题还可以用类似求树的直径的做法求解(未尝试,有待确认),速度更快

HDU-4460 Friend Chains(BFS&权为1所有最短路的最大值)的更多相关文章

  1. HDU 4460 Friend Chains --BFS

    题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include < ...

  2. HDU 4460 Friend Chains (BFS,最长路径)

    题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(li ...

  3. HDU 4460 Friend Chains(map + spfa)

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

  4. HDU 4460 Friend Chains

    Problem Description For a group of people, there is an idea that everyone is equals to or less than ...

  5. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  6. [HDU 3712] Fiolki (带边权并查集+启发式合并)

    [HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...

  7. hdu 4460 第37届ACM/ICPC杭州赛区H题 STL+bfs

    题意:一些小伙伴之间有朋友关系,比如a和b是朋友,b和c是朋友,a和c不是朋友,则a和c之间存在朋友链,且大小为2,给出一些关系,求出这些关系中最大的链是多少? 求最短路的最大距离 #include& ...

  8. hdu 1565&hdu 1569(网络流--最小点权值覆盖)

    方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

随机推荐

  1. 八十九、SAP中ALV事件之三,查看事件自带说明

    一.双击REUSE_ALV_GRID_DISPLAY,来到SE37的这个函数模块中,查看IT_EVENT的相关说明,点击后面的显示按钮 二.翻译第一句 三.翻译第二句 四.翻译第三句 五.翻译第四句 ...

  2. 004、mysql使用SHOW语句找出服务器上当前存在什么数据库

    SHOW DATABASES; 不忘初心,如果您认为这篇文章有价值,认同作者的付出,可以微信二维码打赏任意金额给作者(微信号:382477247)哦,谢谢.

  3. 【Android】家庭记账本手机版开发报告二

    一.说在前面 昨天 完成了对记账本的账单的增删 今天 完善昨天的框架结构( 1.引入ViewModel管理数据.使MainActive 只管理界面.不再管数据了 2.引入AsyncTask.后台执行. ...

  4. Linux系统学习笔记

    6.启动python cd /root/yq/v4_1_6309a_btc_nw_cq nohup python3 -u v4_1_6309a_btc_nw_cq.py >> 6309a_ ...

  5. Day1-T2

    原题目 在小X的认知里,质数是除了本身和1以外,没有其他因数的数. 但由于小 X对质数的热爱超乎寻常,所以小X同样喜欢那些虽然不是质数, 但却是由两个质数相乘得来的数. 于是,我们定义一个数小 X喜欢 ...

  6. bzoj 4236JOIOJI

    一开始忘掉特殊情况也是蛋疼2333(有一直到头的.mp[0][0]是要特判的) 做法也就是找mp[i][j]相同的东西.(貌似可以写成线性方程组(z=x+A,z=y+B)过这个的就是相等(可以先从2维 ...

  7. java题求代码,4、现在有如下的一个数组: int oldArr[]={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} 要求将以上数组中值为0的项去掉,将不为0的值存入一个新的数组,生成的新数组为: int newArr[]={1,3,4,5,6,6,5,4,7,6,7,5}

    public class TEST { public static void main(String[] args) { int [] oldArr= {1,3,4,5,0,0,6,6,0,5,4,7 ...

  8. 字符,字符串,int之间互相转换

    字符转换成字符串:String str = String.valueOf(ch); 字符转换成int: int a = ch; 字符串转换成字符:char ch = str.charAt(0); 字符 ...

  9. 2020PHP面试-PHP篇

    一.列举一些PHP的设计模式 单例模式:保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点. 工厂模式:定义一个创建对象的接口,但是让 ...

  10. APP分享视频H5页面

    男左女右中国APP需要做一个APP分享视频H5页面,效果图见下面的图. 出现的问题: (1)URL参数为中文的时候乱码: (2)vedio点击默认是QQ,微信的播放器: (3)给视频添加一个默认的封面 ...