HDU 4460Friend Chains(對每個點BFS)

果7發表於2013-10-10

Friend Chains

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


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
 


題目大意:給你n個人,有m條連線,求任意兩點之間的最短距離的最大距離,如果有兩個點之間未聯通,那麼輸出-1.

         解題思路:最短路的問題,當時能想到的就是弗洛伊德不過時間複雜度是O(10^9)會超時。可以對每個點都BFS,這樣時間複雜度就降下來了,具體實現見程式碼。

題目地址:Friend Chains

AC程式碼:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<map>
#include<vector>
#include<queue>
using namespace std;
map <string,int> mq;
vector <int> p[1002];
queue <int> que;
int visi[1002];
int dis[1002][1002];
const int ma=10000;

void bfs(int i)
{
    int j;
    memset(visi,0,sizeof(visi));
    visi[i]=1;
    dis[i][i]=0;
    que.push(i);
    while(!que.empty())
    {
        int x=que.front();  //取出隊頭元素
        que.pop();
        for(j=0;j<p[x].size();j++)
        {
            int t=p[x][j];
            if(!visi[t])
            {
                visi[t]=1;
                dis[i][t]=dis[i][x]+1;
                que.push(t);
            }
        }
    }
}

int main()
{
    int n,m,i,j,res;
    char tmp[15];
    while(scanf("%d",&n)&&n)
    {
        mq.clear();   //名字
        for(i=0;i<n;i++)
            p[i].clear();   //聯絡
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            {
               dis[i][j]=ma;   //初始化最大值
            }
        for(i=0;i<n;i++)
        {
            scanf("%s",tmp);
            mq[tmp]=i;
        }
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            int a,b;
            scanf("%s",tmp);
            a=mq[tmp];
            scanf("%s",tmp);
            b=mq[tmp];
            p[a].push_back(b);
            p[b].push_back(a);   //兩者聯絡加進去
        }

        for(i=0;i<n;i++)
            bfs(i);

        res=0;
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
                res=max(res,dis[i][j]);
        if(res==ma) puts("-1");  //存在兩個點不聯通的
        else printf("%d\n",res);
    }
    return 0;
}


相關文章