2136: 单位转换(每日一题)

 题目:

 输入1
4 3
1 foot = 12 inch
1 yard = 3 foot
1 meter = 100 centimeter
1 centimeter = 10 millimeter
750 millimeter to meter
42 yard to inch
10 meter to foot
样例1
0.75
1512
impossible
输入2
4 3
1 fortnight = 14 day
1 microcentury = 0.036525 day
1 microcentury = 1000 nanocentury
1 week = 7 day
22.2 fortnight to nanocentury
2.5 nanocentury to week
3.14 day to fortnight
样例2
8509240.2464065708427
1.3044642857142857142e-05
0.22428571428571428572
输入3
10 2
1 micrometer = 1000 nanometer
1 millimeter = 1000 micrometer
1 meter = 1000 millimeter
1 kilometer = 1000 meter
1 megameter = 1000 kilometer
1 lightsecond = 299.792458 meter
1 lightminute = 60 lightsecond
1 lighthour = 60 lightminute
1 lightday = 24 lighthour
1 lightyear = 365.25 lightday
42 nanometer to lightyear
42 lightyear to nanometer
样例3
4.439403502903384947e-18
3.9735067984839359997e+20

考察知识点:模拟,搜索

思路:

代码:

'算法' dfs ( times:2193ms ) 

#include <bits/stdc++.h>
using namespace std;
const int N = 210;
vector<int> g[N];
map<string, int> pos;
int idx;
double w[N][N];
double ans;
void check(int a, int fa, double cnt, int aim)
{
    if (ans != -1)
        return;
    if (a == aim)
    {
        ans = cnt;
        return;
    }
    for (auto b : g[a])
        if (b != fa)
            check(b, a, cnt * w[a][b], aim);
}
signed main()
{
    int n, q;
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++)
    {
        string a, b, h;
        double cnt, x;
        cin >> x >> a >> h >> cnt >> b;
        if (!pos.count(a))
            pos[a] = (++idx);
        if (!pos.count(b))
            pos[b] = (++idx);
        int pa = pos[a], pb = pos[b];
        g[pa].push_back(pb);
        g[pb].push_back(pa);
        w[pa][pb] = cnt;
        w[pb][pa] = 1.0 / cnt;
    }
    while (q--)
    {
        string a, b, h;
        double cnt;
        cin >> cnt >> a >> h >> b;
        int pa = pos[a], pb = pos[b];
        ans = -1;
        check(pa, -1, cnt, pb);
        if (ans != -1)
            printf("%lf\n", ans);
        else
            puts("impossible");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值