HDOJ 4460 Friend Chains 图的最长路
来源:互联网 发布:剑网三苍爹捏脸数据 编辑:程序博客网 时间:2024/04/29 14:51
类似于树的直径,从任意一个点出发,找到距离该点最远的且度数最少的点.
然后再做一次最短路
Friend Chains
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4227 Accepted Submission(s): 1359
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
/* ***********************************************Author :CKbossCreated Time :2015年08月17日 星期一 16时35分00秒File Name :HDOJ4460.cpp************************************************ */#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <cmath>#include <cstdlib>#include <vector>#include <queue>#include <set>#include <map>using namespace std;const int INF=0x3f3f3f3f;const int maxn=2100;int n,m;int id=1;map<string,int> msi;int getID(string name){if(msi[name]==0) msi[name]=id++;return msi[name]; }struct Edge{int to,next,cost;}edge[maxn*maxn];int Adj[maxn],Size;int du[maxn];void init(){id=1; msi.clear();memset(du,0,sizeof(du));memset(Adj,-1,sizeof(Adj)); Size=0;}void Add_Edge(int u,int v){edge[Size].to=v;edge[Size].cost=1;edge[Size].next=Adj[u];Adj[u]=Size++;}int dist[maxn],cq[maxn];bool inq[maxn];bool spfa(int st){ memset(dist,63,sizeof(dist)); memset(cq,0,sizeof(cq)); memset(inq,false,sizeof(inq)); dist[st]=0; queue<int> q; inq[st]=true;q.push(st); cq[st]=1; while(!q.empty()) { int u=q.front();q.pop(); for(int i=Adj[u];~i;i=edge[i].next) { int v=edge[i].to; if(dist[v]>dist[u]+edge[i].cost) { dist[v]=dist[u]+edge[i].cost; if(!inq[v]) { inq[v]=true; cq[v]++; if(cq[v]>=n+10) return false; q.push(v); } } } inq[u]=false; } return true;}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);while(scanf("%d",&n)!=EOF&&n){init();string name1,name2;for(int i=1;i<=n;i++) {cin>>name1;}scanf("%d",&m);for(int i=0;i<m;i++){cin>>name1>>name2;int id1=getID(name1);int id2=getID(name2);Add_Edge(id1,id2); Add_Edge(id2,id1);du[id1]++; du[id2]++;}spfa(1);int st=1;for(int i=2;i<=n;i++){if(dist[st]<dist[i]) st=i;else if(dist[st]==dist[i]){if(du[st]>du[i]) st=i;}}spfa(st);int ans=0;for(int i=1;i<=n;i++) ans=max(ans,dist[i]);if(ans==INF) ans=-1;cout<<ans<<endl;} return 0;}
0 0
- HDOJ 4460 Friend Chains 图的最长路
- HDOJ 题目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
- 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 最短路spfa
- hdu 4460—— Friend Chains
- HDU 4460 Friend Chains BFS 最短路
- HDUOJ_2544(最短路)(dijkstra)
- AlarmManager理解
- 实训仿QQ电话本-知识总结
- IOS 7+ 生成二维码图片
- 黑马程序员——JAVA基础之面向对象(上)
- HDOJ 4460 Friend Chains 图的最长路
- 文件、颜色、字体标准对话框使用
- 如何学习FPGA——转自网络
- oracle学习之第一个存储过程:打印Hello World
- mysql数据库root用户密码忘记进行修改的办法
- zabbix监控多实例tomcat--不使用JMX
- HD 2120 Ice_cream's world I 【并查集】
- Android Dialog的九种常用形式
- Java Socket 网络编程常见异常