洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II

洛谷传送门

JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II

JDOJ传送门

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的更多相关文章

  1. 2020: [Usaco2010 Jan]Buying Feed, II

    2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[ ...

  2. 洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II

    洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II https://www.luogu.org/problemnew/show/P2616 题目描述 Farmer ...

  3. 【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 ...

  4. 【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- ...

  5. BZOJ 2020 [Usaco2010 Jan]Buying Feed,II:贪心【定义价值】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2020 题意: FJ开车去买K份食物. 如果他的车上有X份食物,每走一里就花费X元. FJ的 ...

  6. BZOJ2020: [Usaco2010 Jan]Buying Feed II

    [传送门:BZOJ2020] 简要题意: 约翰开车回家,遇到了双十一节,那么就顺路买点饲料吧.回家的路程一共有E 公里,这一路上会经过N 家商店,第i 家店里有Fi 吨饲料,售价为每吨Ci 元.约翰打 ...

  7. ACM BUYING FEED

    BUYING FEED 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 Farmer John needs to travel to town to pick up ...

  8. BUYING FEED

    Problem F: F BUYING FEED Description Farmer John needs to travel to town to pick up K (1 <= K < ...

  9. USACO Buying Hay

    洛谷 P2918 [USACO08NOV]买干草Buying Hay https://www.luogu.org/problem/P2918 JDOJ 2592: USACO 2008 Nov Sil ...

随机推荐

  1. 基于sign的pose-graph残差函数构建

    沿sign的法线方向优化车辆位姿,优化目标函数也即残差函数构建过程如下:

  2. 阿里云等云端服务器VPS改造为ROS-Routeros,最大化利用云端硬盘

    因为ROS官方提供的CHR镜像是用16G版本做出来的,所以我们用dd的方法做出来的ROS,空间都是16G. 如果你的云端是40G空间或者更多呢,也只用了16G空间,是不是很浪费. 我今天看到一篇大神写 ...

  3. nginx 目录自动加斜线”/”

    默认配置当你访问http://abc.example.com/dir 时不会加”/” 常见做法 if (-d $request_filename){  rewrite ^/(.*)([^/])$ ht ...

  4. UVA 10924 Prime Words 题解

    Prime Words A prime number is a number that has only two divisors: itself and the number one. Exampl ...

  5. Python课程第六天作业

    1.以自己的理解总结为什么会出现循环导入,并用代码举例说明 循环导入报错并不是应为相互之间引用而导致报错,实际上是在导入一个模块时发现引用的模块不存在 示例如下: m1.py from m2 impo ...

  6. golang --os系统包详解

    环境变量 Environ 获取所有环境变量, 返回变量列表 func Environ() []string package main import ( "fmt" "os ...

  7. WPF 精修篇 多属性触发器

    原文:WPF 精修篇 多属性触发器 多属性触发器就是多个属性都满足在触发 在属性触发器上加了一些逻辑判断 举栗子 这个栗子里  textBox 要满足俩个条件 才能触发背景变色 1)textbox的 ...

  8. C# 调用打印机打印文件

    C# 调用打印机打印文件,通常情况下,例如Word.Excel.PDF等可以使用一些对应的组件进行打印,另一个通用的方式是直接启用一个打印的进程进行打印.示例代码如下: using System.Di ...

  9. windows10 edge浏览器关闭没有提示如何解决

    通常情况下,用户在win10 edge中打开多个窗口,点击关闭按钮就会出现“要关闭所有标签”提示,然后可以点击 取消来取消关闭edge.不过,最近一位windows10系统用户反馈自己在一次勾选“总是 ...

  10. SpringApplication到底run了什么(下)

    在上篇文章中SpringApplication到底run了什么(上)中,我们分析了下面这个run方法的前半部分,本篇文章继续开工 public ConfigurableApplicationConte ...