nyoj 248 BUYING FEED 第三届河南省赛

来源:互联网 发布:淘宝网购能用信用卡吗 编辑:程序博客网 时间:2024/05/19 00:42

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 costsD*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 atlocation X_i (0 < X_i < E) on the number line and can sellJohn 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 leastK 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
样例输入
2 5 3
3 1 2
4 1 2
1 1 1
样例输出

7

题意:这个人要去e的位置运送feed,这些feed是从沿路上的商店里买来的,除了买feed需要用到cent,运送每单位的feed还需还费距离(e-x)的cent,问要运送到e点需要最小的cent,

思路:这是一道贪心,用结构体记录每个商店的位置,feed的数量,和单价,然后计算出从这个商店带出一个单位的feed所需要的cernt,(这个是要本商店的单价,再加上运送到e点的花费),存完之后,用sort排序,从小到大直到取到K为止。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct note
{
  int x,f,c;
  int pr;
}F[105];
int cmp(note a,note b)
{
   return a.pr<b.pr;
}
int main()
{
  int t,k,e,n;
  int i;
  cin>>t;
  while(t--)
  {
     scanf("%d%d%d",&k,&e,&n);
     for(i=1;i<=n;i++)
     {
       scanf("%d%d%d",&F[i].x,&F[i].f,&F[i].c);
       F[i].pr=F[i].c+(e-F[i].x);
     }
    sort(F,F+n,cmp);
    int ans=0;
    for(i=1;i<=n;i++)
    {
        if(k<=0)
            break;
        if(k>=F[i].f)
        {
           ans+=(F[i].pr*F[i].f);
            k-=F[i].f;
        }
        else
        {
            ans+=(F[i].pr*k);
            k=0;
        }
    }
    cout<<ans<<endl;
  }
}

0 0
原创粉丝点击