题目描述

 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).

输入

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)

输出

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

样例输入

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

样例输出

18
50 要认真读题才能看懂题意:
题中说的是如果第一次带两只羊,时间是m+m1+m2;第二次带一只羊用时m1(第一只羊的m);第三次带2只羊用m1+m2(第一只羊和第二只羊的m);
例如第二组数据:第一次带3+4+6+10=23;第二次3+4+10=17;共用时23+17+10=50;
用一个前缀和数组time,time[i]表示单独运送i只羊所花费的时间。
dp[i]表示一个人和i只羊过河所花费的最短时间,则开始时dp[i] = time[i] + M,
以后更新时,dp[i] = min(dp[i],dp[i-j] + m + dp[j]),
j从1循环到i-1,即把i只羊分成两个阶段来运,只需求出这两个阶段的和,
然后加上人从对岸回来所用的时间,与dp[i]进行比较,取最小值。
#include<stdio.h>
#include<algorithm>
using namespace std;
int dp[], time[];
int main()
{
int T, n, m, i, j;
scanf("%d",&T);
while(T--)
{
int a;
scanf("%d%d",&n, &m);
time[] = ;
for(i = ; i <= n; i++)
{
scanf("%d",&a);
time[i] = time[i-] + a;
}
for(i = ; i <= n; i++)
{
dp[i] = time[i] + m;
for(j = ; j < i; j++)
dp[i] = min(dp[i], dp[i-j] + dp[j] + m);
}
printf("%d\n",dp[n]);
}
return ;
}
//time[i]表示一次运送i只羊所花费的时间 //dp[i]表示人和i只羊一起过河所花费的最短时间

River Crossing---河南省第六届大学生程序设计竞赛的更多相关文章

  1. Contest - 中南大学第六届大学生程序设计竞赛(Semilive)

    题1:1160十进制-十六进制 注意他给的数据范围 2^31,int是 2^31-1 #include<iostream> using namespace std; int main() ...

  2. 校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛(Problem E)

    Problem E Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  4. 河南省第十一届ACM大学生程序设计竞赛

    nyoj-1365-山区修路 内存限制:128MB 时间限制:3000ms 特判: No通过数:4 提交数:4 难度:3 题目描述: SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间, ...

  5. 河南省第六届ACM程序设计大赛

    C:  最舒适的路线 (并查集) #include<cstdio> #include<cstring> #include<iostream> #include< ...

  6. 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)

    Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...

  7. XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)

    Problem Description The famous "Alice and Bob" are playing a game again. So now comes the ...

  8. 黑龙江省第七届大学生程序设计竞赛-Mysterious Organization

    描述 GFW had intercepted billions of illegal links successfully. It has much more effect. Today, GFW i ...

  9. 黑龙江省第七届大学生程序设计竞赛-Heap

    描述 A heap is a full binary tree; for each node, its key is greater than its two sub-node’s key. Two ...

随机推荐

  1. PHP代码审计笔记--CSRF漏洞

    0x01 前言 CSRF(Cross-site request forgery)跨站请求伪造.攻击者盗用了你的身份,以你的名义向第三方网站发送恶意请求,对服务器来说这个请求是完全合法的,但是却完成了攻 ...

  2. 51单片机的idata,xdata,pdata,data的详解(转)

    data: 固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码也最小. bit :是指0x20-0x2f的可位寻址区idata:固定指前面0x00-0xff的2 ...

  3. php危险的函数和类 disable_functions/class

    phpinfo()功能描述:输出 PHP 环境信息以及相关的模块.WEB 环境等信息.危险等级:中 passthru()功能描述:允许执行一个外部程序并回显输出,类似于 exec().危险等级:高 e ...

  4. git切换分支(自记)

    git fetch git checkout feature/A4-page

  5. Cufon在渲染网页字体你不知道的事

    清单 1. 无效的 font-family 字体指定 <style> .introduction { font-family:'Baroque Script';} </style&g ...

  6. 自己实现atoi

    bool myatoi(const char *s,int &num) { cout<<(&s)<<endl; num=; while (*s) { ') { ...

  7. DOS 如何取当前时间做为文件名?

    如果要取得以日期为文件名的文件,假设在命令行下键入date返回形式为:当前日期: 2005-06-02 星期四echo > %date:~0,4%%date:~5,2%%date:~8,2%~表 ...

  8. Excel 2010 得到当天的日期/得到一年中的第几周/得到当前一周中的星期几

    =TODAY() ="第"&WEEKNUM(TODAY())&"周" =TEXT(TODAY(),"aaaa") Ctrl ...

  9. Elasticsearch学习之深入搜索六 --- 平衡搜索结果的精准率和召回率

    1. 召回率和精准度 比如你搜索一个java spark,总共有100个doc,能返回多少个doc作为结果,就是召回率,recall 精准度,比如你搜索一个java spark,能不能尽可能让包含ja ...

  10. create-react-app中img图片不现实

    场景:正常的情况下是这么引用图片,我的图片路径是 src/images/login-from-icon1.png <img src="../images/login-from-icon ...