zoukankan      html  css  js  c++  java
  • 【洛谷 1016】旅行家的预算

    题目描述

    一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的)。给定两个城市之间的距离D1D1、汽车油箱的容量CC(以升为单位)、每升汽油能行驶的距离D2D2、出发点每升汽油价格PP和沿途油站数NN(NN可以为零),油站ii离出发点的距离DiDi、每升汽油价格PiPi(i=1,2,…,Ni=1,2,,N)。计算结果四舍五入至小数点后两位。如果无法到达目的地,则输出“No Solution”。

    输入格式

    第一行,D1D1,CC,D2D2,PP,NN。

    接下来有NN行。

    i+1i+1行,两个数字,油站i离出发点的距离DiDi和每升汽油价格PiPi。

    输出格式

    所需最小费用,计算结果四舍五入至小数点后两位。如果无法到达目的地,则输出“No Solution”。

    输入输出样例

    输入 #1
    275.6 11.9 27.4 2.8 2
    102.0 2.9
    220.0 2.2
    
    输出 #1
    26.95
    

    说明/提示

    N le 6N6,其余数字le 500500

    题解:贪心。但是DFS貌似也可以过

    #include<bits/stdc++.h>
    #define re register
    using namespace std;
    int const MANX=9999;
    int n,f;
    double Ans=99999999,sum,c,dis;
    double d[MANX],p[MANX],nd[MANX];
    
    void dfs(int st,double oil,double mo){
       
        if(st==n+1){
            if(mo<Ans)Ans=mo;
            f=1;/
            return;
        }
        if(c*dis<nd[st])return;
        double nx=0;
        for(re int i=st;i<=n;i++){
            nx+=nd[i];
            if(dis*c<nx)break;
            dfs(i+1,c-nx/dis,mo+p[st]*(c-oil));
            dfs(i+1,0,mo+max((double)0,p[st]*nx/dis-p[st]*oil));
            
        }
        return;
    }
    int main(){
        cin>>sum>>c>>dis>>p[0]>>n;
        for(re int i=1;i<=n;i++){
            cin>>d[i]>>p[i];
            nd[i-1]=d[i]-d[i-1];
        }
        nd[n]=sum-d[n];
    
        dfs(0,0,0);
        if(f){
            printf("%.2lf",Ans);
        }
        else printf("No Solution");
        return 0;
    }
  • 相关阅读:
    Maven项目中的配置文件找不到以及打包问题
    企业信息化快速开发平台 JeeSite
    http缓存浅谈
    <mvc:annotation-driven />注解意义
    Spring中报"Could not resolve placeholder"的解决方案
    eclipse怎么停止building workspace
    利用mybatis-generator自动生成代码
    Java 8 中的 Streams API 详解
    tomca配置文件自动还原问题的解决 server.xml content.xml 等
    tomcat 修改默认字符集
  • 原文地址:https://www.cnblogs.com/wuhu-JJJ/p/11484871.html
Copyright © 2011-2022 走看看