HDU - 4460 Friend Chains (spfa)

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个人的组,然后组内的人名,他们之间有m条关系,问的是这个组里,两两之间的最短距离的最大值。

思路,先用map奖字符串名字映射成数字,再用邻接表存一下两两之间的关系,用spfa求两两之间的距离,更新dis[][]数组。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define N 99999999
map<string,int>m;
vector<int>v[1005];
queue<int>q;
int dis[1005][1005];
int book[1005];
int n;
void init() {
    int i,j;
    m.clear();
    for(i=0; i<n; i++)
        v[i].clear();
    for(i=0; i<n; i++) {
        for(j=0; j<n; j++)
            dis[i][j]=N;
    }
}
void spfa(int d) {
    memset(book,0,sizeof(book));
    while(!q.empty()) q.pop();
    int u,i,j;
    dis[d][d]=0;//自己到自己距离为0
    book[d]=1;
    q.push(d);
    while(!q.empty()) {
        u=q.front();
        q.pop();
        for(i=0; i<v[u].size(); i++) {
            j=v[u][i];//拓展到的点
            if(book[j]==0) {
                book[j]=1;
                dis[d][j]=dis[d][u]+1;
                q.push(j);
            }
        }
    }
}
int main() {
    int t;
    int i,j;
    char name[20];
    int a,b;
    while(scanf("%d",&n) && n) {
        init();//初始化
        for(i=0; i<n; i++) {
            scanf("%s",name);
            m[name]=i;//将名字映射成数字
        }
        scanf("%d",&t);
        while(t--) {
            scanf("%s",name);
            a=m[name];
            scanf("%s",name);
            b=m[name];
            v[a].push_back(b);//因为是求两两之间的距离,所以要存成双向路线
            v[b].push_back(a);//建立邻接表
        }
        for(i=0; i<n; i++) {
            spfa(i);//对每个点都进行一次,每次相当于更新dis[i]的那一行
        }
        /*
        for(i=0;i<n;i++){
            for(j=0;j<n;j++)
                printf("%d ",dis[i][j]);
            printf("\n");
        }
         */
        int ans=0;
        for(i=0;i<n;i++){
            for(j=0;j<n;j++){
                ans=max(ans,dis[i][j]);
            }
        }
        if(ans==N){//如果出现两点之间无法到达

            printf("-1\n");
        }
        else
            printf("%d\n",ans);

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值