Topic : http://www.lydsy.com/JudgeOnline/problem.php?id=1617
1617: [Usaco2008 Mar]River Crossing渡河问题
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 867 Solved: 627
[Submit][Status][Discuss]
Description
Farmer John and his N (1 <= N <= 2,500) cows intends over a river, but all the tools they cross the river, just a raft. Because cows are not rowing, cross the river in the whole process, FJ must always be on the raft. On this basis, the number of cows on the raft for each additional 1, FJ take the raft to the other side would have to spend more time. FJ when a person sitting on the raft, he needs to take the raft across M (1 <= M <= 1000) min. When the number of cows from the raft carrying i-1 increased to i, FJ much to spend M_i (1 <= M_i <= 1000) minutes before the raft across the river (that is, on board a cow, FJ too flowers M + M_1 minutes crossing; when two cows on board, time becomes M + M_1 + M_2 minutes later and so on). Well, FJ least how long it takes to put all the cows to the other side of it? Of course, this time a person have to include FJ take the raft back to take over a number of times from the other side of the cow.
Input
Line 1: 2 with a space-separated integers: N and M
First 2..N + 1: Line i + 1 is an integer: M_i
Output
- Line 1: Output an integer of 1, all of the cows are contained FJ minimum time required to cross the river
Sample Input
5 10
3
4
6
100
1
Enter a description:
FJ带了5头奶牛出门。如果是单独把木筏划过河,FJ需要花10分钟,带上
A cow, it is 13 minutes, two cows 17 minutes, three 23 min 4 123 min,
Five-time load in the past, the time spent is 124 minutes.
Sample Output
50
Thinking :
recording prefix and a [I], a transportation cost of a cow I;
Status: DP [I] representative of a minimum cost operation before I cows and transported back;
transfer equation:
dp[i]=a[i]+a[0];
for(int j=0;j<i;j++)
{
dp[i]=min(dp[i],dp[j]+a[i-j]+a[0]);
}
Code
#include<iostream>
#include<stdio.h>
using namespace std;
int n,m;
int a[2505];
int dp[2505];
int main()
{
scanf("%d%d",&n,&m);
a[0]=m;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-1];
}
for(int i=1;i<=n;i++)
{
dp[i]=a[i]+a[0];
for(int j=0;j<i;j++)
{
dp[i]=min(dp[i],dp[j]+a[i-j]+a[0]);
}
}
printf("%d\n",dp[n]-a[0]);
}