Friend Chains(spfa)

Friend Chains

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 22   Accepted Submission(s) : 11
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
 
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <string>
#include <map>
using namespace std;

const int oo=1<<30;
const int maxn=1001;
const int maxm=20001;
int n,m,num;
int cnt;
int dis[maxn],head[maxn];
bool vis[maxn];
queue<int>qu;
map<string, int> mp;
struct node{
    int u;
    int v;
    int w;
    int next;
}edge[maxm];

void add(int u, int v, int w){
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=w;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}

void spfa(int s){
    for(int i=0; i<n; i++){
        dis[i]=oo;
        vis[i]=false;
    }
    dis[s]=0;
    qu.push(s);
    vis[s]=true;
    while(!qu.empty()){
        int u=qu.front();
        qu.pop();
        vis[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            if(dis[u]+edge[i].w<dis[v]){
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v]){
                    vis[v]=true;
                    qu.push(v);
                }
            }
        }
    }
}

void init(){
    while(!qu.empty()){
        qu.pop();
    }
    cnt=0;
    num=0;
    mp.clear();
    memset(head, -1, sizeof(head));
}

int main(){
    string str;
    string str1;
    while(~scanf("%d",&n)){
        init();
        if(n==0)
            break;
        for(int i=0; i<n; i++){
            cin>>str;
            mp[str]=i;
        }
        scanf("%d",&m);
        for(int i=0; i<m; i++){
            cin>>str>>str1;
            int u=mp[str];
            int v=mp[str1];
            add(u, v, 1);
        }
        for(int i=0; i<n; i++){
            spfa(i);
            for(int j=0; j<n; j++){
                num=max(num,dis[j]);
            }
        }
        num=num==oo?-1:num;
        printf("%d\n",num);
    }
    return 0;
}

你可能感兴趣的:(Friend Chains(spfa))