Week4
Program--Medium--399. Evaluate Division
Equations are given in the format A / B = k
, where A
and B
are variables represented as strings, and k
is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0
.
Example:
Given a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].
The input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries
, where equations.size() == values.size()
, and the values are positive. This represents the equations. Return vector<double>
.
According to the example above:
equations = [ ["a", "b"], ["b", "c"] ], values = [2.0, 3.0], queries = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ].
The input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.
题目解析:
这是一个非常明显的图算法的题目。题目给出了点到点的路径以及路径的权值,对于一个查询由起点到终点,找到一条路径由起点去往终点,并计算路径上的权值的积。解法也很直接:
1.首先我们得将图的点以及权值以方便我们访问的方式存储下来,类似课上的路径集合E。在这里,我用一个vector<string, vector<Node>>来储存每个点能到的点的集合,Node上有点的名字以及到它的权值,这样就可以非常方便地访问边以及对应的权值。
2.我们建好了图的结构信息,下一步就是如何查找路径以及返回权值的积。在这里我用的方法是BFS,对每个点进行一次遍历,用vector<string, pair<string, double>>来存储遍历的当前节点的前一节点名以及路径的权值。
3.最后一步用遍历所得的Pre信息来从终点进行反向搜索,不断返回遍历时的上一个几点,直到找到起始点或者没有路径可走为止。
4.如果成功回到起点,证明存在路径从起点到终点,否则不存在路径则返回-1,特殊情况是起点等于终点则直接输出1,其中一个点不存在直接输出-1。
代码:
class Solution {
public:
struct Node{
string name;
double data;
Node(string n, double d) {
name = n;
data = d;
}
};
map<string, vector<Node>> Point;
map<string, pair<string, double>> Pre;
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries) {
for (int i = 0; i < equations.size(); i++) {
pair<string, string> temp = equations[i];
double tempValue = values[i];
Node tempNode(temp.second, tempValue);
Node tempNode2(temp.first, 1/tempValue);
Point[temp.first].push_back(tempNode);
Point[temp.second].push_back(tempNode2);
}
vector<double> result;
cout << "success" << endl;
for (int i = 0; i < queries.size(); i++) {
string end;
queue<string> source;
Pre[queries[i].first].first = queries[i].first;
Pre[queries[i].second].second = 1;
source.push(queries[i].first), end = queries[i].second;
//cout << "i = " << i << endl;
if (!Point[queries[i].first].size() || !Point[end].size()) {
result.push_back(-1.0);
}
else {
result.push_back(Query(source, end));
}
}
return result;
}
double Query(queue<string> s, string e) {
string start = s.front();
while(!s.empty()) {
string temp = s.front();
s.pop();
cout << s.size() << endl;
for (int i = 0; i < Point[temp].size(); i++) {
if (Pre[temp].first != Point[temp][i].name) {
s.push(Point[temp][i].name);
Pre[Point[temp][i].name].first = temp;
Pre[Point[temp][i].name].second = Point[temp][i].data;
}
}
}
string temp = e;
string last = e;
double num = 1;
while (Pre[temp].first != temp && Pre[temp].first != ""){
temp = Pre[last].first;
num *= Pre[last].second;
//cout << "last = " << last << " temp = " << temp << endl;
last = temp;
//cout << "num = " << num << endl;
}
if (temp != start) {
return -1.0;
}
else {
return num;
}
}
};