zoukankan      html  css  js  c++  java
  • 洛谷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
    ",f[ n ]-m) ; 
    39     
    40     return 0 ; 
    41 }
  • 相关阅读:
    防止头文件的重复包含问题
    git常用命令
    redis
    linux常用操作
    数据库安装
    mysql修改表结构
    mysql 忘记root密码及授权访问
    mysql连表查询
    mysql 存取ip方法
    php批量修改表结构
  • 原文地址:https://www.cnblogs.com/third2333/p/6944851.html
Copyright © 2011-2022 走看看