L3-023 计算图
建立结构体保存每个结点的前驱,操作符,来回两遍拓扑排序~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
vector<int> pre;
double data;
int fuhao=;
}Node[maxn];
vector<int> g[maxn];
int a[maxn];
int inDegree[maxn];
int N;
stack<int> topOrder;
double getnum (node a) {
if (a.pre.size()==) return a.data;
else if (a.pre.size()==) {
if (a.fuhao==) return exp(Node[a.pre[]].data);
else if (a.fuhao==) return log(Node[a.pre[]].data);
else if (a.fuhao==) return sin(Node[a.pre[]].data);
}
else if (a.pre.size()==) {
if (a.fuhao==) return Node[a.pre[]].data+Node[a.pre[]].data;
else if (a.fuhao==) return Node[a.pre[]].data-Node[a.pre[]].data;
else if (a.fuhao==) return Node[a.pre[]].data*Node[a.pre[]].data;
}
}
double topSort () {
queue<int> q;
for (int i=;i<N;i++)
if (inDegree[i]==) q.push(i);
while (!q.empty()) {
int u=q.front();
q.pop();
topOrder.push(u);
Node[u].data=getnum(Node[u]);
for (int i=;i<g[u].size();i++)
if (--inDegree[g[u][i]]==) q.push(g[u][i]);
if (q.empty()) return Node[u].data;
}
}
double dfs (int u,int x) {
if (Node[u].fuhao==) {
if (u==x) return ;
else return ;
}
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)+dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)-dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return dfs(Node[u].pre[],x)*Node[Node[u].pre[]].data+dfs(Node[u].pre[],x)*Node[Node[u].pre[]].data;
else if (Node[u].fuhao==) return exp(Node[Node[u].pre[]].data)*dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return 1.0/Node[Node[u].pre[]].data*dfs(Node[u].pre[],x);
else if (Node[u].fuhao==) return cos(Node[Node[u].pre[]].data)*dfs(Node[u].pre[],x);
}
int main () {
scanf ("%d",&N);
int fuhao;
double x,y;
vector<int> v1;
for (int i=;i<N;i++) {
scanf ("%d",&fuhao);
if (fuhao==) {
scanf ("%lf",&x);
Node[i].data=x;
v1.push_back(i);
}
else if (fuhao>=&&fuhao<=) {
scanf ("%lf %lf",&x,&y);
Node[i].pre.push_back((int)x);
Node[i].pre.push_back((int)y);
inDegree[i]+=;
g[(int)x].push_back(i);
g[(int)y].push_back(i);
Node[i].fuhao=fuhao;
}
else if (fuhao>=&&fuhao<=) {
scanf ("%lf",&x);
Node[i].pre.push_back((int)x);
inDegree[i]+=;
g[(int)x].push_back(i);
Node[i].fuhao=fuhao;
}
}
printf ("%.3f\n",topSort());
int u=topOrder.top();
for (int i=;i<v1.size();i++) {
if (i!=) printf (" ");
printf ("%.3f",dfs(u,v1[i]));
}
return ;
}
L3-023 计算图的更多相关文章
- PTA L3-023 计算图 (dfs+数学推导)
“计算图”(computational graph)是现代深度学习系统的基础执行引擎,提供了一种表示任意数学表达式的方法,例如用有向无环图表示的神经网络. 图中的节点表示基本操作或输入变量,边表示节点 ...
- MegEngine计算图、MatMul优化解析
MegEngine计算图.MatMul优化解析 本文针对天元在推理优化过程中所涉及的计算图优化与 MatMul 优化进行深度解读,希望能够帮助广大开发者在利用天元 MegEngine「深度学习,简单开 ...
- 配置 L3 agent - 每天5分钟玩转 OpenStack(99)
上一节我们介绍了路由服务(Routing)的基本功能,今天教大家如何配置. Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 fir ...
- Neutron 理解 (6): Neutron 是怎么实现虚拟三层网络的 [How Neutron implements virtual L3 network]
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- 理解 OpenStack 高可用(HA)(2):Neutron L3 Agent HA 之 虚拟路由冗余协议(VRRP)
本系列会分析OpenStack 的高可用性(HA)概念和解决方案: (1)OpenStack 高可用方案概述 (2)Neutron L3 Agent HA - VRRP (虚拟路由冗余协议) (3)N ...
- php大力力 [023节]CREATE TABLE创建新表sql写字段备注(2015-08-27)
2015-08-27 php大力力023.CREATE TABLE创建新表sql写字段备注 http://www.cnblogs.com/dalitongxue/p/4762182.html 参考: ...
- [Freescale]Freescale L3.14.52_1.1.0 yocto build
可参照:http://blog.csdn.net/wince_lover/article/details/51456745 1. Refer to <基于i.mx6处理器的Yocto项目及Lin ...
- Tensorflow计算模型 —— 计算图
转载自:http://blog.csdn.net/john_xyz/article/details/69053626 Tensorflow是一个通过计算图的形式来表述计算的编程系统,计算图也叫数据流图 ...
- Neutron路由篇:L3 agent+Namespace
Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 firewall 和 floating ip 服务. l3 agent 需 ...
- TensorFlow框架(一) 张量、计算图、会话
参考:中国大学MOOC 北京大学 曹健<TensorFlow笔记> 基于TensorFlow的NN:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型 ...
随机推荐
- IDEAVIM 常用快捷键总结和使用说明
---title: ideavim常用快捷键总结和使用tags: grammar_cjkRuby: true--- #### `待办` ideavim用于编程的常用快捷键说明 常用快捷键 插入(光标前 ...
- 页面中<link>和<script>标签
在html中,经常肯定会有js,css的引入 <head> <title>MyHtml</title> <link rel="stylesheet& ...
- tomcat8.5优化配置
参考文章: https://www.cnblogs.com/steven-snow/p/9262025.html 1.Tomcat内存使用调整 windows系统在bin/catalina.bat文件 ...
- net core,redis的安装和试用
一.window上面安装reids 在github上面下载,地址 安装完成后,进入安装目录打开redis服务, 这里双击Redis服务器即可启动Redis. 二.安装redis可视化工具 命令行操作r ...
- Python代码混淆和加密技术
Python进行商业开发时, 需要有一定的安全意识, 为了不被轻易的逆向. 混淆和加密就有所必要了. 为了增加代码阅读的难度, 源代码的混淆非常必要, 一个在线的Python代码混淆网站. http: ...
- 【应急响应】Tomcat安全加固
一.删除或选定无效账号 二.密码复杂度 三.启用日志记录功能 四.登录超时 五.Tomcat错误页面重定向 六.禁止Tomcat目录列表显示文件
- MVC简要介绍
(转自:http://www.cnbeta.com/articles/107924.htm) MVC不是一种设计模式(design pattern),它是一种架构模式(Architectural pa ...
- 【Webpack】
目录 关于模块化编程 Webpack的工作方式 三个重要的概念 使用Webpack创建一个项目 正式使用Webpack 使用Webpack进行ES6的模块化编程 "本质上,Webpack是一 ...
- java 数据类型优先级
由低到高:byte,short,char—> int —> long—> float —> double 1. 不能对boolean类型进行类型转换. 2. 不能把对象类型转换 ...
- Linux环境下mysql报错:bash: mysql: command not found 的解决方法
# mysql -u root-bash: mysql: command not found 原因:这是由于系统默认会查找/usr/bin下的命令. 如果这个命令不在这个目录下,当然会找不到命令. 我 ...