USACO Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II
Description
Farmer John needs to travel to town to pick up K (1 <= K <= 100)
The county feed lot has N (1 <= N <= 100) stores (convenientlynumbered 1..N) that sell feed. Each store is located on a segmentof the X axis whose length is E (1 <= E <= 350). Store i is atlocation X_i (0 < X_i < E) on the number line and can sell FJ asmuch as F_i (1 <= F_i <= 100) pounds of feed at a cost of C_i (1<= C_i <= 1,000,000) cents per pound. Amazingly, a given point onthe X axis might have more than one store.
What is the minimum amount FJ has to pay to buy and transport theK pounds of feed? FJ knows there is a solution.
It is best for FJ to buy one pound of feed from both the second andthird stores. He must pay two cents to buy each pound of feed fora total cost of 4. When FJ travels from 3 to 4 he is moving 1 unitof length and he has 1 pound of feed so he must pay 1*1 = 1 cents.
The total cost is 4+1+2 = 7 cents.
Input
* Line 1: Three space-separated integers: K, E, and N
* Lines 2..N+1: Line i+1 contains three space-separated integers: X_i,
F_i, and C_i
Output
* Line 1: A single integer that is the minimum cost for FJ to buy and
transport the feed
Sample Input
2 5 3 3 1 2 4 1 2 1 1 1
Sample Output
7
题目翻译:
FJ开车去买K份食物,如果他的车上有X份食物。每走一里就花费X元。 FJ的城市是一条线,总共E里路,有E+1个地方,标号0~E。 FJ从0开始走,到E结束(不能往回走),要买K份食物。 城里有N个商店,每个商店的位置是X_i(一个点上可能有多个商店),有F_i份食物,每份C_i元。 问到达E并买K份食物的最小花费。
题解:
转化成多重背包问题求解。
多重背包就是完全背包的升级版,从每种物品有无限件可以用变成有一个固定的数量,也可以用二进制优化一下,详见具体讲解。
本题可以采用这种动态规划的方式AC
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
int dp[105];
int main()
{
int n,e,k,x,f,c;
scanf("%d%d%d",&k,&e,&n);
for(int i=1;i<=k;i++)
dp[i]=1<<30;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&x,&f,&c);
for(int j=k;j>=1;j--)
for(int l=1;l<=f;l++)
{
if(l>j)
break;
dp[j]=min(dp[j],dp[j-l]+l*(c+e-x));
}
}
printf("%d",dp[k]);
return 0;
}
USACO Buying Feed, II的更多相关文章
- 2020: [Usaco2010 Jan]Buying Feed, II
2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 220 Solved: 162[ ...
- 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...
- 【P2616】 【USACO10JAN】购买饲料II Buying Feed, II
P2616 [USACO10JAN]购买饲料II Buying Feed, II 题目描述 Farmer John needs to travel to town to pick up K (1 &l ...
- 【BZOJ】2020: [Usaco2010 Jan]Buying Feed, II (dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=2020 和背包差不多 同样滚动数组 f[j]表示当前位置j份食物的最小价值 f[j]=min(f[j- ...
- BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020 题意: FJ开车去买K份食物. 如果他的车上有X份食物,每走一里就花费X元. FJ的 ...
- BZOJ2020: [Usaco2010 Jan]Buying Feed II
[传送门:BZOJ2020] 简要题意: 约翰开车回家,遇到了双十一节,那么就顺路买点饲料吧.回家的路程一共有E 公里,这一路上会经过N 家商店,第i 家店里有Fi 吨饲料,售价为每吨Ci 元.约翰打 ...
- ACM BUYING FEED
BUYING FEED 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 Farmer John needs to travel to town to pick up ...
- BUYING FEED
Problem F: F BUYING FEED Description Farmer John needs to travel to town to pick up K (1 <= K < ...
- USACO Buying Hay
洛谷 P2918 [USACO08NOV]买干草Buying Hay https://www.luogu.org/problem/P2918 JDOJ 2592: USACO 2008 Nov Sil ...
随机推荐
- Ubuntu安装微信、钉钉等各种windows软件
详见这个博客,用sudo dpkg -i 安装软件时,如果出现错误,是因为缺少安装依赖关系,用下面的命令解决: sudo apt-get install -f
- oracle--错误笔记(二)--ORA-16014
ORA-16014错误解决办法 01.问题以及解决过程 SQL> select status from v$instance; STATUS ------------ MOUNTED SQL&g ...
- Golang 基础语法介绍及对比(二)
传值与传参 Golong func main() { a := fmt.Println("a = ", a) // 应该输出 "a= 3" a1 := add1 ...
- STM32Cube生成的HID项目,找不到hUsbDeviceFS
症状 在main中尝试发消息给上位机: 解决方法 在STM32生成的HID项目里,默认是没有把hUsbDeviceFS导出的,需要修改usb_device.h文件,在USER CODE BEGIN V ...
- 了解jsp,这一篇就够了.
jsp的执行过程: 1 客户端发出请求. 2 Web容器将JSP转译成Servlet源代码. 3 Web容器将产生的源代码进行编译. 4 Web容器加载编译后的代码并执行. 5 把执行结果响应至客户端 ...
- PHP命令执行php文件需要注意的问题
PHP命令执行php文件需要注意的问题 require_once '/data/fewfawef/wwwroot/Public/queenchuli/common/mysql.php';里面必须要写绝 ...
- JS 学习笔记
在JS中两个对象不能用“==” 或者“===” 来比较,如果硬是要比较的话,始终返回的是false var x = new String("Bill"); var y = new ...
- 【ELK】7. elasticsearch linux上操作es命令详解
========== 1.检查ES节点是否正常启动 curl http://192.168.6.16:9200 正常状态: 非正常状态: 1>确保服务是不是正常启动了,端口用的是哪个 2> ...
- 实现个虚拟机只要几百行的 toy 版就够了
关键是 指令 的 eval 并把 高层代码进行翻译. 典型的项目: 1. java-compiler (C++) 2. 手把手教你构建 C 语言编译器(0)- 前言 | 三点水 (C)
- javascript(六)运算符
运算符概述 JavaScript中的运算符用于算术表达式. 比较表达式. 逻辑表达式. 赋值表达式等.需要注意的是, 大多数运算符都是由标点符号表示的, 比如 "+" 和" ...