hdu 4460 Friend Chains【SPFA+暴力】

本文探讨了FriendChains问题,即在一个社交网络中寻找任意两人间的最短连接路径,确保该路径不超过特定长度k。通过多次单源最短路径算法实现了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Friend Chains

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

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、因为点不多,而且后台数据好像并没有卡SPFA,直接求N次单源最短路即可、967ms Ac、


Ac代码:


#include<stdio.h>
#include<string.h>
#include<map>
#include<queue>
#include<iostream>
using namespace std;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[151511];
int head[10000];
int dis[10000];
int vis[10000];
int cont,n,m;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int SPFA(int ss)
{
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
    dis[ss]=0;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        s.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;
                if(vis[v]==0)
                {
                    vis[v]=1;
                    s.push(v);
                }
            }
        }
    }
    int minn=0;
    for(int i=1;i<=n;i++)
    {
        minn=max(dis[i],minn);
    }
    if(minn==0x3f3f3f3f)return -1;
    else return minn;
}
int main()
{
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        char a[1000];
        map<string ,int >s;
        int contz=0;
        for(int i=0;i<n;i++)
        {
            scanf("%s",a);
            if(s[a]==0)
            {
                s[a]=++contz;
            }
        }
        scanf("%d",&m);
        cont=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<m;i++)
        {
            scanf("%s",a);
            int u=s[a];
            scanf("%s",a);
            int v=s[a];
            add(u,v,1);
            add(v,u,1);
        }
        int ans=0;
        int flag=0;
        for(int i=1;i<=n;i++)
        {
            int tmp=SPFA(i);
            if(tmp==-1)
            {
                flag=1;break;
            }
            else ans=max(ans,tmp);
        }
        if(flag==1)printf("-1\n");
        else printf("%d\n",ans);
    }
}



资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 “STC单片机电压测量”是一个以STC系列单片机为基础的电压检测应用案例,它涵盖了硬件电路设计、软件编程以及数据处理等核心知识点。STC单片机凭借其低功耗、高性价比和丰富的I/O接口,在电子工程领域得到了广泛应用。 STC是Specialized Technology Corporation的缩写,该公司的单片机基于8051内核,具备内部振荡器、高速运算能力、ISP(在系统编程)和IAP(在应用编程)功能,非常适合用于各种嵌入式控制系统。 在源代码方面,“浅雪”风格的代码通常简洁易懂,非常适合初学者学习。其中,“main.c”文件是程序的入口,包含了电压测量的核心逻辑;“STARTUP.A51”是启动代码,负责初始化单片机的硬件环境;“电压测量_uvopt.bak”和“电压测量_uvproj.bak”可能是Keil编译器的配置文件备份,用于设置编译选项和项目配置。 对于3S锂电池电压测量,3S锂电池由三节锂离子电池串联而成,标称电压为11.1V。测量时需要考虑电池的串联特性,通过分压电路将高电压转换为单片机可接受的范围,并实时监控,防止过充或过放,以确保电池的安全和寿命。 在电压测量电路设计中,“电压测量.lnp”文件可能包含电路布局信息,而“.hex”文件是编译后的机器码,用于烧录到单片机中。电路中通常会使用ADC(模拟数字转换器)将模拟电压信号转换为数字信号供单片机处理。 在软件编程方面,“StringData.h”文件可能包含程序中使用的字符串常量和数据结构定义。处理电压数据时,可能涉及浮点数运算,需要了解STC单片机对浮点数的支持情况,以及如何高效地存储和显示电压值。 用户界面方面,“电压测量.uvgui.kidd”可能是用户界面的配置文件,用于显示测量结果。在嵌入式系统中,用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值