洛谷 P1522 牛的旅行 Cow Tours(最短路_Floyd)

本文介绍了一种通过Floyd算法计算牧场直径的方法,并讨论了如何在牧场中任意两个未连接的点间建立连接来优化直径长度。
部署运行你感兴趣的模型镜像

传送门


先用Floyd求一次最短路,然后记录对于每一个点距离最远的点,枚举任意两个不联通的点将它们联通,计算新的牧场直径(两点间的距离加上他们各自的最远距离)。

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
using namespace std;

const double INF=1e9*1.0;

int n;
int x[160],y[160];
double a[160][160],d[160],l1,l2=INF,ans;

double dis(int i,int j)
{
    return sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d %d",&x[i],&y[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int t;
            scanf("%1d",&t);
            if(t) a[i][j]=dis(i,j);
            else if(i!=j) a[i][j]=INF;
        }
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            if(i!=k) for(int j=1;j<=n;j++)
                if(i!=j && k!=j && a[i][k]+a[k][j]<a[i][j]) a[i][j]=a[i][k]+a[k][j];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            if(a[i][j]!=INF) d[i]=max(d[i],a[i][j]);
        l1=max(l1,d[i]);
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(a[i][j]==INF) l2=min(l2,d[i]+d[j]+dis(i,j));
    ans=max(l1,l2);
    printf("%.6f",ans);
}

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

input_tours_for_drones = 20 len_input_tours_for_drones = 7 aoi = utility.build_random_aoi(width_area, height_area, n_target, n_depots, hovering_time=5, seed=seed) depots = aoi.depots depot_first_drone = depots[0] tours_first_drone=[build_random_tour(aoi,depot_first_drone,np.random.randint(len_input_tours_for_drones - 5,len_input_tours_for_drones + 5)) for i in range(input_tours_for_drones)] depot_second_drone = depots[1] tours_second_drone=[build_random_tour(aoi,depot_second_drone,np.random.randint(len_input_tours_for_drones-5, len_input_tours_for_drones + 5)) for i in range(input_tours_for_drones)] uavs_to_tours = {drones[0]: tours_first_drone, drones[1]: tours_second_drone model = TotalCoverageModel(aoi, uavs_to_tours, max_rounds, debug=False) model.build() model.optimize() mrs = model.solution assert mrs is not None, "optimal solution not found" print("TC-OPT covers", mrs.coverage_score(), "targets using", mrs.max_rounds, "rounds") mrs.plot("TC-OPT") # for big istances (over 200/300 points) remove this plot mrs.plot_cumulative_coverage_for_round("TC-OPT") 以上为用Gurobi求解优解问题,请解释以上程序: depot_first_drone = depots[0] tours_first_drone=[build_random_tour(aoi,depot_first_drone,np.random.randint(len_input_tours_for_drones - 5,len_input_tours_for_drones + 5)) for i in range(input_tours_for_drones)] depot_second_drone = depots[1] tours_second_drone=[build_random_tour(aoi,depot_second_drone,np.random.randint(len_input_tours_for_drones-5, len_input_tours_for_drones + 5)) for i in range(input_tours_for_drones)] uavs_to_tours = {drones[0]: tours_first_drone, drones[1]: tours_second_drone是什么意思
06-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值