nyoj248 BUYING FEED(贪心orDP)

BUYING FEED

时间限制:3000 ms  |  内存限制:65535 KB

难度:4

描述

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 pay1*1 = 1 cents.

When John travels from 4 to 5 heis 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.

输入
The first line of input contains a number c giving the number of cases that follow

There are multi test cases ending with EOF.

Each case starts with a line containing three space-separated integers: K, E, and N

Then N lines follow :every line contains three space-separated integers: Xi Fi Ci

输出
For each case,Output A single integer that is the minimum cost for FJ to buy and transport the feed
样例输入
1
2 5 3
3 1 2
4 1 2
1 1 1
样例输出
7
来源
第三届河南省程序设计大赛
上传者

ACM_赵铭浩

今天上午做的时候  因为先入为主的思想  认为这道题就是DP  一直浪费了好多时间  知道下午 听到说贪心可以做

一下子就做出来了  还是太年轻。。。上午也不是没有想贪心 只是想得太浅  没细想

贪心A了以后  因为我感觉 我上午想的没错  看了评论区 才知道 可能会有多个商店在同一点上 。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。我吐血了。。

也算用DP做了

贪心方法:

单价+某点到终点的距离  从小到大排序即可

所买的 肯定是最便宜的。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
	int x;
	int w;
	int per;
}c[400];
int n,e,k;
bool cmp(node a,node b)
{
	return n-a.x+a.per<n-b.x+b.per;
}
int main()
{
	int ncase;
	scanf("%d",&ncase);
	while(ncase--)
	{
		memset(&c,0,sizeof(&c));
		scanf("%d %d %d",&k,&e,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%d %d %d",&c[i].x,&c[i].w,&c[i].per);
		}
		sort(c,c+n,cmp);
		int cost=0;
		for(int i=0;i<n;i++)
		{
			if(k>=c[i].w)
			{
				cost+=(e-c[i].x)*c[i].w+c[i].w*c[i].per;
				k-=c[i].w;
			}
			else
			{
				cost+=(e-c[i].x)*k+k*c[i].per;
				k=0;
			}
			if(k==0)
			break;
		}
		printf("%d\n",cost);
	}
	return 0;
}         

DP 思想:

dp[x][w]  其中x为当前坐标 w为已购买的数量

for i=0.....FI

dp[x][w]=min(dp[x][w],dp[x-1][w-i]+i*Ci+(e-x+1)*i);

AC代码:

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
struct node
{
	int w;
	int per;
};
int main()
{
	int k,e,n;
	int dp[400][105];
	int ncase;
	vector<node>edge[400];
	scanf("%d",&ncase);
	while(ncase--)
	{
		memset(dp,100,sizeof(dp));
		memset(edge,0,sizeof(edge));
		scanf("%d %d %d",&k,&e,&n);
		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++)
					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]);
	}
	return 0;
}                
时间: 2024-07-31 13:16:02

nyoj248 BUYING FEED(贪心orDP)的相关文章

题解 NYOJ248 BUYING FEED

题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=248 分析:英文题目,看懂了,画几下,就发现其实就是一个贪心背包的变形,水题一个~话不多少,代码走起~ /* 部分背包的变体,水题 只需要算出每个商店的单位总费用 = 原单位费用+距离的费用 */ #include <iostream> using namespace std; #include <cstdio> #include <cstdlib> #includ

2020: [Usaco2010 Jan]Buying Feed, II

2020: [Usaco2010 Jan]Buying Feed, II Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 220  Solved: 162[Submit][Status] Description (buying.pas/buying.in/buying.out 128M 1S) Farmer John needs to travel to town to pick up K (1 <= K <= 100) pounds of feed

BUYING FEED (第三届省赛)

BUYING FEED (英文题!!!!   为那些跟我一样acmer翻译一下吧  是实话 看不懂题真的很烦人     以上都是废话, 下面解释 英文题不出意外还是队友写的, 题意是第一行输入  要买的量k   要走的总长度e   店铺的数量n :    然后就是每家店铺距离起点的距离    存货量    成本   . ps : 将一单位的物品移动一个单位的距离要花费1磅运费: 求  需要花费的最小的成本: (这道题的解题思路在于按照  将每家店铺以单位的物品运到终点所需要的成本 排序, 然后从

NYOJ 248 &amp;&amp; BNUOJ 4151 BUYING FEED (贪心)

[题目链接]click here~~ [题目大意]FJ开车去买K份食物,如果他的车上有X份食物.每走一里就花费X元.FJ的城市是一条线,总共E里路,有E+1个地方,标号0~E.FJ从0开始走,到E结束(不能往回走),要买K份食物.城里有N个商店,每个商店的位置是X_i(一个点上可能有多个商店),有F_i份食物,每份C_i元.问到达E并买K份食物的最小花费. [解题思路] 把每个站点的物品看成是一件物品,每件物品除了价值之外,还附加了从站点到终点的运费.也就是买1磅到达终点时的花费,把它作为这个商

[河南省ACM省赛-第三届] BUYING FEED (nyoj 248)

#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; #define N 352 /* 重量*单价+重量*距离 = 重量*(距离+单价) 预处理单价 贪心:优先买价格低的 */ struct Node { int p;// p = (单价+距离) int w; }c[N]; bool cmp(Node a, Node b)

CodeForces 545C Woodcutters (贪心orDP)

[题目链接]:click here~~ [题目大意]: 有n棵树,给出每棵树的位置和高度,然后把树砍掉,树可以向左倒也可以向右倒.输出最多能砍几棵树. [思路]:利用贪心的思想.第一棵树的左边和最后一棵树的右边没树,所以他们向两边倒,然后对于中间的树来说,首先先向左边倒,然后左边距离如果不够的话再向右边倒,向右倒的时候注意更新一下距离. 代码: /* * Problem: CodeForces 545C * Running time: 46MS * Complier: G++ * Author:

Buying Feed, 2010 Nov (单调队列优化DP)

约翰开车回家,又准备顺路买点饲料了(咦?为啥要说"又"字?)回家的路程一共有 E 公里,这一路上会经过 K 家商店,第 i 家店里有 Fi 吨饲料,售价为每吨 Ci 元.约翰打算买 N 吨饲料,他知道商家的库存是足够的,至少所有店的库存总和不会少于 N.除了购买饲料要钱,运送饲料也是要花油钱的,约翰的卡车上如果装着 X 吨饲料,那么他行驶一公里会花掉 X 2 元,行驶 D 公里需要D X 2 元.已知第 i 家店距约翰所在的起点有 Xi 公里,那么约翰在哪些商店买饲料运回家,才能做到最

nyoj-248-buying feed

http://acm.nyist.net/JudgeOnline/problem.php?pid=248 BUYING FEED 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描述 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 cent

算法专题——贪心算法

贪心算法正确性证明: 1.证明贪心选择性质:经过贪心选择,可以获得最优解 2.最优子结构:证明子问题最优解与贪心选择组合,结果依然是最优解 •All we really need to do is argue that an optimal solution to the subproblem, combined with the greedy choice already made, yields an optimal solution to the original problem. 例: