HDU 4460 Friend Chains(map + spfa)

Friend Chains

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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
/*
题意:给出一个无向图,若联通则输出任意一对点之间最短路径中的最大值,
若有孤立一点,则输出-1。
*/
#include <iostream>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
using namespace std;
const int inf=10000;
int n,m,maxn;
map<string,int> mp;
vector<int> s[1005];
int dis[1005];
bool vis[1005];
void spfa(int st)
{
    for(int i=1;i<=n;i++) dis[i]=inf,vis[i]=0;
    queue<int> Q;
    Q.push(st);
    vis[st]=1;
    dis[st]=0;
    while(!Q.empty())
    {
        int u=Q.front();
        vis[u]=0;
        Q.pop();
        for(int i=0;i<s[u].size();i++)
        {
            if (dis[s[u][i]]<=dis[u]+1) continue;
            dis[s[u][i]]=dis[u]+1;
            maxn=dis[s[u][i]]>maxn?dis[s[u][i]]:maxn;
            if (!vis[s[u][i]])
            {
                vis[s[u][i]]=1;
                Q.push(s[u][i]);
            }
        }
    }
    return;
}
int main()
{
    while(scanf("%d",&n) && n)
    {
        for(int i=1;i<=n;i++)
        {
            char ch[15];
            scanf("%s",&ch);
            mp[ch]=i;
            s[i].clear();
        }

        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            char ch1[15],ch2[15];
            scanf("%s %s",&ch1,&ch2);
            s[mp[ch1]].push_back(mp[ch2]);
            s[mp[ch2]].push_back(mp[ch1]);
        }
     int ans=0;
     for(int i=1;i<=n;i++)
     {
         maxn=0;
         spfa(i);
         if (maxn==0) ans=-1;//本来想直接退出,输出-1,但是考虑到现在以i为起点的最短路径计算出来的并不一定是最长的长度,可能是最短路径中的一个中间点
         if (ans>=0 && maxn>ans) ans=maxn;
     }

        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-12 15:21:19

HDU 4460 Friend Chains(map + spfa)的相关文章

HDU 4460 Friend Chains (BFS,最长路径)

题意:给定 n 个人,和关系,问你这个朋友圈里任意两者之间最短的距离是多少. 析:很明显的一个BFS,只要去找最长距离就好.如果不能全找到,就是-1. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include &l

HDU 4571 Travel in time (SPFA 或 dp)

HDU 4571 大概题意:n个点(<=100)m条边(<=1000)的无向图,每个点有消耗costp和价值moneyp,每条边有消耗coste.问从点s出发到点e,消耗不超过t(<=300)所获得的最大价值.经过边必有消耗coste:经过点时,当取得价值moneyp时消耗costp,即为visit该点:当取得价值0,时消耗也为0,即为pass该点.visit的点的moneyp值必须是严格升序. 解法: 容易看出应该用spfa和dp来解.关键时对visit和pass点的处理. 通过flo

HDU 3416 Marriage Match IV(spfa+最大流)

题目的大体意思是:给你一些有向边让你求出给出的点s,t之间最短路的条数. 两边spfa从s到t,和从t到s然后求出在最短路上的点建一条容量为1的边,然后求出s到t的最大的流量,就是最短路的数目. PS:代码写的姿势不够优美. Marriage Match IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2051    Accept

hdu 1251 统计难题 (map水过)

# include <stdio.h> # include <algorithm> # include <string.h> # include <map> # include <iostream> using namespace std; int main() { char a; string x; map<string,int>q; while(true) { scanf("%c",&a); if(a=

hdu 4941 Magical Forest(Map)

http://acm.hdu.edu.cn/showproblem.php?pid=4941 因为地图的行和列很大,操作次数也很多,直接循环模拟肯定不行.但可以用map映射一下,当交换行和列的时候,直接交换它们的映射值,直接O(1)进行交换. #include <stdio.h> #include <iostream> #include <map> #include <set> #include <list> #include <stack

hdu 2112 (最短路+map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14515    Accepted Submission(s): 3405 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发

hdu 4941 stl的map&lt;node,int&gt;用法

#include<iostream> #include<cstdio> #include<cstring> #include<map> using namespace std; typedef struct node{ int x,y; bool operator<(const node &b)const { if(x==b.x) return y<b.y; else return x<b.x; } }node; int main(

HDU 4284 状压dp+spfa堆优化

题意: 给定n个点 m条无向边 d元. 下面m行表示每条边 u<=>v 以及花费 w 下面top 下面top行 num c d 表示点标为num的城市 工资为c 健康证价格为d 目标是经过给定的top个城市,当到达该城市时,必须马上购买该城市的健康证并打工赚钱(每个城市只打工1次) 问从1城市出发,最后回到1城市,能否收集到所有的健康证 思路: 由于top很小,所以状压dp dp[i][tmp]表示当前处于i点 经过城市的状态为tmp时 身上最多的钱. 首先对dis数组floyd 跑出最短路,

hdu 4909 String (map + 状压)

题目大意: 给定一个可能含'?'的字符串.然后问这个字符串有多少个子串是含有所有的字符都只出现两次. 其中'?' 可以被替换成任意字符,也可以被remove... 思路分析: 这是bestcoder的round #3的第三题. 这道题的做法和 4908 的做法差不多. 我们把 '?' 左右两边的状态分别处理出来. 然后用map 计数.然后枚举左边的状态.同时枚举? 对应的字符. 然后去寻找右边对应的状态,此时 ans 就可以加上答案. 注意的是要考虑到以 ? 结尾的情况.所以在 ? 的状态要和上