hdu1009(贪心)


FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 77314    Accepted Submission(s): 26527


Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
 

Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
 

Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
 

Sample Input
  
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
 

Sample Output
  
13.333 31.500
 

Author
CHEN, Yue
 

Source
 


题本身并不难,但是要多考虑下,如,当小鼠没有猫粮,或者有些豆子白送,或者没有交换豆子的房间,或者小鼠没猫粮但是刚好只有一间屋子,且是白送猫粮的情况,总之多多考虑

#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
double m,n;
while(cin>>m>>n)
{
double a[1001][3]={0},cnt=0;
if(m==(-1)&&n==(-1))
break;
if(n==0)
{printf("%.3f\n",cnt);continue;}
int i,j,k;
double t,f=1;
for(i=1;i<=n;++i)
{
scanf("%lf%lf",&a[i][1],&a[i][2]);
}
for(i=1;i<=n;++i)
{
if(a[i][2]==0)
{cnt+=a[i][1];continue;}
else 
{k=i;
for(j=i+1;j<=n;++j)
{
if(a[j][2]==0)
continue;
else if((a[j][1]/a[j][2])>(a[k][1]/a[k][2]))
k=j;
}
if(k!=i)
{
t=a[i][1],a[i][1]=a[k][1],a[k][1]=t;
t=a[i][2],a[i][2]=a[k][2],a[k][2]=t;
}
}
}
for(i=1;i<=n;++i)
{
if(a[i][2]==0)
continue;
else
{ if(m<a[i][2])
{printf("%.3f\n",cnt+(m/a[i][2])*a[i][1]);m=0;f=0;break;}
else if(m==a[i][2])
{printf("%.3f\n",cnt+a[i][1]);m=0;f=0;break;}
else 
{
m-=a[i][2];
cnt+=a[i][1];
f=1;
}
}
}
if(f==1)
printf("%.3f\n",cnt);

}
return 0;
}


基于粒子群优化算法的p-Hub选址优化(Matlab代码实现)内容概要:本文介绍了基于粒子群优化算法(PSO)的p-Hub选址优化问题的研究与实现,重点利用Matlab进行算法编程和仿真。p-Hub选址是物流与交通网络中的关键问题,旨在通过确定最优的枢纽节点位置和非枢纽节点的分配方式,最小化网络总成本。文章详细阐述了粒子群算法的基本原理及其在解决组合优化问题中的适应性改进,结合p-Hub中转网络的特点构建数学模型,并通过Matlab代码实现算法流程,包括初始化、适应度计算、粒子更新与收敛判断等环节。同时可能涉及对算法参数设置、收敛性能及不同规模案例的仿真结果分析,以验证方法的有效性和鲁棒性。; 适合人群:具备一定Matlab编程基础和优化算法理论知识的高校研究生、科研人员及从事物流网络规划、交通系统设计等相关领域的工程技术人员。; 使用场景及目标:①解决物流、航空、通信等网络中的枢纽选址与路径优化问题;②学习并掌握粒子群算法在复杂组合优化问题中的建模与实现方法;③为相关科研项目或实际工程应用提供算法支持与代码参考。; 阅读建议:建议读者结合Matlab代码逐段理解算法实现逻辑,重点关注目标函数建模、粒子编码方式及约束处理策略,并尝试调整参数或拓展模型以加深对算法性能的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值