装船问题
Time Limit: 1000 ms Memory Limit: 65536 KiB
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
输出一个整数,表示可以得到的最大价值。
Sample Input
10010 1020 1030 1040 1050 1060 1070 1080 1090 10100 10
Sample Output
550
Hint
价重比:计算其价值与重量之比
Source
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int p,w,pw;
}Goods;
Goods g[10];
int Compare(const void* a,const void* b)
{
Goods* x=(Goods*)a;
Goods* y=(Goods*)b;
return y->pw-x->pw;
}
int main()
{
int m,i,sum_Price,sum_Weight;
scanf("%d",&m);
for(i=0;i<10;i++)
{
scanf("%d %d",&g[i].p,&g[i].w);
g[i].pw=g[i].p/g[i].w;
}
qsort(g,10,sizeof(Goods),Compare);
sum_Price=0;
sum_Weight=0;
for(i=0;i<10;i++)
{
sum_Weight+=g[i].w;
sum_Price+=g[i].p;
if(sum_Weight>m)
{
sum_Weight-=g[i].w;
sum_Price-=g[i].p;
sum_Price+=(m-sum_Weight)*g[i].pw;
break;
}
}
printf("%d\n",sum_Price);
return 0;
}
954

被折叠的 条评论
为什么被折叠?



