题目描述

 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. Python 扩展知识

    Python 练习题 Python 编程习惯 Python 转义字符 Python 格式化输出 Python 列表表达式 Python 生成器表达式 Python 序列化 Python2 与 Pyth ...

  2. Lua脚本和C++交互(一)

    现在,越来越多的C++服务器和客户端融入了脚本的支持,尤其在网游领域,脚本语言已经渗透到了方方面面,比如你可以在你的客户端增加一个脚本,这个脚本将会帮你在界面上显示新的数据,亦或帮你完成某些任务,亦或 ...

  3. MySQL按照汉字拼音首字母排序

    按照汉字的拼音排序,用的比较多是在人名的排序中,按照姓氏的拼音字母,从A到Z排序: 如果存储姓名的字段采用的是GBK字符集,那就好办了,因为GBK内码编码时本身就采用了拼音排序的方法(常用一级汉字37 ...

  4. 《转载》Eclipse项目上传码云

    本文转载自http://blog.csdn.net/izzyliao/article/details/53074452 把Eclipse项目上传到码云的步骤: 1.登录码云:新建项目 2.输入项目名: ...

  5. 在apache虚拟目录配置

    在apache虚拟目录配置中 <VirtualHost *:80>xxx xxx xxx</VirtualHost> 不能写成 <VirtualHost *>xxx ...

  6. Qt编写的RTSP播放器+视频监控(android版本)

    之前写过vlc版本,ffmpeg版本,也在linux上和嵌入式linux上跑过视频监控,这次想直接用ffmpeg的库写个android版本,qt+ffmpeg+android直接用之前的qt+ffmp ...

  7. echarts - 特殊需求实现方案汇总

    五分钟上手echarts echarts中 设置x||y轴文案.提示文字等为固定字数,超出显示"..." 关于echarts下钻功能的一些总结.js echarts - 特殊需求实 ...

  8. Visual Studio for Mac离线安装教程

    Visual Studio for Mac离线安装教程 可以在线安装,也可以离线安装(本次安装博主使用离线,在线安装失败了) 据说翻个墙就可以,有条件的就翻吧 没条件的我于是选择离线安装………… 离线 ...

  9. 原生js--类的扩充和类型检测

    扩充类的方法: 1.向原型对象上添加方法或属性.例如:Number.prototype.cl = function(){}; 但这种做法并不推荐,因为ES5之前,无法将这些新添加的方法或属性设置为不可 ...

  10. Elasticsearch学习之深入搜索三 --- best fields策略

    1. 为帖子数据增加content字段 POST /forum/article/_bulk { "} } { "doc" : {"content" : ...