hdu-4460 Friend Chains

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

求任意点对的最短距离的最大值,因为题目数据量大,用floyed 和dijkstra都不行,这里用spfa实现

#include<cstdio>
#include<map>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
#define N 1001
#define inf 0x7fffffff
using namespace std;
int dis[N][N],n,vis[N];
vector<int>v[N];
queue<int>q;
map<string,int>mm;
void init()
{
    for(int i=0;i<n;i++)
        v[i].clear();
    for(int i=0;i<n;i++)
    {
        dis[i][i]=0;
        for(int j=i+1;j<n;j++)
          dis[i][j]=dis[j][i]=inf;
    }
    mm.clear();
    while(!q.empty()) q.pop();
}
void spfa(int i)
{
    vis[i]=1;
    dis[i][i]=0;
    q.push(i);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        for(int j=0;j<v[t].size();j++)
        {
            int m=v[t][j];
            if(!vis[m])
            {
                dis[i][m]=dis[i][t]+1;
                vis[m]=1;
                q.push(m);
            }
        }
    }
}
int main()
{
    int m;
    string s,a,b;
    while(scanf("%d",&n)!=EOF&&n)
    {
        init();
        for(int i=0;i<n;i++)
        {
            cin>>s;
            mm[s]=i;
        }
        scanf("%d",&m);
        while(m--)
        {
            cin>>a>>b;
            int x=mm[a];
            int y=mm[b];
            v[x].push_back(y);
            v[y].push_back(x);
        }
        for(int i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis));
            spfa(i);
        }
        int ans=0;
        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {

                ans=max(ans,dis[i][j]);
            }
        }
        if(ans==inf) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-11-10 00:59:20

hdu-4460 Friend Chains的相关文章

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, th

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

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

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

HDU 4460

题意:问给定的一张图中,相距最远的两个点的距离为多少.解法:跟求树的直径差不多,从1 开始bfs,得到一个最远的点,然后再从该点bfs一遍,得到的最长距离即为答案. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <strin

hdu 4460 STL+bfs

题意:一些小伙伴之间有朋友关系,比如a和b是朋友,b和c是朋友,a和c不是朋友,则a和c之间存在朋友链,且大小为2,给出一些关系,求出这些关系中最大的链是多少? 求最短路的最大距离 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include

hdu 1204 糖果大战 (Markov Chains求期望)

糖果大战 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2336    Accepted Submission(s): 797 Problem Description 生日Party结束的那天晚上,剩下了一些糖果,Gandon想把所有的都统统拿走,Speakless于是说:"可以是可以,不过我们来玩24点,你不是已经拿到了一些糖果了吗

hdu 3746 Cyclic Nacklace KMP

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3746 题目: Problem Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed

HDU 3746 Cyclic Nacklace(KMP求循环节)

Description CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspir

HDU 3746 Cyclic Nacklace

Description: CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left. he is too distressed and thinking about how to tide over the last days. Being inspi

hdu 3487 Play with Chain

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3487 YaoYao is fond of playing his chains. He has a chain containing n diamonds on it. Diamonds are numbered from 1 to n.At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.He will perform t