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:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型 ...
随机推荐
- AcWing 827. 双链表
https://www.acwing.com/problem/content/829/ #include <iostream> using namespace std; ; int m; ...
- 题解【洛谷P1938】 [USACO09NOV]找工就业Job Hunt
题面 题解 将路径连边\((x, y, d)\) ,将航线连边\((x, y, d - w)\).其中线路是从\(x\)到\(y\),航线的费用为\(w\),\(d\)的含义如题面. 跑一遍\(SPF ...
- DSP---TI CCSv5.5.x-Windows安装
TI CCSv5.5.x(正式版)-Windows版本 国内2013年9月13日首发安装CCSv5.5图示 *请关掉防火墙及杀毒软件进行安装 第一步 第二步 安装程序检测到挂起的重新启动,这可能在安装 ...
- django入门(二)MTV开发模式
MTV开发模式,顾名思义,M是models,T是templates,V是view. 之前的教程没有牵扯到html,然后今天将告诉你如何转到自己做的静态页面 首先还是先创建一个app,python ma ...
- 微信小程序 获取cookie 以及设置 cookie
小程序开发中我们需要获取到后端给的cookie进行请求验证,但是微信并没有帮我们保存cookie,那么我们要维持会话需要自己来保存cookie,并且请求的时候加上cookie 1.获取cookie 在 ...
- cannot be resolved to a type解决方法!!!
小楼今天在做一个Java项目的时候遇到一个大家经常遇到的问题:XXX cannot be resolved to a type 看到一百多个errors时的时候,小楼也是被吓得赶紧去找度娘. 归纳一下 ...
- python正则子组匹配
子组匹配返回找到的第一个匹配项 []表示匹配列表中的任意一个,返回找到的第一个 这样可以发现如果要查找字母的话可以使用[a-z],返回找到的第一个字母 查找数字使用[0-9],返回找到的第一个数字相当 ...
- IntelliJ IDEA 2017.3尚硅谷-----忽略大小写提示
- 7_1 除法(UVa725)<选择合适的枚举对象>
如果把数字0到9分配成2个整数(各五位数),现在请你写一支程序找出所有的配对使得第一个数可以整除第二个数,而且商为N(2<=N<=79),也就是:abcde / fghijk = N这里每 ...
- hadoop3.1.1 HA高可用分布式集群安装部署
1.环境介绍 涉及到软件下载地址:https://pan.baidu.com/s/1hpcXUSJe85EsU9ara48MsQ 服务器:CentOS 6.8 其中:2 台 namenode.3 台 ...