[USACO2007NOVS] Cow Hurdles S
题目描述
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的更多相关文章
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏( floyd )
直接floyd.. ---------------------------------------------------------------------------- #include<c ...
- 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏
1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 424 Solved: 272 ...
- bzoj1641 / P2888 [USACO07NOV]牛栏Cow Hurdles
P2888 [USACO07NOV]牛栏Cow Hurdles Floyd $n<=300$?果断Floyd 给出核心式,自行体会 $d[i][j]=min(d[i][j],max(d[i][k ...
- POJ 3615 Cow Hurdles(最短路径flyod)
Cow Hurdles Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9337 Accepted: 4058 Descr ...
- Luogu P2888 [USACO07NOV]牛栏Cow Hurdles
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- 洛谷 P2888 [USACO07NOV]牛栏Cow Hurdles
题目戳 题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the ...
- OJ 21651::Cow Hurdles(佛罗一德的变式)
Description Farmer John wants the cows to prepare for the county jumping competition, so Bessie and ...
- 洛谷P2888 [USACO07NOV]牛栏Cow Hurdles
题目描述 Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gan ...
- 【poj3615】 Cow Hurdles
http://poj.org/problem?id=3615 (题目链接) 题意 给出一张有向图,求从u到v最大边最小的路径的最大边.→_→不会说话了.. Solution 好久没写Floyd了,水一 ...
- BZOJ 1641: [Usaco2007 Nov]Cow Hurdles 奶牛跨栏
Description Farmer John 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ...
随机推荐
- ESP32C3 LEDC_PWM
LEDC_PWM LED 控制器 (LEDC) 主要用于控制 LED,也可产生 PWM 信号用于其他设备的控制,ESP32C3有 6 路通道.设置 LEDC 通道分三步完成.与 ESP32 不同 ...
- Spring Boot通过企业邮箱发邮件被Gmail退回的问题解决方法
这两天给我们开发的Chrome插件:Youtube中文配音增加了账户注册和登录功能,其中有一步是邮箱验证,所以这边会在Spring Boot后台给用户的邮箱发个验证信息.如果发邮件,之前的文章教程里就 ...
- MacOS系统(M1/M2)安装AI绘画StableDiffusion保姆级教程
安装完成后,推荐阅读这篇教程:AI绘画:Stable Diffusion 终极炼丹宝典:从入门到精通 实操环境: macOS 13 Arm64(建议12以上的系统使用) Apple M1 先来看几个样 ...
- 1Nginx基础及编译安装
1Nginx基础 1.Nginx概述 Nginx 功能介绍 Nginx(发音为"engine-x")是一个开源的高性能 HTTP和反向代理服务器.它具有以下功能: 1.静态文件服务 ...
- 《Linux基础》01. 概述
@ 目录 1:Linux的应用领域 1.1:个人桌面领域的应用 1.2:服务器领域 1.3:嵌入式领域 2:Linux介绍 3:Linux和Unix的关系 4:Linux基本规则 Linux介绍 1: ...
- MIT 6.828 Lab实验记录 —— lab1 Booting PC
实验参考信息 MIT 6.828 lab1 讲义地址 MIT 6.828 课程 Schedule MIT 6.828 lab 环境搭建参考 MIT 6.828 lab 工具guide Brennan' ...
- 纯分享:将MySql的建表DDL转为PostgreSql的DDL
背景 现在信创是搞得如火如荼,在这个浪潮下,数据库也是从之前熟悉的Mysql换到了某国产数据库. 该数据库我倒是想吐槽吐槽,它是基于Postgre 9.x的基础上改的,至于改了啥,我也没去详细了解,当 ...
- 数据可视化【原创】vue+arcgis+threejs 实现立体光圈闪烁效果
本文适合对vue,arcgis4.x,threejs,ES6较熟悉的人群食用. 效果图: 素材: 主要思路: 先用arcgis externalRenderers封装了一个ExternalRender ...
- Sermant类隔离架构:解决JavaAgent场景类冲突的实践
本文分享自华为云社区<Sermant类隔离架构解析--解决JavaAgent场景类冲突的实践>,作者:华为云开源. Sermant是基于Java字节码增强技术的无代理服务网格,其利用Jav ...
- 解决Promise的多并发问题
提起控制并发,大家应该不陌生,我们可以先来看看多并发,再去聊聊为什么要去控制它 多并发一般是指多个异步操作同时进行,而运行的环境中资源是有限的,短时间内过多的并发,会对所运行的环境造成很大的压力,比如 ...