装船问题

来源:互联网 发布:加工中心手工编程实例 编辑:程序博客网 时间:2024/04/27 14:12

Problem Description

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

Input

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

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

源代码:

#include<stdio.h>#include<stdlib.h>struct boat{int p;int w;int pw;}a[100];int cmp(const void *a,const void*b){struct boat *x=(struct boat *)a;struct boat *y=(struct boat *)b;return y->pw-x->pw;}int main(){int i,he=0,j=0,m,n=10;scanf("%d",&m);for(i=0;i<10;i++){scanf("%d%d",&a[i].p,&a[i].w);a[i].pw=a[i].p/a[i].w;}qsort(a,n,sizeof(struct boat),cmp);while(m>0){if(m-a[j].w>=0){he=he+a[j].p;m=m-a[j].w;}else{he=he+m*a[j].pw;m=m-a[j].w;}j++;if(j==10) break;}printf("%d\n",he);return 0;}




0 0
原创粉丝点击