题目描述

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. 文心一言 VS 讯飞星火 VS chatgpt (78)-- 算法导论7.4 2题

    二.如果用go语言,证明:在最好情况下,快速排序的运行时间为 Ω(nlgn). 文心一言: 在最好情况下,快速排序的运行时间为Ω(nlgn)的证明如下: 快速排序是一种分治算法,它将一个数组分成两个子 ...

  2. [ABC128E] Roadwork

    2023-01-14 题目 题目传送门 翻译 翻译 难度&重要性(1~10):4 题目来源 AtCoder 题目算法 区间覆盖,线段树,双堆 解题思路 可以将问题转化为区间覆盖问题和单点查询问 ...

  3. JAVA语言基础day01

    笔记: Java开发环境: java编译运行过程: 编译期:.java源文件,经过编译,生成.class字节码文件 运行期:JVM加载.class并运行.class(0和1) 特点:跨平台,一次编译到 ...

  4. [Python3] 初识py, 一个简单练手的小玩意. 快递查询

    有图有真相 脚本代码 最近刚入门py, 准备写点小玩意练练手. 于是决定拿快递100开刀. 因为它的api很简单. # 快递100 API # 作者: 剑齿虎 # 邮箱: yuxiaobo64@gma ...

  5. Excel单元格快速交换相邻位置内容

    一.相邻两列内容交换(A1与B1交换)1.首先选择A1单元格的边框位置,出现了向上下左右的十字标志 2.此时按住shift键,并且拖向B1单元格的右边,出现"工"汉字标志 3.松开 ...

  6. List取指定元素

    例如: List<string> list = new List<string>(); list.Take(50).ToList();//取前50条 list.Skip(10) ...

  7. JDK21来了!附重要更新说明

    JDK21 计划23年9月19日正式发布,虽然一直以来都是"版本随便出,换 8 算我输",但这么多年这么多版本的折腾,如果说之前的 LTS版本JDK17你还觉得不香,那 JDK21 ...

  8. 循序渐进介绍基于CommunityToolkit.Mvvm 和HandyControl的WPF应用端开发(4) -- 实现DataGrid数据的导入和导出操作

    在我们设计软件的很多地方,都看到需要对表格数据进行导入和导出的操作,主要是方便客户进行快速的数据处理和分享的功能,本篇随笔介绍基于WPF实现DataGrid数据的导入和导出操作. 1.系统界面设计 在 ...

  9. 【matplotlib基础】--动画

    matplotlib的动画一直是一个强大但使用频率不高的功能,究其原因,一方面展示动画需要一定的媒介,没有图形和文字展示方便:二来大家更关心的是分析结果的最终图表,图表的动态展示则没有那么重要. 不过 ...

  10. 《最新出炉》系列初窥篇-Python+Playwright自动化测试-17-处理鼠标悬停

    1.简介 有些测试场景或者事件,playwright根本就没有直接提供方法去操作,而且也不可能把各种测试场景都全面覆盖提供方法去操作.比如:就像鼠标悬停,一般测试场景鼠标悬停分两种常见,一种是鼠标悬停 ...