zoukankan      html  css  js  c++  java
  • 第六届河南省赛 River Crossing 简单DP

    1488: River Crossing

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 83  Solved: 42

    SubmitStatusWeb Board

    Description

     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 all crossings, but adding sheep to the raft makes it traverse the river more slowly.

    When Afandi is on the raft alone, it can cross the river in M minutes When the i sheep are added, it takes Mi minutes longer to cross the river than with i-1 sheep (i.e., total M+M1   minutes with one sheep, M+M1+M2 with two, etc.).

    Determine the minimum time it takes for Afandi to get all of the sheep across the river (including time returning to get more sheep).

    Input

    On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 5  Each case contains:

    * Line 1: one space-separated integers: N and M      (1 ≤ N ≤ 1000 , 1≤ M ≤ 500).

    * Lines 2..N+1:  Line i+1 contains a single integer: Mi  (1 ≤ Mi ≤ 1000)

    Output

    For each test case, output a line with the minimum time it takes for Afandi to get all of the sheep across the river.

    Sample Input

    2
    2 10
    3
    5
    5 10
    3
    4
    6
    100
    1

    Sample Output

    18
    50

    思路:注意一句话,When Afandi is on the raft alone, it can cross the river in M minutes When the i sheep are added, it takes Mi minutes longer to cross the river than with i-1 sheep (i.e., total M+M1   minutes with one sheep, M+M1+M2 with two, etc.).
    这句话有点迷。说明并没有对羊进行编号。假如第一只,第二只运过去,回来运第三只羊,花费还是按照第一只的算,以此类推。
    另dp[i]为当运送第i只羊的最小花费,那么相当于它是由两种状态转移来的,一种是前i只羊一起运送,一种就是前j只羊一起运送,然后i-j到i只羊为第二批再运送一次,两者加和得到的较小值。
    即dp[i]=min(dp[i],dp[i-j]+dp[j]+m)。

    代码:
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<string>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    using namespace std;
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            int n,m;
            scanf("%d %d",&n,&m);
            int dp[n+1],sum[n+1];
            fill(dp,dp+n+1,0);
            fill(sum,sum+n+1,0);
            for(int i=1;i<=n;i++) {
                scanf("%d",&sum[i]);sum[i]+=sum[i-1];
            }
            for(int i=1;i<=n;i++) {
                dp[i]=sum[i]+m;
                for(int j=1;j<=i;j++) {
                    dp[i]=min(dp[i],dp[j]+dp[i-j]+m);
                }
            }
            printf("%d
    ",dp[n]);
        }
        return 0;
    }
    




  • 相关阅读:
    「疫期集训day7」周期
    「字符串」哈希板子
    「疫期集训day6」雨林
    「疫期集训day5」火焰
    「数据结构」对顶堆
    「STL中的常用函数 容器」
    「单调队列优化DP」P2034 选择数字
    bootstrap table使用及遇到的问题
    ArcGIS栅格影像怎么从WGS84地理坐标转成Xian80投影坐标
    arcgis如何求两个栅格数据集的差集
  • 原文地址:https://www.cnblogs.com/lemonbiscuit/p/7775990.html
Copyright © 2011-2022 走看看