ny716 River Crossing
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
题目讲解:我和别人讨论很久才弄懂题目,有的说和过河问题很像,但是两个意思不同,又有很大的差别
我还是讲解一下大概意思吧,题中说道阿凡提需要把羊送到对面去,但是呢他只有一条船,而且他也可以选择每次带几个羊过去,
当然了,也可以一次性的带走完,带不完的话还要回来再带走,这样的话回来花的时间也要加上了,需要注意的是每个并羊没有对应的时间,
它所需要的时间取决于在这个时间顺序中她是第几个上船的;
下面看一下案列1:很显然羊是一下被带走完的,时间是3+5+10(自己的时间)=18分钟;
然而案例二却看不懂了吧,我就详细分析一下:首先是五个羊当然会有五个时间了,你有很多选择,一次带走完,一个一个的....
如果一次一个时间是多少呢?第一次走:3+10 回来:10 然后第二次再走:3+10(或许就在这疑惑了,怎么还是3呢为什么不是4呢,我说了羊没有对应的时间,
第一个上船的时间就是3,如果运两个羊第二个时间是4,然后回来后,又重新开始第一个,第二个....)然后又回来:10 第三次再走:3+10 回来:10 第四次再走3+10 回来:10
第五次再走:3+10 所以共:3+10 +10 +3+10 +10 +3+10 +10 +3+10 +10 +3+10=105分钟;
最优解应该是这个样子的第一次带三个羊:3+4+6+10 回来:10 再带走两个羊:3+4+10 共23+10+17=50分钟
参考代码如下:参考的大神的#include<iostream>
#include<cstdio>
using namespace std;
const int mmax=0x3fffffff;
int a[],mmin[],m;
int dp(int n)
{
if(n<=)
return ;
if(mmin[n]!=mmax)
return mmin[n];
int ans=mmax;
for(int i=;i<=n;i++)
{
if(dp(n-i)+a[i]+m<ans)
ans=dp(n-i)+a[i]+m;
}
return mmin[n]=ans;
}
int main()
{
int i,j,k,t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
a[]=m;
for(i=;i<=n;i++)
{
scanf("%d",&a[i]);
a[i]+=a[i-];
mmin[i]=mmax;
}
printf("%d\n",dp(n)-m);
}
return ;
}代码二:
#include <iostream>
#include <algorithm>
#include <fstream>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int n,k,f[],ans[],l;
int i,m,j,temp;
scanf("%d",&k);
for(i=;i<=k;i++)
{
memset(f,,sizeof(f));
memset(ans,,sizeof(ans));
scanf("%d %d",&n,&m);
f[]=m;
for(j=;j<=n;j++)
{ scanf("%d",&temp);
f[j]=f[j-]+temp;
}
ans[]=f[];
for(j=;j<=n;j++)
{
ans[j]=f[j];
for(l=;l<j;l++)
if(ans[l]+ans[j-l]+m<ans[j])
ans[j]=ans[l]+ans[j-l]+m;
}
printf("%d\n",ans[n]); }
return ;
}
ny716 River Crossing的更多相关文章
- BZOJ1617: [Usaco2008 Mar]River Crossing渡河问题
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 654 Solved: 4 ...
- BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题( dp )
dp[ i ] = max( dp[ j ] + sum( M_1 ~ M_( i - j ) ) + M , sum( M_1 ~ M_i ) ) ( 1 <= j < i ) 表示运 ...
- BZOJ 1617: [Usaco2008 Mar]River Crossing渡河问题
题目 1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer ...
- 第六届河南省赛 River Crossing 简单DP
1488: River Crossing Time Limit: 1 Sec Memory Limit: 128 MB Submit: 83 Solved: 42 SubmitStatusWeb ...
- bzoj1617 / P2904 [USACO08MAR]跨河River Crossing
P2904 [USACO08MAR]跨河River Crossing 显然的dp 设$f[i]$表示运走$i$头奶牛,木筏停在未过河奶牛一侧所用的最小代价 $s[i]$表示一次运$i$头奶牛到对面的代 ...
- 1617: [Usaco2008 Mar]River Crossing渡河问题(dp)
1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1219 Solved: ...
- [bzoj1617][Usaco2008 Mar]River Crossing渡河问题_动态规划
River Crossing渡河问题 bzoj-1617 Usaco-2008 Mar 题目大意:题目链接. 注释:略. 想法:zcs0724出考试题的时候并没有发现这题我做过... 先把m求前缀和, ...
- USACO River Crossing
洛谷 P2904 [USACO08MAR]跨河River Crossing https://www.luogu.org/problem/P2904 JDOJ 2574: USACO 2008 Mar ...
- P2904 [USACO08MAR]跨河River Crossing
题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when ...
随机推荐
- Hadoop,HBase集群环境搭建的问题集锦(二)
10.艾玛, Datanode也启动不了了? 找到log: Caused by: java.net.UnknownHostException: Invalid host name: local hos ...
- Strom优化指南
摘要:本文主要讲了笔者使用Strom中的一些优化建议 1.使用rebalance命令动态调整并发度 Storm计算以topology为单位,topology提交到Storm集群中运行后,通过storm ...
- SVN mime-type 笔记
背景: 1.最近使用执行svn diff的时候发现有些文本文件无法显示: 2.浏览器会通过判断获取文件的 MIME 类型, 调用不同的客户端程序或使用不同的方式来执行.如果文件的 MIME 缺失或者有 ...
- 解决win10 获取不到ip
1.检查Dhcp服务是否启动 2.关闭防火墙
- ubuntu中设置xampp开机启动
sudo ln -s /opt/lampp/lampp /etc/init.d/lampp sudo update-rc.d -f lampp defaults
- 【转】Asp.Net MVC4 之Url路由
MVC4常见路由的处理方式 //直接方法重载+匿名对象 routes.MapRoute( name: "Default", url: "{controller}/{act ...
- python之函数用法isinstance()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法isinstance() #isinstance() #说明:返回一个布尔值,判断数据 ...
- 如何使用jmeter来实现更大批量的并发的解决方案
近期在用JMeter进行负载测试的 时候,发现使用单台机器模拟测试超过比如500个进程的并发就有些力不从心或者说不能如实的反应实际情况,在执行的过程中,JMeter自身会自动关闭, 要解决这个问题,则 ...
- HDUOJ --2566
统计硬币 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- vscode自定义背景颜色
vscode自定义背景颜色 大概做前端的builder(只会打代码的才是coder,嘻嘻~)一半以上都会使用vscode编辑代码吧,vscode很轻量,支持的文件拖拽加入编辑区功能我个人认为很方便 ...