-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem_LCR_111_calcEquation.cc
85 lines (79 loc) · 2.07 KB
/
Problem_LCR_111_calcEquation.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
// @sa https://leetcode.cn/problems/evaluate-division/
// @sa Problem_0399_calcEquation.cc
class Solution
{
public:
vector<double> calcEquation(vector<vector<string>>& equations,
vector<double>& values,
vector<vector<string>>& queries)
{
int vars = 0;
// 统计节点
unordered_map<string, int> variables;
int n = equations.size();
for (int i = 0; i < n; i++)
{
if (!variables.count(equations[i][0]))
{
variables[equations[i][0]] = vars++;
}
if (!variables.count(equations[i][1]))
{
variables[equations[i][1]] = vars++;
}
}
// 建图
vector<vector<std::pair<int, double>>> edges(vars);
for (int i = 0; i < n; i++)
{
int idxA = variables[equations[i][0]];
int idxB = variables[equations[i][1]];
edges[idxA].push_back({idxB, values[i]});
edges[idxB].push_back({idxA, 1.0 / values[i]});
}
vector<double> ans;
for (auto& q : queries)
{
double res = -1.0;
if (!variables.count(q[0]) && !variables.count(q[1]))
{
int idxA = variables[q[0]];
int idxB = variables[q[1]];
if (idxA == idxB)
{
res = 1.0;
}
else
{
queue<int> points;
points.push(idxA);
vector<double> ratios(vars, -1.0);
ratios[idxA] = 1.0;
while (!points.empty() && ratios[idxB] < 0)
{
int cur = points.front();
points.pop();
for (auto& [next, val] : edges[cur])
{
if (ratios[next] < 0)
{
// 没计算过的才需要计算
// ratios数组相当于一般 bfs 的 vsisted 数组
ratios[next] = ratios[cur] * val;
points.push(next);
}
}
}
res = ratios[idxB];
}
}
ans.push_back(res);
}
return ans;
}
};