题目描述

Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as little energy as possible to jump over the hurdles.

Obviously, it is not very difficult for a cow to jump over several very short hurdles, but one tall hurdle can be very stressful. Thus, the cows are only concerned about the height of the tallest hurdle they have to jump over.

The cows' practice room has \(N (1 ≤ N ≤ 300) stations\), conveniently labeled \(1..N\). A set of \(M (1 ≤ M ≤ 25,000)\) one-way paths connects pairs of stations; the paths are also conveniently labeled \(1..M\). Path i travels from station Si to station Ei and contains exactly one hurdle of height \(H_i\) (\(1 ≤ H_i ≤ 1,000,000\)). Cows must jump hurdles in any path they traverse.

The cows have \(T\) (1 ≤ \(T\) ≤ 40,000) tasks to complete. Task \(i\) comprises two distinct numbers, Ai and Bi (\(1 ≤ A_i ≤ N\);$ 1 ≤ B_i ≤ N$), which connote that a cow has to travel from station \(A_i\) to station \(B_i\) (by traversing over one or more paths over some route). The cows want to take a path the minimizes the height of the tallest hurdle they jump over when traveling from \(A_i\) to \(B_i\) . Your job is to write a program that determines the path whose tallest hurdle is smallest and report that height.

输入格式

* Line 1: Three space-separated integers: N, M, and T

* Lines 2..M+1: Line i+1 contains three space-separated integers: \(S_i\) , \(E_i\) , and \(H_i\)

* Lines M+2..M+T+1: Line i+M+1 contains two space-separated integers that describe task i: \(A_i\) and \(B_i\)

输出格式

* Lines \(1..T\): Line \(i\) contains the result for task \(i\) and tells the smallest possible maximum height necessary to travel between the stations. Output -1 if it is impossible to travel between the two stations.

5 6 3
1 2 12
3 2 8
1 3 5
2 5 3
3 4 4
2 4 8
3 4
1 2
5 1

样例输出 #1

4
8
-1

方法类似最短路。首先可以用floyd预处理出两点间路径最大值的最小值(也就是以最大值作为更新Floyd的过程),然后回答询问即可。

#include<bits/stdc++.h>
using namespace std;
const int N=305;
int n,m,t,u,v,w;
int dp[N][N];
int main()
{
memset(dp,0x7f,sizeof(dp));
scanf("%d%d%d",&n,&m,&t);
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
dp[u][v]=min(dp[u][v],w);
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j]=min(dp[i][j],max(dp[i][k],dp[k][j]));
while(t--)
{
scanf("%d%d",&u,&v);
if(dp[u][v]>1000000)
printf("-1\n");
else
printf("%d\n",dp[u][v]);
}
return 0;
}

优化1:可以将Floyd改为跑n遍Dijkstra,因为一个位置如果现在是最小的,那么别的点再走一条边取一个max最多也就会更这个位置一样,所以dijkstra的贪心仍然符合。

优化2:参考货车运输

[USACO2007NOVS] Cow Hurdles S的更多相关文章

  1. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )

    直接floyd.. ---------------------------------------------------------------------------- #include<c ...

  2. 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 424  Solved: 272 ...

  3. bzoj1641 / P2888 [USACO07NOV]牛栏Cow Hurdles

    P2888 [USACO07NOV]牛栏Cow Hurdles Floyd $n<=300$?果断Floyd 给出核心式,自行体会 $d[i][j]=min(d[i][j],max(d[i][k ...

  4. POJ 3615 Cow Hurdles(最短路径flyod)

    Cow Hurdles Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9337   Accepted: 4058 Descr ...

  5. Luogu P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

  6. 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles

    题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...

  7. OJ 21651::Cow Hurdles(佛罗一德的变式)

    Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and ...

  8. 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles

    题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...

  9. 【poj3615】 Cow Hurdles

    http://poj.org/problem?id=3615 (题目链接) 题意 给出一张有向图,求从u到v最大边最小的路径的最大边.→_→不会说话了.. Solution 好久没写Floyd了,水一 ...

  10. BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏

    Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...

随机推荐

  1. Html+JavaScript实现手写签名

    前言 Hello各位,本葡萄又来啦,今天遇到的场景是这样的:在日常业务流程中,经常需要某一流程环节中相关责任人员进行审批签字,早期许多公司为了省事就直接会把这位负责人的签名以键盘打字(楷体)的形式打印 ...

  2. 云服务器中Linux如何安装宝塔面板?

    作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn 官方使用手册:https://www.kancloud.cn/chudong/bt2017/42420 ...

  3. Azure Data Factory(七)数据集验证之用户托管凭证

    一,引言 上一篇文章中,我们讲解了 Azure Data Factory 在设置数据集类型为  Dataverse 的时候,如何连接测试.今天我们继续讲解认证方式这一块内容,打开 Link Servi ...

  4. 用Rust手把手编写一个Proxy(代理), 动工

    用Rust手把手编写一个Proxy(代理), 动工 项目 ++wmproxy++ gitee 传送门 github 传送门 设计流程图 flowchart LR A[客户端] -->|Http| ...

  5. 3步体验在DAYU200开发板上完成OpenHarmony对接华为云IoT

    本文分享自华为云社区<DAYU200+OpenHarmony 3.1.1对接华为云IOT[华为云IoT+鸿蒙]>,作者:DS小龙哥. 一.前言 OpenHarmony 3.1.1 是一个开 ...

  6. Solution -「九省联考 2018」劈配

    Description Link. 一年一度的综艺节目<中国新代码>又开始了.Zayid 从小就梦想成为一名程序员,他觉得这是一个展示自己的舞台,于是他毫不犹豫地报名了. 轻车熟路的 Za ...

  7. Spring-Boot-Starter 学习笔记(1)

    Spring-Boot-Starter 1. 准备配置类和 Bean 对象 Spring Boot 提供了两个注解: @Configuration:Spring 提供的配置类注解,作用在类上,代表整个 ...

  8. Quantitative Relationship Induction

    数量关系是指事物之间的数值或数量之间的相互关系(+.-.*./). 数量关系描述各种量的变化和相互关系.数量关系可以包括数值的比较.增减.比例.百分比.平均值等方面. 在数学中,数量关系可以通过代数方 ...

  9. Python基础——计算机组成原理、操作系统概述、编程语言的由来、编程语言分类、python介绍、 安装Cpython解释器、 第一个python程序

    文章目录 一 引子: 1.1 什么是语言?什么是编程语言?为何要有编程语言? 1.2 什么是编程?为什么要编程? 二 计算机组成原理 2.1.什么是计算机? 2.2.为什么要用计算机? 2.3.计算机 ...

  10. 文心一言 VS 讯飞星火 VS chatgpt (114)-- 算法导论10.2 7题

    七.用go语言,给出一个 O(n)时间的非递归过程,实现对一个含 n个元素的单链表的逆转.要求除存储链表本身所需的空间外,该过程只能使用固定大小的存储空间. 文心一言: 在Go语言中,你可以使用迭代方 ...