题目描述

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. DateTime 相关的操作汇总【C# 基础】

    〇.前言 在日常开发中,日期值当然是不可或缺的,能够清晰的在脑海中梳理出最快捷的实现也非常重要,那么今天就来汇总一下. 一.C# 中的本机时间以及格式化 如何取当前(本机)时间?很简单,一句话解决: ...

  2. 动态规划二 & 贪心算法 实验三

    一.贪心算法和动态规划法解决背包问题. 有一个背包其容积 C = 13.现有表格内的物品可以购买. 商品 价格 P 体积 V 啤酒 24 10 汽水 2 3 饼干 9 4 面包 10 5 牛奶 9 4 ...

  3. KRPANO太阳光插件

    KRPano太阳光插件可以在全景项目中添加太阳光特效,如下图所示: 同时,该插件支持可视化编辑 使用说明 1.下载插件,把插件放入skin文件夹里面 2.在tour.xml文件中,添加下面的插件引用 ...

  4. 【krpano】 ASP浏览量插件

    简述 这是一个Asp版krpano统计访问量案例,运用asp代码控制增值来实现的功能:现将案例上传网站供大家学习研究,希望对大家有所帮助. 功能 用户进入网页增值或刷新增值. 案例展示 所有文件如下图 ...

  5. Kafka Stream 高级应用

    9.1将Kafka 与其他数据源集成 对于第一个高级应用程序示例,假设你在金融服务公司工作.公司希望将其现有数据迁移到新技术实现的系统中,该计划包括使用 Kafka.数据迁移了一半,你被要求去更新公司 ...

  6. IO流知识汇总(不断更新)

    BIO.NIO.AIO有什么区别? BIO.NIO.AIO是Java中用于处理IO的三种不同的方式,它们之间的区别如下: BIO(Blocking IO):同步阻塞IO,传统的IO模型,也称为传统IO ...

  7. 我看懂了oracle中pivot行转列的用法

    我看懂了PIVOT的用法 用法Select * From 表名,PIVOT(  SUM('要合并的列1'),MAX('要合并的列2'),....FOR 将值转换成列的列名 IN(列名1,列名2,列名3 ...

  8. ExcelPatternTool 开箱即用的Excel工具包现已发布!

    目录 ExcelPatternTool 功能 特点: 快速开始 使用说明 常规类型 高级类型 Importable注解 Exportable注解 IImportOption导入选项 IExportOp ...

  9. LINUX基础知识和命令 二

    LINUX alias (别名) 自定义命令=原始命令 原始命令中有特殊符@#%()请打上 引号,单双无所谓 例:vim /etc/sysconfig/network-scripts/ifcfg-en ...

  10. xxl-job安装部署文档

    xxl-job安装部署文档 注意:替换yaml文件中的mysql地址 安装方式 kubectl apply -f xxl-job.yaml -n xxxx 安装配置文件 xxl-job.yaml ## ...