顶点对间最短路: SPFA —— 2012.11.04 ACM 杭州 现场赛 H题:Friend Chains!

Friend Chains

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 195    Accepted Submission(s): 77


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
 

————————————————————————————————————————————————

题目意思:给一些点,和点之间的关系(双向)。求所有顶点对间 的最短距离 中的最大值。

         虽然以前看过SPFA 算法,但自己没敲过。比赛之前看了一道SPFA 类似的题,貌似还复杂些。
当时现场赛和一个胖子闹矛盾。main函数中最后for循环没大括号。变成只对最后一次SPFA 进行判断。一直 WA。   
杭州赛区打铁,只做一个题目。 。   。    


//http://acm.hdu.edu.cn/showproblem.php?pid=4460

#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <queue>

using namespace std;
#define INF 99999999

map<string, int>h;

int n, m;
queue <int> q;
int inq[9999];
int dis[9999];
vector<vector <int> > relat;

void SPFA(int x)
{
    int u, v;
    for (int j = 0; j < n; j++)
    {
        dis[j] = INF;
    }
    dis[x] = 0;

    memset(inq, 0, sizeof (inq[0]) * (n + 100));
    inq[x] = 1;

    while (!q.empty())
        q.pop();

    q.push(x);

    while(!q.empty())
    {
        u = q.front();
        q.pop();
        inq[u] = 0;

        for (int j = (int)relat[u].size() -1; j >= 0; j--)
        {
            v = relat[u][j];
            if (dis[v] > dis[u] + 1)
            {
                dis[v] = dis[u] + 1;

                if ( !inq[v])
                {
                    inq[v] = 1;
                    q.push(v);
                }
            }

        }
    }

}

int main()
{
    int ind1, ind2, ans;
    string s1, s2;

    while (scanf("%d", &n)  && n != 0)
    {
        h.clear();
        for (int j = 0; j < n; j++)
        {
            cin >> s1;
            h.insert(make_pair(s1, j));
        }

        cin >> m;
        relat.clear();
        relat.resize(n);

        for (int j = 0; j < m; j++)
        {
            cin >> s1 >> s2;
            ind1 = h[s1];
            ind2 = h[s2];
            relat[ind1].push_back(ind2);
            relat[ind2].push_back(ind1);
        }

        ans = -INF;
        for (int j = 0; j < n; j++)
        {
            SPFA(j);
            for (int k = 0; k < n; k++)
            {
                if (dis[k] == INF)
                {
                    j = INF;
                    ans = -INF;
                    break;
                }
                ans = dis[k] > ans ? dis[k] : ans;
            }
        }

        if (ans == -INF)
            cout << "-1" << endl;
        else
            cout << ans << endl;
    }


	return 0;
}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值