hdu4460 Friend Chains
来源:互联网 发布:娱乐圈爆料知乎天涯 编辑:程序博客网 时间:2024/06/10 19:16
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 .
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
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 100000000int 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;}
- hdu4460 Friend Chains
- HDU4460 Friend Chains
- hdu4460-最短路&思维- Friend Chains
- hdu 4460 Friend Chains
- hdu 4460 Friend Chains
- HDU 4460 Friend Chains
- HDU 4460 Friend Chains
- Friend Chains(spfa)
- hdu-4460 Friend Chains
- HDU 4460 Friend Chains
- HDU 4460 Friend Chains
- hdu 4460 Friend Chains (BFS)
- hdu_4460 Friend Chains bfs+vector
- hdu 4460 Friend Chains(BFS)
- HDU 4460 Friend Chains --BFS
- hdu 4460 Friend Chains(bfs)
- hdu4460
- hdu4460
- SMPP是一种通用的短信接口协议
- hdu4296 Buildings
- Unsupported major.minor version 51.0解决办法
- Sqlite工具类
- Hbase 基本应用及命令
- hdu4460 Friend Chains
- 如何去掉字符串前面的0
- MVC hyperlink
- 各种开源许可证的区别
- hibernate中如何对没有id的表进行管理
- HDU-3018几笔画问题
- (Java基础知识)java 套接字
- Swap
- C#——LINQ to object 基本用法