Lemonade Trade

4990: Lemonade Trade

时间限制: 1 Sec  内存限制: 128 MB  Special Judge
提交: 64  解决: 9
[提交][状态][讨论版][命题人:admin]

题目描述

The lunch break just started! Your mother gave you one litre of pink lemonade. You do not like pink lemonade and want blue lemonade instead. Fortunately for you the other children in class are willing to trade with you.
Each of them is willing to offer you any quantity of the virtually infinite amount of lemonade they got from their mother, in exchange for their favourite lemonade, according to some exchange rate. The other children are sitting in a long row in the class room and you will walk along the row, passing each child only once. You are not allowed to walk back! Of course, you want to maximise the amount of blue lemonade you end up with. In case you can obtain more than 10 litres of blue lemonade, this is more than you will need, and you will throw away any excess (and keep the 10 litres). 
Fortunately, you know in advance what everybody is offering for trade. Your task is to write a program to find the maximum amount of blue lemonade that you can obtain.

输入

The input consists of the following:
• One line containing a single integer 0 ≤ N ≤ 105, the number of children in the class room, excluding yourself;
• N lines, each containing two strings O, W and a floating point number 0.5 < R < 2,the name of the lemonade that is offered, the name of the lemonade that is wanted,and the exchange rate: for every litre of lemonade W that you trade you get R litres of lemonade O in return.
All strings are guaranteed to have at most 10 alphanumeric characters.

输出

Output a single line containing a single floating point number M, the maximum amount (in litres) of blue lemonade that you can obtain. In case you could obtain more than 10 litres, M is considered to be 10. You are required to output M with absolute precision 10−6.

样例输入

3
blue pink 1.0
red pink 1.5
blue red 1.0

样例输出

1.500000000000000

提示

 

来源

BAPC2017 

【题意】用1升W颜色的的物品可以交换到R升O颜色的物品,初始有1升粉红颜色的物品,求最后最多能够交换多少升蓝颜色的物品。

注意:按照输入的顺序依次交换,不能走回头路,蓝颜色物品多于10升的时候输出10;

【思路】当走到第i个人处时,如果W颜色物品存在,则去交换O颜色物品,否则不交换。因为最后数据量最大可能为2^(100000),所以取log。

注意:log(a*b) = loga + logb;

【代码如下】

 

#include <bits/stdc++.h>
using namespace std;

const double eps = 1e-7;
map<string,double>mp;
int n;
double r;
char o[20],w[20];

int main(){
    scanf("%d",&n);
    mp["pink"] = 0.0;
    for(int i = 0; i < n; i ++){
        scanf("%s%s%lf",o,w,&r);
        r = log10(r);
        if(!mp.count(w)) continue;
        if(!mp.count(o)) mp[o] = mp[w] + r;
        else mp[o] = max(mp[o],mp[w]+r);
    }
    double ans = mp["blue"];
    if(ans - 1.0 >= eps) printf("10.0\n");
    else if(ans == 0) printf("0.0\n");
    else printf("%.8lf\n",pow(10.0,ans));
    return 0;
}

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值