USACO River Crossing

Luo Gu P2904 [USACO08MAR] river crossing 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

Title Description

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 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 Format

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Line i+1 contains a single integer: M_i

Output Format

* Line 1: The minimum time it takes for Farmer John to get all of the cows across the river.

Sample input and output

Input # 1
5 10 
3 
4 
6 
100 
1 
Output # 1
50 

Description / Tips

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.

 

Suddenly thought of DP, but the data processing will be very troublesome.

But still we should behave step by step process DP.

We set DP [i] represents the minimum time to send i cows across the river.

So how do we transfer it?

This uses a floyd idea (in fact, floyd idea of ​​dynamic programming is used)

That is, when we enumerate each cow, and then re-enumerate a breakpoint, which is sent in two batches to the minimum time i cows past.

This would maintain the optimal dp [] array, the ultimate answer is dp [n].

Next is the difficulty of data storage.

We should understand, I m here [i] If you want to store an array of individual words can be extremely hard to deal with.

Then we use a common means, the initial value of dp m_i directly into an array, let dp be updated with the update (because we find is the minimum time, and we do not need to enumerate the idea in the end save m_i which number)

Then you can dp up.

Transfer equation is dp [i], dp [j] + dp [ij] + dp [0].

Here we must note that, dp [0] must be added to, or else not consider FJ own rowing back time.

 

Code:

#include<cstdio>
#include<algorithm>
using namespace std;
int n,m;
int dp[2501];
int main()
{
    scanf("%d%d",&n,&m);
    dp[0]=m;
    for(int i=1;i<=n;i++)
    {
        int a;
        scanf("%d",&a);
        dp[i]=dp[i-1]+a;
    }
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            dp[i]=min(dp[i],dp[j]+dp[i-j]+dp[0]);
    printf("%d",dp[n]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fusiwei/p/11258229.html