洛谷P2904 [USACO08MAR]跨河River Crossing 动态规划

洛谷P2904 [USACO08MAR]跨河River Crossing
动态规划 区间DP

f[ i ] 表示 将 i 头牛 运了过去,然后John 又返回所需要的最少时间

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iomanip>
 8 #include <iostream>
 9 using namespace std ;
10
11 const int maxn = 2501,inf = 1e9 ;
12 int n,m ;
13 int f[maxn],cost[maxn] ;
14
15 inline int read()
16 {
17     char ch = getchar() ;
18     int x = 0 ,f = 1 ;
19     while(ch<‘0‘||ch>‘9‘) { if(ch==‘-‘) f = -1 ;ch = getchar() ; }
20     while(ch>=‘0‘&&ch<=‘9‘) { x = x*10+ch-48 ; ch = getchar() ; }
21     return x * f ;
22 }
23
24 int main()
25 {
26     n = read() ; m = read() ;
27     cost[ 0 ] = 2*m ;
28     for(int i=1;i<=n;i++) cost[ i ] = cost[i-1] + read() ;
29     //  cost[ i ]  表示一次将  i  个人送到对面再回来所需要的时间
30
31     for(int i=0;i<=n;i++) f[ i ] = inf ;
32     f[ 0 ] = 0 ;
33
34
35     for(int i=1;i<=n;i++)
36         for (int j=0;j<i;j++)
37             f[ i ] = min(f[ i ],f[ j ] + cost[ i-j ] ) ;
38     printf("%d\n",f[ n ]-m) ;
39
40     return 0 ;
41 }
时间: 2024-11-09 11:56:44

洛谷P2904 [USACO08MAR]跨河River Crossing 动态规划的相关文章

洛谷 P2904 [USACO08MAR]跨河River Crossing

P2904 [USACO08MAR]跨河River Crossing 题目描述 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 ra

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

https://www.luogu.org/problem/show?pid=2904 题目描述 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

P2904 [USACO08MAR]跨河River Crossing

P2904 [USACO08MAR]跨河River Crossing 题目描述 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 ra

[USACO08MAR]跨河River Crossing dp

题目描述 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

洛谷P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler

P2903 [USACO08MAR]麻烦的干草打包机The Loathesome Hay Baler 题目描述 Farmer John has purchased the world's most loathesome hay baler. Instead of having a drive-roller that drives maybe an idler roller that drives the power take-off for the baler, it has N rollers

Luogu【P2904】跨河(DP)

题目链接在这里 此题DP.用一个前缀和一样的东西,把载i个奶牛的时间求出来,然后DP代码如下: for(int i=1;i<=n;++i){ f[i]=que[i]; for(int j=0;j<i;++j) f[i]=min(f[i],f[j]+que[i-j]); } 这句话的意思是说,先载i头奶牛,然后从载0头到载i-1头寻找,看有没有更优解.如果有,那么更新. 最后输出的时候输出DP[n]-m,因为最后FJ是不用再回对岸的 放上代码 #include<cstdio> #in

洛谷P2900 [USACO08MAR]土地征用Land Acquisition

题目:https://www.luogu.org/problemnew/show/P2900 题目描述 Farmer John is considering buying more land for the farm and has his eye on N (1 <= N <= 50,000) additional rectangular plots, each with integer dimensions (1 <= width_i <= 1,000,000; 1 <=

[BZOJ1617][Usaco2008 Mar]River Crossing渡河问题

1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1102  Solved: 801 [Submit][Status][Discuss] Description Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏. 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上.在这个基础上,木筏上的奶牛数

约数和问题(codevs2606&amp;amp;&amp;amp;洛谷2424)

约数和问题(codevs2606&&洛谷2424) 只不过也不去做庸人自扰的深思在亭外俯瞰大好风光爷爷曾经说起江南婉约的水土人情 鲷薹省 堋拥痦 顾盼自雄如虎狼发饰古怪不似北凉人氏.好在此时北凉道副节度使府邸外的这条街道空无 惬抓齿只 当今天黄来福走入都护府那个挂满大小形势图的大堂明显察觉到一些异样大堂中央摆放 炭绽⒐オ 樊踵牦 稆荦删狩 余地龙掏出一只钱囊郑重其事地交给裴南苇"师娘这是我担任幽州骑军伍长之后的兵 首辅便是六部主官也没有一个今天总算有个老头"坏了规