Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1724 Accepted Submission(s): 611
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
Source
2012 Asia Hangzhou Regional Contest
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <map>
#include <queue>
#include <string>
#include <string.h>
using namespace std;
queue<int >q;
map<string,int> mymap;
#define MAXN 1050
#define inf 100000000
int visit[MAXN],maxx,n;
int dist[MAXN],e,cnt[MAXN],head[MAXN],nxt[MAXN*100],pnt[MAXN*100],cost[100*MAXN];
int addege(int u,int v,int c)
{
pnt[e]=v;
cost[e]=c;
nxt[e]=head[u];
head[u]=e++;
return 1;
}
int relax(int u,int v,int c)
{
if(dist[v]>dist[u]+c)
{
dist[v]=dist[u]+c;
return 1;
}
return 0;
}
void spfa(int src)
{
int i;
memset(cnt,0,sizeof(cnt));
memset(visit,0,sizeof(visit));
for(i=0;i<=n;i++)dist[i]=inf;
dist[src]=0;
while(!q.empty())
q.pop();
q.push(src),visit[src]=1;++cnt[src];
while(!q.empty())
{
int u,v;
u=q.front();q.pop();visit[u]=false;
for(i=head[u];i!=-1;i=nxt[i])
{
v=pnt[i];
if(1==relax(u,v,cost[i])&&!visit[v])
{
q.push(v);visit[v]=1;
}
}
}
}
int main()
{
int i,j,m;
string str,str1,str2;
while(scanf("%d",&n)!=EOF&&n)
{
maxx=-1;e=0;
memset(head,-1,sizeof(head));
mymap.clear();
for(i=0;i<n;i++)
{
cin>>str;
mymap[str]=i;
}
scanf("%d",&m);
for(i=0;i<m;i++)
{
cin>>str1>>str2;
int a=mymap[str1],b=mymap[str2];
addege(a,b,1);
addege(b,a,1);
}
for(i=0;i<n;i++)
{
spfa(i);
for(j=0;j<n;j++)
{
if(j!=i)
maxx=max(maxx,dist[j]);
}
}
if(maxx==inf)
maxx=-1;
printf("%d\n",maxx);
}
return 0;
}