[leetcode] 399. Evaluate Division
看到这道题,2个点和一个权值,然后想到图,但是leetcode就是这样,没给数据范围,感觉写起来很费劲,然后就开始用图来做,添加边的时候,注意正向边和反向变,然后查询的时候,先判断2个点是否都出现,然后判断是不是自己到自己,最后用dfs或者bfs到图里面去搜索,第一次交的时候,忘记用vis标记访问过的点,导致tle,加上之后,就ac了,套路一般吧,不知道还有什么trick。
double dfs(vector<vector<pair<int, double> > > &e, int x, int y) {
int n = e.size();
vector<double> dis(n, 1);
queue<int> q;
q.push(x);
vector<bool> tag(n, 0);
while(!q.empty()) {
int u = q.front(); q.pop();
//cout << u << endl;
for (int i = 0; i < e[u].size(); i++) {
int a = e[u][i].first;
double b = e[u][i].second;
if(tag[a]) continue;
dis[a] = dis[u] * b;
if(a == y) return dis[a];
q.push(a);
tag[a] = 1;
}
}
return -1.0;
}
vector<double> calcEquation(vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> query) {
vector<vector<pair<int, double> > > e;
map<string, int> m;
for (int i = 0; i < equations.size(); i++) {
string x = equations[i].first, y = equations[i].second;
if(!m.count(x)) {
vector<pair<int, double> > t;
m[x] = e.size(); e.push_back(t);
}
if(!m.count(y)) {
vector<pair<int, double> > t;
m[y] = e.size(); e.push_back(t);
}
int a = m[x],b = m[y];
e[a].push_back({b, values[i]});
e[b].push_back({a, 1.0 / values[i]});
}
//cout << "ad" << endl;
//cout << e.size() << endl;
vector<double> res;
for (int i = 0; i < query.size(); i++) {
string x = query[i].first, y = query[i].second;
//cout <<x << " " << y << endl;
if(!m.count(x) || !m.count(y)) {
res.push_back(-1.0);
} else {
if(x == y) {
res.push_back(1.0);
} else {
int a = m[x], b = m[y];
double t = dfs(e, a, b);
res.push_back(t);
}
}
}
return res;
}
[leetcode] 399. Evaluate Division的更多相关文章
- LN : leetcode 399 Evaluate Division
lc 399 Evaluate Division 399 Evaluate Division Equations are given in the format A / B = k, where A ...
- [LeetCode] 399. Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [Leetcode Week3]Evaluate Division
Evaluate Division题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/evaluate-division/description/ Desc ...
- 【LeetCode】399. Evaluate Division 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】399. Evaluate Division
题目如下: Equations are given in the format A / B = k, whereA and B are variables represented as strings ...
- 399. Evaluate Division
图像题,没觉得有什么简单的办法,貌似可以用Union Find来解. 感觉有2种思路,一种是先全部构建好每2个点的weight,然后直接遍历queires[][]来抓取答案. 一种是只构建简单的关系图 ...
- [LeetCode] Evaluate Division 求除法表达式的值
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- Leetcode: Evaluate Division
Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...
- [LeetCode] 150. Evaluate Reverse Polish Notation 计算逆波兰表达式
Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...
随机推荐
- MySQL 约束
MySQL中约束保存在information_schema数据库的table_constraints中,可以通过该表查询约束信息: 约束主要完成对数据的检验,保证数据库数据的完整性:如果有相互依赖数据 ...
- cocos2d-x CCSpriteBatchNode
转自:http://www.cnblogs.com/jiackyan/archive/2013/04/14/3019880.html 1.先说下渲染批次:这是游戏引擎中一个比较重要的优化指标,指的是一 ...
- 对PostgreSQL的prepared statement的深入理解
看官方文档: http://www.postgresql.org/docs/current/static/sql-prepare.html PREPARE creates a prepared sta ...
- C# Redis Server分布式缓存编程(二)
在Redis编程中, 实体和集合类型则更加有趣和实用 namespace Zeus.Cache.Redis.Demo { public class Person { public int Id { g ...
- git 回退和删除操作
今天不小心把分支的commit提交到master上了.衰 主要通过下面几个命令解决了,很简单记录一下. git reset –hard 回退到某一个版本git push origin :xxxx ...
- Parse error: syntax error, unexpected T_PUBLIC in 问题解决
class 类中 public function _getInfo($sn){ $title = ''; $_array = explode('~', $sn); ...
- BZOJ 2257: [Jsoi2009]瓶子和燃料 裴蜀定理
2257: [Jsoi2009]瓶子和燃料 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...
- Fragment的使用简单介绍【Android】
Fragment在实际项目开发中使用的越来越多,如今简介一下 布局文件: <LinearLayout xmlns:android="http://schemas.android.com ...
- ABAP 日期函数
一 财务期间处理 T_CODE: OB29 **取 公司年度变式, 和 货币 SELECT SINGLE waers periv FROM t001 INTO (v_waers,v_p ...
- C#_delegate EndInvoke
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...