BZOJ 1617 Usaco River Crossing

一开始还以为是贪心,结果WA了一发。

才想到这是一个DP题目,子问题就是运送第i头牛时的最小花费。

那么转移方程也好表示。

sum[i]表示前缀和  sum[0]表示单独一个人过河的时间m

dp[i]=min(dp[i],dp[j]+sum[i-j]+sum[0])

#include <cstdio>
#include <algorithm>
#include <cstring>
using std::min;
int n,tot;
int a[5005],dp[5005];

int main(){
    memset(dp,0x3f3f3f3f,sizeof(dp));
    scanf("%d%d",&n,&a[0]);
    for(int i=1;i<=n;i++){
        int x;
        scanf("%d",&x);
        a[i]=a[i-1]+x;
    }
    dp[0]=0;
    for(int i=1;i<=n;i++){
        dp[i]=a[i];
        for(int j=1;j<i;j++){
            dp[i] = min(dp[i],dp[j] + a[i-j] + a[0]);
        }
    }
    printf("%d\n",dp[n]);
    return 0;
}
时间: 2024-12-20 05:02:22

BZOJ 1617 Usaco River Crossing的相关文章

BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题

[题解] 显然是个DP题. 设$f[i]$表示送$i$头牛过河所需的最短时间,预处理出$t[i]$表示一次性送i头牛过河所需时间,那么我们可以得到转移方程:$f[i]=min(f[i],f[i-j]+t[j]+t[0])$ (这里的$t[0]$指的是FJ独自过河的时间) 这样就可以做一个$n$方的DP了 #include<cstdio> #include<algorithm> #define rg register #define inf (1e9) #define N (1000

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 )  表示运送 i 只羊的最少时间 , 转移很显然. T T 我自己第一次写时 , dp多带了个k表示第几次送牛过河 , 可是这并没有什么卵用..还 TLE 了... ------------------------------------------------------------------------- #in

[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必须始终在木筏上.在这个基础上,木筏上的奶牛数

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

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

BZOJ 1143 祭祀 river(最大独立集)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1143 题意:给出一个有向无环图.在其中找出一个最大的点集使得点集中任意两个点之间不可达. 思路:首先在给出图中跑一次floyd,这样g[i][j]=1则i可到达j.那么题意就是求最大独立集.最大独立集=|G|-最小顶点覆盖=|G|-二分图最大匹配. int g[N][N],match[N],visit[N]; int n; int DFS(int u) { int i; FOR1(i,

NYOJ - 716 - River Crossing --第六届河南省程序设计大赛 (简单DP!!)

River Crossing 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Afandi is herding N sheep across the expanses of grassland  when he finds himself blocked by a river. A single raft is available for transportation. Afandi knows that he must ride on the raft for

洛谷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 #inclu

[BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)

[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given