River Crossing---河南省第六届大学生程序设计竞赛
题目描述
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---河南省第六届大学生程序设计竞赛的更多相关文章
- Contest - 中南大学第六届大学生程序设计竞赛(Semilive)
题1:1160十进制-十六进制 注意他给的数据范围 2^31,int是 2^31-1 #include<iostream> using namespace std; int main() ...
- 校第十六届大学生程序设计竞赛暨2016省赛集训队选拔赛(Problem E)
Problem E Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- 河南省第十一届ACM大学生程序设计竞赛
nyoj-1365-山区修路 内存限制:128MB 时间限制:3000ms 特判: No通过数:4 提交数:4 难度:3 题目描述: SNJ位于HB省西部一片群峰耸立的高大山地,横亘于A江.B水之间, ...
- 河南省第六届ACM程序设计大赛
C: 最舒适的路线 (并查集) #include<cstdio> #include<cstring> #include<iostream> #include< ...
- 湖南省第六届大学生程序设计大赛原题 F Biggest Number (UVA1182)
Biggest Number http://acm.hust.edu.cn/vjudge/contest/view.action?cid=30851#problem/F 解题思路:DFS(检索)+BF ...
- 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 ...
- 黑龙江省第七届大学生程序设计竞赛-Mysterious Organization
描述 GFW had intercepted billions of illegal links successfully. It has much more effect. Today, GFW i ...
- 黑龙江省第七届大学生程序设计竞赛-Heap
描述 A heap is a full binary tree; for each node, its key is greater than its two sub-node’s key. Two ...
随机推荐
- 【代码审计】QYKCMS_v4.3.2 任意文件读取漏洞分析
0x00 环境准备 QYKCMS官网:http://www.qykcms.com/ 网站源码版本:QYKCMS_v4.3.2(企业站主题) 程序源码下载:http://bbs.qingyunke. ...
- iOS开发--libxml/HTMLparser.h file not found 解决方法 (libxml.dylib错误处理)
点击左边项目的根目录,再点击右边的Build Settings,手工输入文字:“Header search paths”,然后单击(或双击,点击弹出面板下面的“+”号进行添加)“ Header sea ...
- 使用pyinotify监控文件系统的变化
pyinotify依赖Linux内核inotify功能,它需要在2.6.13版本的内核的Linux系统上运行. 1. 安装pyinotify pip install pyinotify 安装完后可以直 ...
- 如何在Windows系统上利用Telnet协议连接Linux服务器
Telnet协议是Internet远程登录服务的标准协议,它为用户提供了在本地计算机上完成远程主机工作的能力.很多终端使用者都习惯在计算机上利用Telnet会话来远程控制服务器.这里小编就分两步为大家 ...
- 十款不错的Hybrid App移动开发框架
本文转载至http://www.pureasme.com/blog/2015/0419476.html ionic 是个高级的 HTML5 移动端应用框架,是个很漂亮的使用 HTML5 开发混合移动应 ...
- MyEclipse 10 下在线安装插件
昨天不知道怎么就删除了电脑中的eclipse 我x,还原不回来了. 今天就安装了最新版本的myeclipse10,大家都知道,MyEclipse 中有一个烦人的 Software and Worksp ...
- liunx trac 插件使用之DateFieldPlugin
插件GanttCalendarPlugin安装完以后,有一个问题,就是在选择起始与结束时间的时候,为了方便有datepicker功能,如图 需要用到插件DateFieldPlugin,官网链接http ...
- 分布式实时日志系统(一)环境搭建之 Jstorm 集群搭建过程/Jstorm集群一键安装部署
最近公司业务数据量越来越大,以前的基于消息队列的日志系统越来越难以满足目前的业务量,表现为消息积压,日志延迟,日志存储日期过短,所以,我们开始着手要重新设计这块,业界已经有了比较成熟的流程,即基于流式 ...
- jQuery Sizzle选择器(三)
在Sizzle的入口方法Sizzle()中看到的一个根据浏览器来初始化document各个方法的函数setDocument(),接下来主要看一下这个方法都做了什么. 但之前有必要看一下它用到的一些Si ...
- slave库写redo、binlog不实时丢数据的场景
1.slave涉及相关文件 slave读取master的binlog日志后,需要落地3个文件:relay log.relay log info.master info: relay log: 即读取过 ...