hdu 4460 Friend Chains
来源:互联网 发布:百度联盟 域名推广 编辑:程序博客网 时间:2024/04/28 06:12
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4460
题目描述:
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 489 Accepted Submission(s): 235
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
3XXXYYYZZZ2XXX YYYYYY ZZZ0
Sample Output
2
题意:给出一个无向图,若联通则输出任意一对点之间最短路径中的最大值,若不联通则输出-1。
题解:对每个点做n次bfs求最短路径,判断图的连通性,注意考虑-1的情况,避免WA。另外bfs的时候利用vector存储一个点的子节点来优化bfs一个父结点时寻找 有哪些子节点,这样就不用穷举所有点判断此点是不是这个父节点的子节点。不然会TLE。
代码:
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<queue>#include<map>#include<iostream>#include<string>#include<vector>using namespace std;map<string,int> NameMap;bool Graph[1000+5][1000+5]={false};bool Vis[1000+5]={false};int KLen[1000+5]={0};int N=0,M=0;string Str,Str1;int k=0,K=0;vector<int> vec[1000+5];/*BFS the whole graph*/int BFS(int cur){queue<int> Queues;Queues.push(cur);Vis[cur]=true;while(!Queues.empty()){int popcur=Queues.front();Queues.pop();int i=0;int count=vec[popcur].size();//optimizationfor(i=0;i<=count-1;i++){int j=vec[popcur][i];if(Graph[popcur][j]&&!Vis[j]){KLen[j]=KLen[popcur]+1;Queues.push(j);Vis[j]=true;if(KLen[j]>k){k=KLen[j];}}}}return(0);}/*for test*/int test(){return(0);}/*main process*/int MainProc(){while(scanf("%d",&N)!=EOF&&N>0){memset(Graph,false,sizeof(Graph));memset(Vis,false,sizeof(Vis));memset(KLen,0,sizeof(KLen));NameMap.clear();k=0;K=0;int i=0;for(i=0;i<=N-1;i++){cin>>Str;NameMap[Str]=i;//from 0 to N-1vec[i].clear();}scanf("%d",&M);for(i=0;i<=M-1;i++){cin>>Str>>Str1;Graph[NameMap[Str]][NameMap[Str1]]=true;Graph[NameMap[Str1]][NameMap[Str]]=true;vec[NameMap[Str]].push_back(NameMap[Str1]);vec[NameMap[Str1]].push_back(NameMap[Str]);}for(i=0;i<=N-1;i++)//n count of bfs to the graph{k=0;memset(Vis,false,sizeof(Vis));memset(KLen,0,sizeof(KLen));BFS(i);if(k>0&&K>=0&&K<k){K=k;}if(k<=0){K=-1;}}printf("%d\n",K);}return(0);}int main(){MainProc();return(0);}
- hdu 4460 Friend Chains
- hdu 4460 Friend Chains
- HDU 4460 Friend Chains
- HDU 4460 Friend Chains
- hdu-4460 Friend Chains
- HDU 4460 Friend Chains
- HDU 4460 Friend Chains
- hdu 4460 Friend Chains (BFS)
- hdu 4460 Friend Chains(BFS)
- HDU 4460 Friend Chains --BFS
- hdu 4460 Friend Chains(bfs)
- Hdu 4460 Friend Chains 最短路spfa
- hdu 4460—— Friend Chains
- HDU 4460 Friend Chains BFS 最短路
- HDU-#4460 Friend Chains(bfs)
- hdu 4460 Friend Chains 题解(SPFA)
- HDU 4460 Friend Chains(map + spfa)
- HDU 4460 Friend Chains(最短路)
- 浅谈数据库设计技巧(上)
- 进大公司还是进小公司
- POJ 2488解题报告
- sql sever 和mysql的存储过程的日期时间转换DATEDIFF
- 非常仔细明了 关于如何求解多边形面积
- hdu 4460 Friend Chains
- Regex
- ASP.NET四种页面导航方式分析
- 进制之间的转换
- [struts2][问题集合]
- jsp课程(7)---jsp+servlet+javabean 实现的简单网上购物车
- 布隆过滤器,数据处理的神来之笔!BloomFilter
- uva 442 Matrix Chain Multiplication
- 无刷新多文件上传(iframe)