[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 想让她的奶牛准备郡级跳跃比赛,贝茜和她的伙伴们正在练习跨栏.她们很累,所以她们想消耗最少的能量来跨栏. 显然,对于一头奶牛跳过几个矮栏是很容易的,但是高 ... 
随机推荐
- Sealos 国内集群正式上线,可一键运行 LLama2 中文版大模型!
			2023 年 7 月 19 日,MetaAI 宣布开源旗下的 LLama2 大模型,Meta 首席科学家.图灵奖得主 Yann LeCun 在推特上表示 Meta 此举可能将改变大模型行业的竞争格局. ... 
- 《深入理解Java虚拟机》读书笔记:字节码指令简介
			字节码指令简介 Java虚拟机的指令由一个字节长度的.代表着某种特定操作含义的数字(称为操作码,Opcode)以及跟随其后的零至多个代表此操作所需参数(称为操作数,Operands)而构成.由于Jav ... 
- linux 查找目录中的大文件
			find是Linux系统中常用的文件查找命令.它可以在文件系统中查找指定条件的文件,并执行相应的操作.语法格式如下: find [pathname] [options] pathname: 指定查找的 ... 
- Redis系列20:LFU内存淘汰算法分析
			Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ... 
- Go 并发编程 - Goroutine 基础 (一)
			基础概念 进程与线程 进程是一次程序在操作系统执行的过程,需要消耗一定的CPU.时间.内存.IO等.每个进程都拥有着独立的内存空间和系统资源.进程之间的内存是不共享的.通常需要使用 IPC 机制进行数 ... 
- 论文日记五:QueryInst
			导读 实例分割领域21年的SOTA,整个模型结构设计总述为以下: 1)设计了类似于Cascade Mask R-CNN的多阶段迭代优化的bbox和mask预测头: 2)基于query的实例分割方法,延 ... 
- 4.go语言复合类型简述
			目录 1. 本章前瞻 2.来自leetcode的例题 描述 分析 题解 3. 复合类型新版本的变化 3.1 string和[]byte的高效转化 3.2 内置函数clear 4. 复合类型概述 4.1 ... 
- JWT(Json Wen Token)原理剖析
			JWT(即json web token),大家先看下面这张图 大家可以观察到,jwt String就是生成后的jwt字符集,其中有两个 "."(注意:jwt校验会对".& ... 
- 万字长文教你实现华为云IoT+OpenHarmony智能家居开发
			本文分享自华为云社区<华为云IoT+OpenHarmony的智能家居开发>,作者:袁睿. 一.选题说明 1. 选题为基于OpenHarmony的智能家居,应用场景为户用,受益人群为住户. ... 
- redis基本数据类型 set类型
			127.0.0.1:6379> SADD s1 a b c (integer) 3 127.0.0.1:6379> SMEMBERS s1 1) "b" 2) &quo ... 
