usaco2.4.3 Cow Tours

SPFA讲述及小练习:【USACO题库】2.4.3 Cow Tours牛的旅行(含代码) 欢迎收看: 保障电脑安全 拒绝编译错误 DEV-C++题解 今天我们来看一道USACO的题目,本人也是做了三四天才做出来的: 题目描述   农民John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区不连通。这样,农民John就有多个牧区了。   John想在农场里添加一条路径(注意,恰好一条)。对这条路径有以下限制:   一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区的 阅读详情

一 原题

Cow Tours

Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pastures that cannot be connected by any sequence of cow paths, thus partitioning Farmer John's farm into multiple fields.

Farmer John would like add a single a cow path between one pair of pastures using the constraints below.

A field's `diameter' is defined to be the largest distance of all the shortest walks between any pair of pastures in the field. Consider the field below with five pastures, located at the points shown, and cow paths marked by lines:

                15,15   20,15
                  D       E
                  *-------*
                  |     _/|
                  |   _/  |
                  | _/    |
                  |/      |
         *--------*-------*
         A        B       C
         10,10   15,10   20,10

The `diameter' of this field is approximately 12.07106, since the longest of the set of shortest paths between pairs of pastures is the path from A to E (which includes the point set {A,B,E}). No other pair of pastures in this field is farther apart when connected by an optimal sequence of cow paths.

Suppose another field on the same plane is connected by cow paths as follows:

                         *F 30,15
                         / 
                       _/  
                     _/    
                    /      
                   *------ 
                   G      H
                   25,10   30,10

In the scenario of just two fields on his farm, Farmer John would add a cow path between a point in each of these two fields (namely point sets {A,B,C,D,E} and {F,G,H}) so that the joined set of pastures {A,B,C,D,E,F,G,H} has the smallest possible diameter.

Note that cow paths do not connect just because they cross each other; they only connect at listed points.

The input contains the pastures, their locations, and a symmetric "adjacency" matrix that tells whether pastures are connected by cow paths. Pastures are not considered to be connected to themselves. Here's one annotated adjacency list for the pasture {A,B,C,D,E,F,G,H} as shown above:

                A B C D E F G H
              A 0 1 0 0 0 0 0 0
              B 1 0 1 1 1 0 0 0
              C 0 1 0 0 1 0 0 0
              D 0 1 0 0 1 0 0 0
              E 0 1 1 1 0 0 0 0
              F 0 0 0 0 0 0 1 0
              G 0 0 0 0 0 1 0 1
              H 0 0 0 0 0 0 1 0

Other equivalent adjacency lists might permute the rows and columns by using some order other than alphabetical to show the point connections. The input data contains no names for the points.

The input will contain at least two pastures that are not connected by any sequence of cow paths.

Find a way to connect exactly two pastures in the input with a cow path so that the new combined field has the smallest possible diameter of any possible pair of connected pastures. Output that smallest possible diameter.

PROGRAM NAME: cowtour

INPUT FORMAT

Line 1:An integer, N (1 <= N <= 150), the number of pastures
Line 2-N+1:Two integers, X and Y (0 <= X ,Y<= 100000), that denote that X,Y grid location of the pastures; all input pastures are unique.
Line N+2-2*N+1:lines, each containing N digits (0 or 1) that represent the adjacency matrix as described above, where the rows' and columns' indices are in order of the points just listed.

SAMPLE INPUT (file cowtour.in)

8
10 10
15 10
20 10
15 15
20 15
30 15
25 10
30 10
01000000
10111000
01001000
01001000
01110000
00000010
00000101
00000010

OUTPUT FORMAT

The output consists of a single line with the diameter of the newly joined pastures. Print the answer to exactly six decimal places. Do not perform any special rounding on your output.

SAMPLE OUTPUT (file cowtour.out)

22.071068


二 分析

dfs找联通块+floyd最短路


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: cowtour
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4396 KB]
   Test 2: TEST OK [0.000 secs, 4396 KB]
   Test 3: TEST OK [0.000 secs, 4396 KB]
   Test 4: TEST OK [0.000 secs, 4396 KB]
   Test 5: TEST OK [0.011 secs, 4396 KB]
   Test 6: TEST OK [0.011 secs, 4396 KB]
   Test 7: TEST OK [0.011 secs, 4396 KB]
   Test 8: TEST OK [0.011 secs, 4396 KB]
   Test 9: TEST OK [0.011 secs, 4396 KB]

All tests OK.

Your program ('cowtour') produced all correct answers! This is your submission #3 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROG:cowtour
*/

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAX_PASTURE = 155;
const double INF = 99999999;

int n;
int x[MAX_PASTURE], y[MAX_PASTURE];
char mat[MAX_PASTURE][MAX_PASTURE];
int f[MAX_PASTURE], f_num;
double d[MAX_PASTURE][MAX_PASTURE];
double dis[MAX_PASTURE];
double base, ans = INF;

void dfs(int idx) {
    if(f[idx] != 0) return;
    f[idx] = f_num;
    for(int i = 0; i < n; i++) {
        if(mat[idx][i] == '0') continue;
        dfs(i);
    }
}

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

void floyd() {
    for(int i = 0; i < n; i++) {
        for(int j = i; j < n; j++) {
            if(i == j) 
                d[i][j] = 0; 
            else if (mat[i][j] == '0')
                d[i][j] = d[j][i] = INF;
            else
                d[i][j] = d[j][i] = calc(i, j);
        }
    }

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            for(int k = 0; k < n; k++) {
                if(d[j][i] + d[i][k] < d[j][k])
                    d[j][k] = d[j][i] + d[i][k];
            }
        }
    }
}

void init() {
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d%d", &x[i], &y[i]);
    }
    for(int i = 0; i < n; i++) {
        scanf("%s", mat[i]);
    }
}

int main() {
    freopen("cowtour.in", "r", stdin);
    freopen("cowtour.out", "w", stdout);
    init();
    
    for(int i = 0; i < n; i++) {
        if(f[i] != 0) continue;
        f_num++;
        dfs(i);
    }

    floyd();

    for(int i = 0; i < n; i++) {
       for(int j = 0; j < n; j++) {
           if(f[i] != f[j]) continue;
           if(d[i][j] > dis[i]) dis[i] = d[i][j];
       } 
    }
   
    for(int i = 0; i < n; i++) {
        if(dis[i] > base) base = dis[i];
    }
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            if(f[i] == f[j]) continue;
            double candidate = calc(i, j) + dis[i] + dis[j];
            candidate = max(candidate, base);
            if(candidate < ans) ans = candidate;
        }
    }

    printf("%.6f\n", ans);
    return 0;
}



银行数据仓库体系实践(3)--数据架构 狭义的数据仓库数据架构用来特指数据分布,广义的数据仓库数据架构还包括数据模型、数据标准和数据治理。即包含相对静态部分如元数据、业务对象数据模型、主数据、共享数据,也包含相对动态部分如数据流转、ETL、整合、访问应用和数据全生命周期管控治理。 数据架构层面通过数据分类、分层部署等手段,从非功能性视角将数据合理布局。通过整体架构管控和设计,支持业务操作类和管理分析类应用(系统),满足业... 阅读详情

相关推荐

别再手动调时间了!用Windows自带的tzutil命令,1分钟搞定时区错误

本文介绍了如何使用Windows自带的tzutil命令快速解决时区错误问题。通过简单的命令行操作,用户可以一键切换时区、查看全球时区代码库,并实现自动化时区同步。相比传统GUI设置,tzutil更可靠且高效,特别适合企业环境批量部署。

weixin_42531494的博客 314

AI流式响应JSON解析:partial-json-parser原理与实战

JSON作为数据交换的核心格式,其解析通常依赖于标准库的完整语法检查。然而,在AI流式响应等实时场景中,数据以碎片化形式陆续到达,传统JSON.parse()会因语法不完整而直接失败,无法实现渐进式数据提取。这催生了增量解析技术,它通过状态机追踪解析上下文,在语法允许范围内对不完整输入进行‘最大努力’解析,实时输出已确定的结构化信息。该技术对提升大语言模型应用交互体验具有关键价值,尤其在处理OpenAI API流式响应、智能体中间件等需要实时解析AI思考过程与指令混合输出的场景中。本文聚焦的partial-

weixin_30765505的博客 688

Zynq 【SDK裸机开发之PS】——DDR的内存分配使用详解

Zynq如果跑操作系统的话,内存管理的工作都会交给系统来操作,但是如果是裸机的话,则需要自己对DDR3的内存进行管理。

shizhibuyi1234的博客 7377

问题 N: 2.4.3 Cow Tours牛的旅行

【代码】问题 N: 2.4.3 Cow Tours牛的旅行。

m0_64083326的博客 150

【WiFi软件开发】IOCTL和Netlink----用户空间和内核空间交互的两种方式

linux开发中通常会涉及用户空间和内核模块的交互。本文介绍了两种典型的用户空间和内核模块通信方式:ioctl和netlink

liuwe1ye的博客 2348

TPC好像是这么回事儿

TPC(Transmit Power Control)发送功率控制,本觉得就是控制一下发送功率,原理很简单,但看原版协议死活每搞懂什么意思,恼羞成怒!!!! 后来又看了一些贴子和cisco的相关功能介绍,再回头看协议,好像是这么一回事: 1、所谓TPC,是AP控制STA的发送功率的一种手段。在802.11d中,已定义了使用Country Element传递区域无线通信规定的信道和最大发送功率信息: 通过获取这些信息,STA可以确定自己应该遵守什么规则,可以用什么发送功率在哪些信道上通信。 2

两只勤劳的蜜蜂 8629

USACO Section 2.4 Cow Tours (cowtour)

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two past...

weixin_30955341的博客 180

华为手机连接Win11失败?内核隔离与驱动签名冲突的终极解决指南

在Windows操作系统中,驱动程序是硬件与系统通信的桥梁,其数字签名机制是保障系统内核安全的核心防线。基于虚拟化的安全技术通过强制验证驱动签名,能有效隔离恶意代码,提升系统抵御高级威胁的能力。然而,严格的签名策略有时会与部分硬件驱动产生兼容性问题,导致设备无法识别,直接影响文件传输、应用调试等核心工作流。本文聚焦于华为手机在开启Windows 11内核隔离功能时出现的连接故障,深入剖析驱动签名验证与Hypervisor保护的代码完整性原理,并提供从驱动更新、策略调整到无线替代方案的完整解决路径,帮助用户在

weixin_31309419的博客 352

2019 GDUT Rating Contest #I 题解

2019 GDUT Rating Contest #I 题解 A题 题面: Farmer John is considering a change in how he allocates buckets for milking his cow...

qq_36679229的博客 969

Linux 下安装 openjdk 17【详细步骤】

JDK17,安装教程。

甘蓝的专栏 7503

斑马算法优化变分模态分解ZOA-VMD数字信号去噪(优化K值 alpha值)【含Matlab源码 4887期】

斑马算法优化变分模态分解ZOA-VMD数字信号去噪(优化K值 alpha值) 完整的代码,包运行;运行操作视频见优快云资源!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 810

USACO 2.4.3 牛的旅行 Cow Tours

题解 图论入门级的题目。算最短路径,处理需要点技巧。代码很巧妙,琢磨一下啊。 Code #include &amp;lt;iostream&amp;gt; #include &amp;lt;cstdio&amp;gt; #include &amp;lt;cstring&amp;gt; #include &amp;lt;cstdlib&amp;gt; #include &amp;lt;cmath&amp;gt; #inc

蟋蟀的博客 436

UE5 Niagara基础知识讲解

在UE5版本中,Niagara粒子系统引入了许多可添加的模块,分别用于控制发射器的生成和更新、粒子的生成和更新以及渲染效果。本文采用虚幻5.2.1版本,以图文方式对Niagara基础知识进行讲解。

一碗清深的博客 2468

USACO 2.4.3 Cow Tours

Cow Tours Farmer John has a number of pastures on his farm. Cow paths connect some pastures with certain other pastures, forming a field. But, at the present time, you can find at least two pasture

379

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

【USACO题库】2.4.3 Cow Tours牛的旅行

题目描述   农民John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区不连通。这样,农民John就有多个牧区了。 John想在农场里添加一条路径(注意,恰好一条)。对这条路径有以下限制: 一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区的牧场,牧区用“*”表示,

Hello,I'm Proking. 1049

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【USACO2-4-3】牛的旅行 Cow Tours floyd

原题#include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;cmath&gt; #include&lt;cstring&gt; #include&lt;cstdlib&gt; #include&lt;cctype&gt; #define inf 0x3f3f3f3f using namespace std; struct node{i...

Mininda 475

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

USACO2.4.3 Cow Tours (cowtour)

核心是Floyd求任意两点最短路,如下: //Floyd算法 void floyd(int n) { for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } MAP[ ][ ]记录两

xsy_victory的专栏 489

【USACO 2.3.4】Cow Tours 牛的旅行

题目描述 农民 John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧场通过任何路径都不连通。这样,Farmer John就有多个牧场了。 John想在牧场里添加一条路径(注意,恰好一条)。对这条路径有以下限制: 一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区...

weixin_30535843的博客 110

P1522 [USACO2.4]牛的旅行 Cow Tours

link 代码&思路 #include<bits/stdc++.h> #define x first #define y second//保险起见别用define using namespace std; typedef pair<int,int> pii; const int N=152; const double inf=1e20; int n; char g[N][N]; pii q[N];//保存坐标 double d[N][N],maxd[N];//d是邻接矩阵,m

typedef的博客 319

USACO 牛的旅行 Cow Tours

Floyd+枚举 简单“几何”题。

ForceLab 1180

USACO 2.4 cow tours奶牛的旅行

floyd 一开始脑残地想到强连通分支 tarjan 后来看到数据规模约莫估计不会用这么高深的东西 农民 John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通。这样,Farmer John就有多个牧场了。 John想在农场里添加一条路径(注意,恰好一条)。 如果说像这样 A...

weixin_30911809的博客 110

洛谷P1522 牛的旅行 Cow Tours

题目描述 农民 John的农场里有很多牧区。有的路径连接一些特定的牧区。一片所有连通的牧区称为一个牧场。但是就目前而言,你能看到至少有两个牧区通过任何路径都不连通。这样,Farmer John就有多个牧场了。 John想在牧场里添加一条路径(注意,恰好一条)。对这条路径有以下限制: 一个牧场的直径就是牧场中最远的两个牧区的距离(本题中所提到的所有距离指的都是最短的距离)。考虑如下的有5个牧区

蒟蒻的博客 369

USACO2.4のP1522-牛的旅行(Cow Tours)【最短路Flody】

正题 评测记录:https://www.luogu.org/recordnew/lists?uid=52918&amp;pid=P1522 题目大意 有n个点,连接任意两个不同联通块上的点,使这个新的联通块之间最远的两个点的距离最远。 解题思路 先FlodyO(n3)O(n^3)O(n3)计算两两之间的距离 然后计算出每个点最远的点的距离,之后对于枚举两个不同联通块中的点,那么可能产生的新的权...

QuantAsk 1094

luogu P1522 牛的旅行 Cow Tours

题目传送门:https://www.luogu.org/problemnew/show/P1522题意:给出n个坐标,以及他们的连通情况,你可以再选任意两个点相连,求此时的最小的直径(图的直径:图中最远两点的距离)。思路:用floyd求出当前图的最短路情况。设p[i]表示点i连出去的最长的边,则dis[i]=max(f[i][j])(其中1≤j≤n,f[i][j]表示从点i到点j的最短路),进一步...

zsyz_ZZY的博客 312
上一篇: usaco2.4.2 Overfencing
下一篇: usaco2.4.4 Bessie Come Home
max_kibble
博客等级 码龄11年 2粉丝 110原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值