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:用张量表示数据,用计算图搭建神经网络,用会话执行计算图,优化线上的权重(参数),得到模型 ...
随机推荐
- DVWA全级别之File Inclusion(文件包含)
File Inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(include(),req ...
- 在 JavaScript 中为什么 typeof null 的结果是 object?
java 中的 null:既是对象,又不是对象,史称「薛定谔的对象」. typeof null==='object'; ..//true null instanceof Object //fals ...
- Go_sql注入
我们任何时候都不应该自己拼接SQL语句! sqlInjectDemo("xxx' or 1=1#") sqlInjectDemo("xxx' union select * ...
- 【C语言】复合函数求值
例子:求复合函数F(G(X)),其中F(x)=|x-3|+|x+1|,函数G(x)=x^2-3x. 分析:从复合函数的结构可以看出,F函数的自变量为G函数的绝对值,可以将F函数和G函数作为独立的函数实 ...
- SARS病毒
每一道题目皆是一处美丽的风景: 何为科技的力量和程序的思维哦,在这暑假的编程之路上,我要好好地体验一番来嘞! 数学规律是:f(n)=2^(n-1)+4^(n-1). //递推分析可得!具体过程 ...
- python夜记
关于多行字符串(multi-line strings)的表现: Python列表是基于0索引的.(zero-indexed). 晌午起床来嘞,再来些笔记: Treasures 1: 列表方法rever ...
- docker-api的使用(java)
通过docker-api来执行docker相关的操作. 配置 可以在docker启动文件docker.service中加入如下 vi /lib/systemd/system/docker.serv ...
- Atcoder Beginner Contest151D(迷宫问题求任意两点最短路径的最大值,BFS)
BFS可以求得最短路,DFS会找到从当前点到图中叶子结点的路径. #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using na ...
- JS-常用方法合集
部分方法使用jQuery!!!//tab切换 /* * 参数tablist为触发事件id * 参数tabmain为执行切换id * 参数ev为触发事件的动作 */ function tab(tabli ...
- Gitee 码云 pages 搭建vue项目记录
首先要在.gitignore文件去掉/dist,这个文件默认是不上传的,但是执行打包bulid的时候会生成dist文件,而线上访问的是打包之后的dist文件: vue.config.js文件,如下图箭 ...