1000个点floyd是一定会超的,不管怎么改。另外这道题目我很疑惑。因为现场赛的编译器是G++,而我下面的代码是在HDU上面用c++通过的。但在HDU上用G++提交一直TLE,不知道如果比赛那天提交能不能过,代码效率极其低下,应该使用了map和string的关系。如果用HASH和char[]的话时间应该可以在G++过掉。当时想过尝试SPFA,但没去敲。
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 602 Accepted Submission(s): 267
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 .
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.
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.
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<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MAXN 1010
struct node
{
int to;
int dis;
};
bool in[MAXN];
int s[MAXN];
vector<node> g[MAXN];
map<string,int> msi;
int n,m,dis[MAXN],cnt[MAXN];
int ans;
int spfa(int s)
{
queue<int> q;
int tmp=-1;
for(int i=1;i<=n;i++)
{
dis[i]=INF;
in[i]=false;
}
dis[s]=0;
in[s]=true;
q.push(s);
while(!q.empty())
{
int tag=q.front();
in[tag]=false;
q.pop();
for(int i=0;i<g[tag].size();i++)
{
int j=g[tag][i].to;
if(dis[tag]+g[tag][i].dis<dis[j])
{
dis[j]=dis[tag]+g[tag][i].dis;
if(!in[j])
{
q.push(j);
in[j]=true;
cnt[j]++;
}
}
}
}
for(int i=1;i<=n;i++)
{
tmp=max(tmp,dis[i]);
}
return tmp;
}
void solve()
{
node e;
string x1,x2;
msi.clear();
ans=0;
for(int i=1;i<=n;i++)
{
cin>>x1;
msi[x1]=i;
g[i].clear();
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
cin>>x1>>x2;
e.to=msi[x2];
e.dis=1;
g[msi[x1]].push_back(e);
e.to=msi[x1];
e.dis=1;
g[msi[x2]].push_back(e);
}
int ans=-1;
for(int i=1;i<=n;i++)
{
ans=max(spfa(i),ans);
if(ans==INF)
break;
}
if(ans==INF)
{
printf("-1\n");
}
else
printf("%d\n",ans);
}
int main()
{
while(~scanf("%d",&n)&&n)
solve();
return 0;
}