HDU 4460Friend Chains(对每个点BFS)

来源:互联网 发布:unity3d软件下载 编辑:程序博客网 时间:2024/05/16 10:17

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
3XXXYYYZZZ2XXX YYYYYY ZZZ0
 

Sample Output
2
 

Source
2012 Asia Hangzhou Regional Contest
 


题目大意:给你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;}


原创粉丝点击