zoukankan      html  css  js  c++  java
  • HDU 4460 Friend Chains

    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

    求最短路径的最长路,直接上floyd果断超时。改用bfs,卡过。

    #include <stdio.h>
    #include <map>
    #include <string>
    #include <queue>
    #include <iostream>
    #define inf 0x3f3f3f3f
    #define MAXN 1005
    using namespace std;
     
    int cnt;
    map< string,int > M;
    int head[MAXN];
    bool visited[MAXN];
    int dist[MAXN][MAXN];
    struct EdgeNode{
        int to;
        int next;    
    }edges[MAXN*20];
    
    void addedge(int u, int v){
        edges[cnt].to=v;
        edges[cnt].next=head[u];
        head[u]=cnt++;
    }
    
    void bfs(int u){
        queue<int> Q;
        Q.push(u);
        dist[u][u]=0;
        memset(visited,0,sizeof(visited));
        visited[u]=1;
        while( !Q.empty() ){
            int now=Q.front();
            Q.pop();
            for(int i=head[now]; i!=-1; i=edges[i].next){
                int to=edges[i].to;
                if(!visited[to]){
                    dist[u][to]=dist[u][now]+1;
                    Q.push(to);
                    visited[to]=1;
                }
            }
        }
    }
    
    int main(int argc, char *argv[])
    {
        int n,k;
        string a,b;
        while(scanf("%d",&n)!=EOF && n){
            M.clear(); 
            memset(head,-1,sizeof(head));
            for(int i=1; i<=n; i++){
                for(int j=i+1; j<=n; j++){
                    dist[i][j]=dist[j][i]=inf;
                }
            }
            for(int i=1; i<=n; i++){
                string name;
                cin>>name;
                M[name]=i;
            }
            cnt=0;
            scanf("%d",&k);
            while(k--){
                cin>>a>>b;
                addedge(M[a],M[b]);
                addedge(M[b],M[a]);
            }
            for(int i=1; i<=n; i++)bfs(i);
            int ans=0;
            for(int i=1; i<=n; i++){
                for(int j=i+1; j<=n; j++){
                    if(dist[i][j]>ans)
                        ans=dist[i][j];
                }
            }
            if(ans==inf)
                printf("-1
    ");
            else
               printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    FPGrowth 实现
    数据库事务的4个特性ACID
    redis简介(keeper实时报表的基本部分)
    HADOOP中的CRC数据校验文件
    潜在语义分析
    AdaBoost 算法
    cocosBuilder生成cbbi文件,绑定到cocos2d-x
    C++继承模型
    Android应用经典主界面框架之中的一个:仿QQ (使用Fragment, 附源代码)
    从今天開始学习iOS开发(iOS 7版)--实现一款App之Foundation框架的使用
  • 原文地址:https://www.cnblogs.com/chenjianxiang/p/3552437.html
Copyright © 2011-2022 走看看