HDU 4460 Friend Chains 第37届ACM杭州赛区 H题

Friend Chains

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


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
 
这个题目是现场过的,不过回来后想想当时做的有点不对,于是自己重新写了一遍并且自己坐了一组数据验证了错误,但是现场赛毕竟过了,而且HDU也过了。
大致思路是:由于边的权值都是1,所以只需要找度最小的作为起始点,求所有点最短路径即可。现场赛做的时候只做了一个最小权值的点最短路径,事实上这样是不对的。应该做所有最小权值点为起始点的最短路径。
这两种做法的时间 分别是 93ms 和593ms。
下面上代码
首先是错误的但是AC的代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define MAXN 1001
#define inf 1000000000
int n;
int m;
int mymap[1001][1001];
struct dnode
{
    int degree;
    int id;
}node[1001];
int cmp(dnode a,dnode b)
{
    return a.degree<b.degree;
}


void dijkstra(int n,int mat[][MAXN],int s,int* min){
    int v[MAXN],i,j,k;
    for (i=0;i<n;i++)
        min[i]=inf,v[i]=0;
    for (min[s]=0,j=0;j<n;j++){
        for (k=-1,i=0;i<n;i++)
            if (!v[i]&&(k==-1||min[i]<min[k]))
                k=i;
        for (v[k]=1,i=0;i<n;i++)
            if (!v[i]&&min[k]+mat[k][i]<min[i])
                min[i]=min[k]+mat[k][i];
    }
}


int main()
{
    char cmd[16];
    char str1[16],str2[16];
    int i;
    while(scanf("%d",&n),n)
    {
        map<string,int> mp;
        for (i=0;i<n;i++)
        {
            scanf("%s",cmd);
            mp[cmd]=i;
            node[i].degree=0;
            node[i].id=i;
        }

        for (i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
            {
                mymap[i][j]=inf;

            }

        }
        scanf("%d",&m);
        for (i=1;i<=m;i++)
        {
            scanf("%s%s",str1,str2);
            int start=mp[str1],end=mp[str2];
            mymap[start][end]=mymap[end][start]=1;
            node[start].degree++;
            node[end].degree++;
        }

        sort(node,node+n,cmp);
        int min[1001];
        dijkstra(n,mymap,node[0].id,min);
        int k=-1;
        for (i=0;i<n;i++)
        {
            if (i!=node[0].id&&k<min[i])
            {
                k=min[i];
            }
        }
        if (k==inf)
        printf("-1\n");
        else
        {
            printf("%d\n",k);
        }



    }
    return 0;
}
然后是正确的AC代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define MAXN 1001
#define inf 1000000000
int n;
int m;
int mymap[1001][1001];
struct dnode
{
    int degree;
    int id;
}node[1001];
int cmp(dnode a,dnode b)
{
    return a.degree<b.degree;
}


void dijkstra(int n,int mat[][MAXN],int s,int* min){
    int v[MAXN],i,j,k;
    for (i=0;i<n;i++)
        min[i]=inf,v[i]=0;
    for (min[s]=0,j=0;j<n;j++){
        for (k=-1,i=0;i<n;i++)
            if (!v[i]&&(k==-1||min[i]<min[k]))
                k=i;
        for (v[k]=1,i=0;i<n;i++)
            if (!v[i]&&min[k]+mat[k][i]<min[i])
                min[i]=min[k]+mat[k][i];
    }
}


int main()
{
    char cmd[16];
    char str1[16],str2[16];
    int i;
    while(scanf("%d",&n),n)
    {
        map<string,int> mp;
        for (i=0;i<n;i++)
        {
            scanf("%s",cmd);
            mp[cmd]=i;
            node[i].degree=0;
            node[i].id=i;
        }

        for (i=0;i<n;i++)
        {
            for (int j=0;j<n;j++)
            {
                mymap[i][j]=inf;

            }

        }
        scanf("%d",&m);
        for (i=1;i<=m;i++)
        {
            scanf("%s%s",str1,str2);
            int start=mp[str1],end=mp[str2];
            mymap[start][end]=mymap[end][start]=1;
            node[start].degree++;
            node[end].degree++;
        }

        sort(node,node+n,cmp);
        int min[1001];

        int k=-1;
        int tmp=node[0].degree;
        for (i=0;i<n&&node[i].degree==tmp;i++)
        {
            dijkstra(n,mymap,node[i].id,min);
            for (int j=0;j<n&&j!=node[i].id;j++)
            {
                if (k<min[j])
                k=min[j];
            }
        }
        if (k==inf)
        printf("-1\n");
        else
        {
            printf("%d\n",k);
        }



    }
    return 0;
}


 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值