dp or 贪心 --- hdu : Road Trip

本文介绍了一种通过精心规划旅行路线来最小化燃油成本的方法。该方法基于一系列小镇之间的旅行,考虑了不同小镇的燃油价格差异及所需燃油量,通过算法实现最低成本的燃油购买与销售策略。
Road Trip
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 29, Accepted users: 29
Problem 12882 : No special judgement
Problem description

You are planning a road trip to visit your friends, each of whom live in different towns. Of course, you don't want to pay any more for fuel on the trip than you need to. However, the price of fuel in each of the towns is different, so you will need to carefully plan the trip to minimise the cost. For example, it might make sense to take on extra fuel at a town where the price is low so that you don't need to buy so much at a town where it is more expensive. Indeed, it may even make sense to sell excess fuel at some towns to recoup some of the costs. Of course, your car can only hold a certain amount of fuel, and you have to make sure you take on enough fuel at each town to reach the next (assume that it's OK to arrive with an empty tank).
Your task is to write a program to help you plan your trip.



Input

Input will consist of specifications for a series of journeys. Information for each journey will begin with a line containing an integer 0 < c < 100 that specifies the capacity of the car's fuel tank in litres and an integer 0 < t < 20 that specifies the number of towns to visit for that journey. A line containing two zeros indicates the end of input.
The following t lines contain information about successive stages on the journey: the price (in fixed point dollars-and-cents format, 0.01 <= p < 9.99) of one litre of fuel (either to buy or to sell) in the town at the beginning of the stage, and the number of litres of fuel (an integer, 1 <= n < 100) needed to reach the next town.



Output

Output should consist of one line for each journey comprising the journey number (formatted as shown) followed by a single space and the minimum cost of completing that journey, formatted as a fixed-point number with 2 decimal places.



Sample Input
10 3
2.00 7
1.50 8
1.00 3
50 6
1.50 20
4.20 5
1.15 35
1.41 27
1.92 30
2.21 15
0 0
Sample Output
Journey 1: 29.00
Journey 2: 117.64
Problem Source
HNU Contest 

 

Mean:

你要开车去n个小镇拜访你的老朋友,但是你不是高富帅,只得将油钱尽量的省,现在给你油箱的容量、小镇数量、每个小镇的油价、到达下一个小镇需要的油,你可以买油也可卖油,问你最少的油钱是多少。

analyse:

这题就是一个贪心的思想,如果下一站的油价比这站高,那么别想了,将油箱装满,到下一站就可以赚钱;反之就只需买够路上用的就可。

当然有人说这题是dp,这也没错,看个人怎么理解,其实这题就两个状态之间转移,dp已经退化为贪心。

Time complexity:O(n)

 

Source code:

//Memory   Time
// 1254K    0MS
// by : Snarl_jsb
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<iomanip>
#include<string>
#include<climits>
#include<cmath>
#define MAX 110
#define LL long long
using namespace std;
struct Node
{
   double p;
   int next;
};
Node node[MAX];
int v,n;
int main()
{
   int kase=1;
   while(scanf("%d %d",&v,&n),v+n)
   {
       double cost=0.0;
       printf("Journey %d: ",kase++);
       bool flag=0;
       for(int i=1;i<=n;i++)
       {
           scanf("%lf %d",&node[i].p,&node[i].next);
           if(node[i].next>v)
           {
               flag=1;
               break;
           }
       }
       if(flag==1)
       {
           puts("0.00");
           continue;
       }
       int now=0;
       for(int i=1;i<n;i++)   //zui hou mei pan
       {


            if(i>1) // begin to No.2
           {
               if(node[i].p>node[i-1].p&&now>node[i].next)   //sell
               {
                   int tmp=now-node[i].next;
                   cost-=node[i].p*tmp;
                   now-=tmp;
               }
           }


           if(node[i].p<node[i+1].p)   //  earn money
           {
               int tmp=v-now;
               cost+=node[i].p*tmp;
               now=v-node[i].next;
           }
           else    // bu ke zhuan qian
           {
               int tmp;
               if(node[i].next>now)
               {
                   tmp=node[i].next-now;
                   now=0;
               }
               else
               {
                   tmp=0;
                   now-=node[i].next;
               }
               cost+=node[i].p*tmp;
           }

       }
       // final
       if(now>node[n].next)
       {
           int tmp=now-node[n].next;
           cost-=tmp*node[n].p;
           now=0;
       }
       else
       {
           int tmp=node[n].next-now;
           cost+=tmp*node[n].p;
           now=0;
       }

       printf("%.2lf\n",cost);
   }
   return 0;
}
内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值