zoukankan      html  css  js  c++  java
  • HDOJ 4460 Friend Chains 图的最长路


    类似于树的直径,从随意一个点出发,找到距离该点最远的且度数最少的点.

    然后再做一次最短路

    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 .
     

    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.
     

    Output
    For each case, print the minimum value k in one line. 
    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
     

    Source
     


    /* ***********************************************
    Author        :CKboss
    Created 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;
    }
    




  • 相关阅读:
    day3:python测试题
    day4:Python列表(list)元组( tuple)字典(dict)
    day3:python运算符及数据类型(str)(int)
    2day:Python基础
    1day:了解python
    centos下安装storm
    Linux下添加,删除,修改,查看用户和用户组
    svn默认地址老发生改变,记下默认路径
    hive 遇到的问题及解决方法
    hadoop2.5.2 安装与部署
  • 原文地址:https://www.cnblogs.com/mthoutai/p/7010907.html
Copyright © 2011-2022 走看看