题目:
输入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;
}