BUYING FEED
Problem F: F BUYING FEED
Description
Farmer John needs to travel to town to pick up K (1 <= K <= 100)pounds of feed. Driving D miles with K pounds of feed in his truck costs D*K cents.
The county feed lot has N (1 <= N <= 100) stores (conveniently numbered 1..N) that sell feed. Each store is located on a segment of the X axis whose length is E (1 <= E <= 350). Store i is at
location X_i (0 < X_i < E) on the number line and can sell John as much 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 on the X axis might have more than one store.
Farmer John starts at location 0 on this number line and can drive only in the positive direction, ultimately arriving at location E, with at least K pounds of feed. He can stop at any of the feed stores along the way and buy any amount of feed up to the the store's limit.
What is the minimum amount Farmer John has to pay to buy and transport the K pounds of feed? Farmer John knows there is a solution.
Consider a sample where Farmer John needs two pounds of feed from three stores (locations: 1, 3, and 4) on a number line whose range is 0..5:
0 1 2 3 4 5
---------------------------------
1 1 1 Available pounds of feed
1 2 2 Cents per pound
It is best for John to buy one pound of feed from both the second and third stores. He must pay two cents to buy each pound of feed for a total cost of 4. When John travels from 3 to 4 he is moving 1 unit of length and he has 1 pound of feed so he must pay 1*1 = 1 cents.
When John travels from 4 to 5 he is moving one unit and he has 2 pounds of feed so he must pay 1*2 = 2 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: Xi Fi Ci
Output
A single integer that is the minimum cost for FJ to buy and transport the feed
Sample Input
3 1 2
4 1 2
1 1 1
Sample Output
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1010;
struct node{
int xi;
int per;
int w;
}stu[MAX];
int n, e, k;
bool cmp(node a, node b) {//让每个点都买单位数量到终点的价格作为单价排序
return n-a.xi+a.per < n-b.xi+b.per;
}
int main() {
while(~scanf("%d%d%d", &k, &e, &n)) {
for(int i = 0; i < n; i++) {
scanf("%d%d%d", &stu[i].xi, &stu[i].w, &stu[i].per);
}
sort(stu, stu+n, cmp);
int sum = 0;//贪心 放包问题
for(int i = 0; i < n; i++) {
if(k >= stu[i].w) {
k -= stu[i].w;
sum += (e-stu[i].xi)*stu[i].w+stu[i].w*stu[i].per;
} else {
sum += (e-stu[i].xi)*k+k*stu[i].per;
k = 0;
}
if(k == 0)
break;
}
printf("%d\n", sum);
}
}
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
const int MAX = 200;
struct node
{
int w;
int per;
};
int k,e,n;
int dp[400][105];// dp[x][w] 其中x为当前坐标,w为已购买数量
vector<node>edge[400];
int main()
{
while(~scanf("%d %d %d",&k,&e,&n))
{
fill(dp[0], dp[0]+MAX*MAX, INF);
for(int i=0;i<n;i++)
{
int xi;
node temp;
scanf("%d %d %d",&xi,&temp.w,&temp.per);
edge[xi].push_back(temp);//一点可能多商店
}
dp[0][0]=0;//边界 //求每种状态
for(int i=1;i<=e;i++)
{
for(int l=k;l>=0;l--)
{
dp[i][l]=dp[i-1][l];//先继承一下,再去更新
for(int j=0;j<edge[i-1].size();j++)//前面那个点的所有商店,跑一遍
{
int Per=edge[i-1][j].per;//当前单价
int W=edge[i-1][j].w;//当前数量
for(int p=0;p<=W;p++)//购买l和w 以内的 物品 的花费
if(l>=p)
dp[i][l]=min(dp[i][l],dp[i-1][l-p]+p*Per+(e-i+1)*p);
//当前状态的最小花费,要么不买, 要么在前一个的状态中找一个能达到该状态,并且花费小的
}
}
}
printf("%d\n",dp[e][k]);//到达5点,购买k的花费就是答案。
}
return 0;
}
BUYING FEED的更多相关文章
- ACM BUYING FEED
BUYING FEED 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 Farmer John needs to travel to town to pick up ...
- 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 ...
- USACO Buying Feed, II
洛谷 P2616 [USACO10JAN]购买饲料II Buying Feed, II 洛谷传送门 JDOJ 2671: USACO 2010 Jan Silver 2.Buying Feed, II ...
- 【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- ...
- Buying Feed, 2010 Nov (单调队列优化DP)
约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里,这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算买 ...
- [河南省ACM省赛-第三届] BUYING FEED (nyoj 248)
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> us ...
- 【BZOJ2059】Buying Feed 购买饲料
题面 约翰开车来到镇上,他要带V吨饲料回家.如果他的车上有X吨饲料,每公里就要花费X^2元,开车D公里就需要D* X^2元.约翰可以从N家商店购买饲料,所有商店都在一个坐标轴上,第i家店的位置是Xi, ...
随机推荐
- cudnn 卷积例子
运行环境:linux cuda cudnn cudnn API:https://docs.nvidia.com/deeplearning/sdk/cudnn-developer-guide/index ...
- Java中Calendar/SimpleDateFormat/Date常用方法总结
//获取当前时刻yyyy-MM-dd HH:mm:ss Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new S ...
- CSS之EM相对单位
之前以为em单位只是在font-size中起到继承作用, 后来慢慢觉得,继承,应该是在几乎所有样式中都可以是实现的,比如height,width,border... 今天才简单测试了下,果真是可以实现 ...
- vmware的双网卡以及Pro的注册码
DC/OS的master需要能够上外网而且能够和本地内网设备交互,于是打算在虚拟机上面做测试,于是调研了一下虚拟机的双网卡配置. 最推荐的方式是使用vmware的station,而不是player ...
- Sentry的要点
1.Apache的Build 在研究Sentry的时候,发现没有bin.jar,只能手工编辑工程,但是编辑发现很多jar包有问题:在访问官网的时候发现左侧菜单中有一项是builds,点开后(https ...
- 开启现有android项目
开启工程 在屏幕上方的选单列上,选择「File->New->Project」, 会弹出「New Project」对话视窗.Eclipse 是通用的编辑环境,可根据你所安装的不同扩充套件而支 ...
- Hibernate Validator--创建自己的约束规则
尽管Bean Validation API定义了一大堆标准的约束条件, 但是肯定还是有这些约束不能满足我们需求的时候, 在这种情况下, 你可以根据你的特定的校验需求来创建自己的约束条件. 3.1. 创 ...
- 杂项-Log:log4net
ylbtech-杂项-Log:log4net log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等) ...
- 在Altium Designer 2009下如何添加Logo图
最近用Altium Designer 2013(14.2)绘制PCB,之后想在板子上放置一个LOGO图.要是用Altium Designer10以前的版本,过程也很简单,可在用Altium Desig ...
- TS学习之解构与展开
一.解构 1.解构数组 let input = [1, 2]; let [first, second] = input; console.log(first); // outputs 1 consol ...