zoukankan      html  css  js  c++  java
  • HDU 4460 Friend Chains(map + spfa)

    Friend Chains

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
    Total Submission(s) : 4   Accepted Submission(s) : 2

    Font: Times New Roman | Verdana | Georgia

    Font Size: ← →

    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

    /*
    题意:给出一个无向图,若联通则输出任意一对点之间最短路径中的最大值,
    若有孤立一点,则输出-1。
    */
    #include <iostream>
    #include<cstdio>
    #include<queue>
    #include<map>
    #include<vector>
    using namespace std;
    const int inf=10000;
    int n,m,maxn;
    map<string,int> mp;
    vector<int> s[1005];
    int dis[1005];
    bool vis[1005];
    void spfa(int st)
    {
        for(int i=1;i<=n;i++) dis[i]=inf,vis[i]=0;
        queue<int> Q;
        Q.push(st);
        vis[st]=1;
        dis[st]=0;
        while(!Q.empty())
        {
            int u=Q.front();
            vis[u]=0;
            Q.pop();
            for(int i=0;i<s[u].size();i++)
            {
                if (dis[s[u][i]]<=dis[u]+1) continue;
                dis[s[u][i]]=dis[u]+1;
                maxn=dis[s[u][i]]>maxn?dis[s[u][i]]:maxn;
                if (!vis[s[u][i]])
                {
                    vis[s[u][i]]=1;
                    Q.push(s[u][i]);
                }
            }
        }
        return;
    }
    int main()
    {
        while(scanf("%d",&n) && n)
        {
            for(int i=1;i<=n;i++)
            {
                char ch[15];
                scanf("%s",&ch);
                mp[ch]=i;
                s[i].clear();
            }
    
            scanf("%d",&m);
            for(int i=1;i<=m;i++)
            {
                char ch1[15],ch2[15];
                scanf("%s %s",&ch1,&ch2);
                s[mp[ch1]].push_back(mp[ch2]);
                s[mp[ch2]].push_back(mp[ch1]);
            }
         int ans=0;
         for(int i=1;i<=n;i++)
         {
             maxn=0;
             spfa(i);
             if (maxn==0) ans=-1;//本来想直接退出,输出-1,但是考虑到现在以i为起点的最短路径计算出来的并不一定是最长的长度,可能是最短路径中的一个中间点
             if (ans>=0 && maxn>ans) ans=maxn;
         }
    
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    mac上finalShell的安装
    c 字符串与字符串操作
    .net5 MailKit
    c 99乘法表
    element 动态表单加自定义校验
    遇到的问题 vscode 问题
    vue-element-admin eslint 规则查询表
    利用html2canvas 导出网页 (只是用于自己的笔记,如果需要看配置,自行查找插件api)
    git 常用命令
    uniapp中自动打包微信小程序后自动上传代码
  • 原文地址:https://www.cnblogs.com/stepping/p/5744762.html
Copyright © 2011-2022 走看看