sdutacm-装船问题

来源:互联网 发布:字帖推荐 知乎 编辑:程序博客网 时间:2024/04/27 19:06

装船问题

TimeLimit: 1000MS Memory Limit: 65536KB

SubmitStatistic

Problem Description

王小二毕业后从事船运规划工作,吉祥号货轮的最大载重量为M吨,有10种货物可以装船。第i种货物有wi吨,总价值是pi。王小二的任务是从10种货物中挑选若干吨上船,在满足货物总重量小于等于M的前提下,运走的货物的价重比最大。

Input

输入数据的第一行有一个正整数M(0 < M < 10000),表示所有货物最大载重量。在接下来的10行中,每行有若干个数(中间用空格分开),第i行表示的是第i种货物的货物的总价值pi ,总重量wi(piwi的整数倍,0 < pi , wi < 1000)

Output

输出一个整数,表示可以得到的最大价值。

Example Input

100

10 10

20 10

30 10

40 10

50 10

60 10

70 10

80 10

90 10

100 10

Example Output

550

Hint

价重比:计算其价值与重量之比

Author

#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>struct nodea{   double c,p;   double j;}node[10000000];int cmp( const void*a,const void*b){struct nodea *g =(nodea*)a;struct nodea *d = (nodea*)b;  return d->j - g->j;}int main(){   int m,n,w;   while(~scanf("%d",&w))   {      n=10;        for(int i=1;i<=n;i++)        {            scanf("%lf%lf",&node[i].p,&node[i].c);            node[i].j = node[i].p/node[i].c;        }        qsort(&node[1],n,sizeof(node[1]),cmp);        int sum = 0;        int i=1;        while(1)        {            if(w-node[i].c>0)            {               w -= node[i].c;               sum += node[i].p;            }            else            {               break;            }            i++;        }        sum += node[i].j *  w;   printf("%d\n",sum);   }  return 0;}/***************************************************User name: jk160505徐红博Result: AcceptedTake time: 0msTake Memory: 108KBSubmit time: 2017-01-11 19:35:13****************************************************/

0 0
原创粉丝点击