zoukankan      html  css  js  c++  java
  • HDU 4460 Friend Chains(最远最短路,bfs)

    Friend Chains

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


    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

    题目大意与分析

    求最远的两个点的最短距离,n<=1000,所以用n次bfs即可

    #include<bits/stdc++.h>
    
    using namespace std;
    
    
    string s;
    int flag,n,m,anss,i,vis[1005];
    vector<int>mp[1005];
    
    void bfs(int x)
    {
        if(flag==1)
        return;
        queue<int> q;
        q.push(x);
        q.push(0);
        int num=1;
        int temp=0;
        while(!q.empty())
        {
            int next=q.front();
            q.pop();
            int step=q.front();
            q.pop();
            temp=max(temp,step);
            for(int i=0;i<mp[next].size();i++)
            {
                if(vis[mp[next][i]]==0)
                {
                    //cout<<mp[next][i]<<endl;
                    vis[mp[next][i]]=1;
                    q.push(mp[next][i]);
                    q.push(step+1);
                    num++;
                }
            }
        }
        if(num!=n)
        flag=1;
        else
        anss=max(temp,anss);
        //cout<<endl;
    }
    
    int main()
    {
        while(cin>>n)
        {
            
            map<string,int>name;
            flag=0;
            anss=0;
            memset(mp,0,sizeof(mp));
            if(n==0)
            break;
            for(i=0;i<n;i++)
            {
                cin>>s;
                name[s]=i;
            }
            cin>>m;
            while(m--)
            {
                string a,b;
                cin>>a>>b;
                mp[name[a]].push_back(name[b]);
                mp[name[b]].push_back(name[a]);
            }
            for(i=0;i<n;i++)
            {
                memset(vis,0,sizeof(vis));
                vis[i]=1;
                bfs(i);
            }
            if(flag)
            cout<<"-1"<<endl;
            else
            cout<<anss<<endl;
        }
    }
  • 相关阅读:
    day56 js收尾,jQuery前戏
    解决:No module named 'haystack.urls'
    用PicGo+Gitee(码云)搭建Markdown图床
    Python正课138 —— 基础扩展4 django
    Python正课140 —— DRF 进阶1 序列化、增删改查
    Markdown基本语法
    Python正课139 —— DRF 入门1
    用PicGo+GitHub+Typora搭建个人图床
    解决django.core.exceptions.ImproperlyConfiguredmysqlclient 1.3.13 or
    解决:Django中AttributeError:'str'objects has no attribute 'decode'
  • 原文地址:https://www.cnblogs.com/dyhaohaoxuexi/p/12637712.html
Copyright © 2011-2022 走看看