HDU 4460 求任意两点最短距离的最大距离
2016-10-27 16:21
411 查看
Friend Chains
[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5324 Accepted Submission(s): 1718
[/b]
[align=left]Problem Description[/align]
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 .
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case, print the minimum value k in one line.
If the value of k is infinite, then print -1 instead.
[align=left]Sample Input[/align]
3
XXX
YYY
ZZZ
2
XXX YYY
YYY ZZZ
0
[align=left]Sample Output[/align]
2
[align=left]Source[/align]
2012 Asia Hangzhou Regional Contest
[align=left]Recommend[/align]
We have carefully selected several similar problems for you: 5932 5931 5930 5928 5925
求任意两点最短距离的最大距离。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pll;
typedef long long ll;
map<string ,int>mp;
vector<int>g[1005];
int dis[1005][1005];
int vis[1005];
void bfs(int i)
{
memset(vis,0,sizeof(vis));
queue<int>q;
dis[i][i]=0;
q.push(i);
vis[i]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
int sz=g[u].size();
for(int x=0; x<sz; x++)
{
int v=g[u][x];
if(vis[v])
continue;
if(dis[i][v]>dis[i][u]+1)
dis[i][v]=dis[i][u]+1;
vis[v]=1;
q.push(v);
}
}
}
int main()
{
int n;
while(cin>>n,n)
{
mp.clear();
for(int i=0; i<n; i++)
{
string tmp;
cin>>tmp;
mp[tmp]=i;
g[i].clear();
}
int m;
cin>>m;
while(m--)
{
string a,b;
cin>>a>>b;
g[mp[a]].push_back(mp[b]);
g[mp[b]].push_back(mp[a]);
}
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
dis[i][j]=99999999;
for(int i=0; i<n; i++)
bfs(i);
int ans=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
ans=max(ans,dis[i][j]);
if(ans!=99999999)
cout<<ans<<endl;
else
cout<<-1<<endl;
}
return 0;
}
相关文章推荐
- HDU 4460 Friend Chains(任意两点最短路的最大值 bfs或spfa)
- c++递归实现关于无向图中任意两点的最短距离
- hdu 4460 求所有任意两点间的最短路
- Floyd-Warshall算法--求任意两点最短距离
- hdu 2376 Average distance DFS 求树上任意两点距离和
- hdu 6097 Mindis(圆上一点到圆内(距圆心相等的)两点的距离和最短)
- HDU 1869 floyd 求两点间的最短距离
- hdu 3938 Portal 计算两点路上最大距离中的最小值<=L的路径个数 离线并查集
- 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon
- HDU_5723_最小生成树+任意两点距离的期望
- hdu 6097 Mindis(圆上一点到圆内(距圆心相等的)两点的距离和最短)
- hdu 2376(求树上任意两点之间距离之和的平均值)
- 图论03——改进的任意两点间最短距离及路径
- hdu 2376(求树上任意两点之间距离之和的平均值)
- hdu 6097 Mindis(圆上一点到圆内(距圆心相等的)两点的距离和最短)
- hdu 2544 最短路 图论-求两点之间的最短距离 Dijkstra
- 已知有向图任意两点的最短距离,求最小边数
- Floyd 任意两点间的最短距离 dp
- 图论02——任意两点间最短距离及路径
- hdu 6097 Mindis(圆上一点到圆内(距圆心相等的)两点的距离和最短)