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 ...
随机推荐
- pyhton验证码识别
1.PIL 下载地址: PIL-1.1.7.win-amd64-py2.7.exe 2.tesseract-ocr下载地址: tesseract-ocr-setup-3.02.02.exe 3.pyt ...
- Nginx 访问控制
根据 IP 限制访问: location /admin/ { allow 192.168.1.1; allow 192.168.1.2; deny all; } 根据正则限制访问: location ...
- goto的用法
using UnityEngine; using System.Collections; public class goto1 : MonoBehaviour { public bool can = ...
- [Cubieboard] Node.js 在 Lubuntu 上安装指南
在 Ubuntu 下安装 Node.js sudo apt-get install python-software-properties python g++ make sudo add-apt-re ...
- java虚拟机性能监控调优及原则
摘抄 http://uule.iteye.com/blog/2114697 一.JVM内存模型及垃圾收集算法 1.根据Java虚拟机规范,JVM将内存划分为: New(年轻代) Tenured(年老 ...
- hadoop自动提交脚本
自动提交到hadoop系统,然后调用wordcount的任务,并下载输出的文件. #!/bin/sh #从给定的路径获取文件列表,提交到hadoop系统,使用wordcount的功能统计单词数量 #e ...
- input回车问题
今天有一个问题,就是input对象没有加任何事件自己回车导致跳到了404页面.处理的时候,并发现没找到回车事件的控制. 那么只有一种情况,就是自带的回车控制. 百度了一下,如下面博文里面的写法.我这边 ...
- Linux设备驱动剖析之IIC(一)
写在前面 由于IIC总线只需要两根线就可以完成读写操作,而且通信协议简单,一条总线上可以挂载多个设备,因此被广泛使用.但是IIC总线有一个缺点,就是传输速率比较低.本文基于Linux-2.6.36版本 ...
- WP8.1学习系列(第二十二章)——在页面之间导航
在本文中 先决条件 创建导航应用 Frame 和 Page 类 页面模板中的导航支持 在页面之间传递信息 缓存页面 摘要 后续步骤 相关主题 重要的 API Page Frame Navigation ...
- python 函数式编程:高阶函数,map/reduce
python 函数式编程:高阶函数,map/reduce #函数式编程 #函数式编程一个特点就是,允许把函数本身作为参数传入另一个函数,还允许返回一个函数 #(一)高阶函数 f=abs f print ...