题目描述

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.

输入输出样例

输入样例#1:
复制

5 10
3
4
6
100
1
输出样例#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[ i ] 表示前 i 个奶牛的总耗费;

那么 dp 转移的时候枚举转移点即可;注意要加上单独返回时的时间;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, m;
int dp[maxn]; int main()
{
//ios::sync_with_stdio(0);
rdint(n); rdint(dp[0]);
for (int i = 1; i <= n; i++) {
int tmp; rdint(tmp); dp[i] = dp[i - 1] + tmp;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i; j++) {
dp[i] = min(dp[i], dp[j] + dp[i - j] + dp[0]);
}
}
cout << dp[n] << endl;
return 0;
}

[USACO08MAR]跨河River Crossing dp的更多相关文章

  1. bzoj1617 / P2904 [USACO08MAR]跨河River Crossing

    P2904 [USACO08MAR]跨河River Crossing 显然的dp 设$f[i]$表示运走$i$头奶牛,木筏停在未过河奶牛一侧所用的最小代价 $s[i]$表示一次运$i$头奶牛到对面的代 ...

  2. 【洛谷】P2904 [USACO08MAR]跨河River Crossing(dp)

    题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when ...

  3. [luoguP2904] [USACO08MAR]跨河River Crossing(DP)

    传送门 f[i] 表示送前 i 头牛过去再回来的最短时间 f[i] = min(f[i], f[j] + sum[i - j] + m) (0 <= j < i) ——代码 #includ ...

  4. P2904 [USACO08MAR]跨河River Crossing

    题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when ...

  5. 洛谷—— P2904 [USACO08MAR]跨河River Crossing

    https://www.luogu.org/problem/show?pid=2904 题目描述 Farmer John is herding his N cows (1 <= N <= ...

  6. 【洛谷2904/BZOJ1617】[USACO08MAR]跨河River Crossing(动态规划)

    题目:洛谷2904 分析: 裸dp-- dp方程也不难想: \(dp[i]\)表示运\(i\)头牛需要的最短时间,\(sum[i]\)表示一次运\(i\)头牛(往返)所需的时间,则 \[dp[i]=m ...

  7. 洛谷 P2904 [USACO08MAR]跨河River Crossing

    题目 动规方程 f[i]=min(f[i],f[i−j]+sum) 我们默认为新加一头牛,自占一条船.想象一下,它不断招呼前面的牛,邀请它们坐自己这条船,当且仅当所需总时间更短时,前一头奶牛会接受邀请 ...

  8. USACO River Crossing

    洛谷 P2904 [USACO08MAR]跨河River Crossing https://www.luogu.org/problem/P2904 JDOJ 2574: USACO 2008 Mar ...

  9. 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 )  表示运 ...

随机推荐

  1. appium-unittest框架中的断言

    1.首先unittest本身是一个python的测试框架,他有他自己的使用规则: 2.如果用其中的方法,需要引入,方法: import unittest class Login(unittest.Te ...

  2. Json-lib 进行java与json字符串转换之一

    这篇文章主要介绍了在java中,JSON字符串与java对象的相互转换实例详解,非常不错,具有参考借鉴价值,需要的朋友可以参考下. 在开发过程中,经常需要和别的系统交换数据,数据交换的格式有XML.J ...

  3. VotingClassifier

    scores : array of float, shape=(len(list(cv)),) Array of scores of the estimator for each run of the ...

  4. 【总结整理】行内标签span设置position:absolute/float属性可以设置宽度与高度

    postion:absolute 跳出文本流,不是行内元素,设置宽高有效,我的理解. 引用下曹刘阳写的<编写高质量代码-web前端开发修炼之道>一书中看到的一句话:position:abs ...

  5. SQL 连贯操作 [1]

    一. 连贯入门 查找到 id 为 1,2,3,4 中按照创建时间的倒序的前两位. 在 Home/controller/UserController.class.php 下插入 1.连贯操作入门 $us ...

  6. winform 打印

    pageSetupDialog 打印设置,和对话框控件差不多的套路,把控件拖到窗口中后,会在下方显示, 然后在制作的菜单中找到打印设置,双击进入点击事件写代码 按照之前的套路, DialogResul ...

  7. 杭电ACM刷题(2):1005,Number Sequence 标签: 杭电acmC语言 2017-05-11 22:43 116人阅读

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  8. ZROI #88

    传送门 分析 我们考虑把每个A[i]考虑为山峰的高度,每次的B考虑为海平面 于是我们知道对于A[i]和A[i-1],如果A[i-1]<A[i]则在A[i-1]<B<=A[i]时会使陆 ...

  9. C#多线程的简单例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. Loading Large Bitmaps Efficiently

    有效地加载大位图文件-Loading Large Bitmaps Efficiently 图像有各种不同的形状和大小.在许多情况下,他们往往比一个典型应用程序的用户界面(UI)所需要的资源更大.例如, ...