USACO River Crossing
洛谷 P2904 [USACO08MAR]跨河River Crossing
https://www.luogu.org/problem/P2904
JDOJ 2574: USACO 2008 Mar Silver 3.River Crossing
https://neooj.com:8082/oldoj/problem.php?id=2574
题目描述
Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation.
FJ knows that he must ride on the raft for all crossings and that that adding cows to the raft makes it traverse the river more slowly.
When FJ is on the raft alone, it can cross the river in M minutes (1 <= M <= 1000). When the i cows are added, it takes M_i minutes (1 <= M_i <= 1000) longer to cross the river than with i-1 cows (i.e., total M+M_1 minutes with one cow, M+M_1+M_2 with two, etc.). Determine the minimum time it takes for Farmer John to get all of the cows across the river (including time returning to get more cows).
Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏。 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上。在这个基础上,木筏上的奶牛数目每增加1,FJ把木筏划到对岸就得花更多的时间。 当FJ一个人坐在木筏上,他把木筏划到对岸需要M(1 <= M <= 1000)分钟。当木筏搭载的奶牛数目从i-1增加到i时,FJ得多花M_i(1 <= M_i <= 1000)分钟才能把木筏划过河(也就是说,船上有1头奶牛时,FJ得花M+M_1分钟渡河;船上有2头奶牛时,时间就变成M+M_1+M_2分钟。后面的依此类推)。那么,FJ最少要花多少时间,才能把所有奶牛带到对岸呢?当然,这个时间得包括FJ一个人把木筏从对岸划回来接下一批的奶牛的时间。
输入格式
* Line 1: Two space-separated integers: N and M
* Lines 2..N+1: Line i+1 contains a single integer: M_i
输出格式
* Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.
输入输出样例
5 10
3
4
6
100
1
50
说明/提示
There are five cows. Farmer John takes 10 minutes to cross the river alone, 13 with one cow, 17 with two cows, 23 with three, 123 with four, and 124 with all five.
Farmer John can first cross with three cows (23 minutes), then return (10 minutes), and then cross with the last two (17 minutes). 23+10+17 = 50 minutes total.
一下子就想到DP,但是这个数据处理会很麻烦。
但是还是应该循规蹈矩地一步一步走DP的流程。
我们设dp[i]表示把i头奶牛送过河的最小时间。
那么我们怎么去转移呢?
这里用到了一个floyd思想(其实floyd就是用的动态规划的思想)
就是我们在枚举每一头奶牛的时候,再从头枚举一个断点,也就是分两批把i头奶牛送过去的最小时间。
这样就能维护出最优的dp[]数组,最终的答案就是dp[n]。
接下来是数据存储的难点。
我们应该明白,这里的m[i]数组假如要单独存放的话会极其不好处理。
那我们就采用一种常用的手段,把m_i直接设置成dp数组的初值,让其随dp的更新而更新(因为我们求得是最小时间,而我们枚举的思路不需要再保存m_i到底是哪个数)
然后就可以进行dp了。
转移方程是dp[i],dp[j]+dp[i-j]+dp[0]。
这里要注意的是,dp[0]一定要加上去,要不然就考虑不到FJ自己划船回去的时间。
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int dp[];
int main()
{
scanf("%d%d",&n,&m);
dp[]=m;
for(int i=;i<=n;i++)
{
int a;
scanf("%d",&a);
dp[i]=dp[i-]+a;
}
for(int i=;i<=n;i++)
for(int j=;j<i;j++)
dp[i]=min(dp[i],dp[j]+dp[i-j]+dp[]);
printf("%d",dp[n]);
return ;
}
USACO River Crossing的更多相关文章
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题( dp )
dp[ i ] = max( dp[ j ] + sum( M_1 ~ M_( i - j ) ) + M , sum( M_1 ~ M_i ) ) ( 1 <= j < i ) 表示运 ...
- BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题
题目 1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
- 第六届河南省赛 River Crossing 简单DP
1488: River Crossing Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 42 SubmitStatusWeb ...
- ny716 River Crossing
River Crossing 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Afandi is herding N sheep across the expanses ...
- bzoj1617 / P2904 [USACO08MAR]跨河River Crossing
P2904 [USACO08MAR]跨河River Crossing 显然的dp 设$f[i]$表示运走$i$头奶牛,木筏停在未过河奶牛一侧所用的最小代价 $s[i]$表示一次运$i$头奶牛到对面的代 ...
- 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1219 Solved: ...
- [bzoj1617][Usaco2008 Mar]River Crossing渡河问题_动态规划
River Crossing渡河问题 bzoj-1617 Usaco-2008 Mar 题目大意:题目链接. 注释:略. 想法:zcs0724出考试题的时候并没有发现这题我做过... 先把m求前缀和, ...
- BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题
[题解] 显然是个DP题. 设$f[i]$表示送$i$头牛过河所需的最短时间,预处理出$t[i]$表示一次性送i头牛过河所需时间,那么我们可以得到转移方程:$f[i]=min(f[i],f[i-j]+ ...
随机推荐
- SQL Server 迁移数据库 (四)备份和还原
1. 备份 2. 复制 3. 粘贴 4. 还原 截图软件出问题了,估计重启下就好,但是备份还原比较简单,懂的都懂,马上下班了就不贴图了.
- CF-55 C.Pie or die
做法:先把四个角分别覆盖一条边,于是问题转化为判断离边最近的一个点是否离边的距离大于等于4 #include<iostream> #include<cstdio> #inclu ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题
F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...
- spring cloud hystrix dashboard 没有/actuator/hystrix.stream路径解决
首先我用的是spring boot Greenwich.SR2 在测试hystrix-dashboard监控服务时,发现访问localhost:9001/actuator/hystrix.stream ...
- Oracle数据库的关键系统服务整理
在Windows 操作系统下安装Oracle 9i时会安装很多服务——并且其中一些配置为在Windows 启动时启动.在Oracle 运行在Windows 下时,有些服务可能我们并不总是需要但又害怕停 ...
- ItelliJ Idea 2019提交TFVC变更,系统提示Validation must be performed before checking in
问题描述 全新安装的Idea 2019,从Azure DevOps Server 2019 (原名TFS)的TFVC代码库下载文件,正常. 修改代码后,签入,系统提示"Validation ...
- 物联网架构成长之路(43)-k8s从入门到放弃
0. 前言 这段时间要入门一下CI/CD了,以前简单的了解过Jenkins,现在要把以下的这个图的架构搭建起来.国外可能一两个命令就安装完成的事情,我折腾了2天多,真的差点放弃了. 1. 安装Virt ...
- LeetCode 141:环形链表 Linked List Cycle
给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. Given a l ...
- js Iframe与父级页面通信及IE9-兼容性
一. postMessage window.postMessage()方法安全地启用Window对象之间的跨源通信:例如,在页面和它产生的弹出窗口之间,或者在页面和嵌入其中的iframe之间. 二.语 ...
- Javascript 实现倒计时效果
代码来自于网上. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...