hdu 4460 求所有任意两点间的最短路

本文介绍了一种在给定朋友关系网络中计算任意两点间最短路径长度的方法,使用了广度优先搜索(BFS)或SPFA算法,并提供了代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 575    Accepted Submission(s): 258


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
 

Source


题意:

求任意两点间的最短路的距离的最大值


思路:  用flord  dij  都会超时

可以用bfs  或者spfa

下面的代码是别人的bfs 贴上

来源于http://www.cnblogs.com/pony1993/archive/2012/11/09/2762289.html

//============================================================================
// Name        : HDU4460.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <vector>
#include<string>
using namespace std;
const int MAXN=1010;
const int INF=0x3f3f3f3f;
int dis[MAXN][MAXN];
bool used[MAXN];
vector<int>vec[MAXN];
queue<int>que;
void bfs(int i)
{
    memset(used,false,sizeof(used));
    dis[i][i]=0;
    used[i]=true;
    que.push(i);
    while(!que.empty())
    {
        int t=que.front();
        que.pop();
        int m=vec[t].size();
        for(int j=0;j<m;j++)
        {
            int v=vec[t][j];
            if(used[v])continue;//从某点到某点如果有多条路 根据que的性质可以知道最短的路径会被先标记 所以就不用再走了
			if(dis[i][v]>dis[i][t]+1)//由于权值都是1  这句话也可以不加
            dis[i][v]=dis[i][t]+1;
            que.push(v);
            used[v]=true;
        }
    }
}

map<string,int>mp;
int main() {
    string str;
    string str2;
    int n,m,i,j;
    while(scanf("%d",&n)==1 && n)
    {
        mp.clear();
        for(i=0;i<n;i++)
        {
            cin>>str;
            mp[str]=i;
        }

        for(i=0;i<n;i++)
        {
            dis[i][i]=0;
            for(j=i+1;j<n;j++)
                dis[i][j]=dis[j][i]=INF;
        }
        scanf("%d",&m);
        for(i=0;i<n;i++)vec[i].clear();
        while(m--)
        {
            cin>>str>>str2;
            int t1=mp[str];
            int t2=mp[str2];
            vec[t1].push_back(t2);
            vec[t2].push_back(t1);
        }
        for(i=0;i<n;i++)bfs(i);
        int ans=0;
        for(i=0;i<n;i++)
             for(j=i+1;j<n;j++)
                 ans=ans>dis[i][j]?ans:dis[i][j];
        if(ans==INF)ans=-1;
        printf("%d\n",ans);
    }
    return 0;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值