洛谷P1038 神经网络

 1 //拓扑排序 坑点多 考验语文水平
 2 #include<bits/stdc++.h>
 3 #include<queue>
 4 using namespace std;
 5 const int maxn=105,maxm=10005;
 6 queue<int> q;
 7 struct edge{
 8     int to,dis;edge *Nex;
 9 }e[maxm],*head[maxn];
10 int top=-1;
11 void add(int x,int y,int z)
12 {
13     e[++top].to=y;e[top].dis=z;
14     e[top].Nex=head[x];head[x]=&e[top];
15 }
16 int n,p,c[maxn],u[maxn],du[maxn];
17 bool v[maxn];
18 void topo()
19 {
20     for(int i=1;i<=n;++i) if(!du[i]) q.push(i),u[i]=0;//输入层的状态为读入默认状态 与公式无关(阈值按0算) 
21     while(!q.empty())
22     {
23         int p=q.front();q.pop();
24         c[p]-=u[p];
25         if(head[p]==NULL) v[p]=1;//打标记 只输出出度为0的点的状态 
26         if(c[p]<=0) c[p]=0;//c<0 该神经元不会向下传输状态 但可以把c改成0 对后续计算无影响 方便统计出度为0的点 
27         for(edge *i=head[p];i!=NULL;i=i->Nex)
28         {
29             --du[i->to];
30             c[i->to]+=i->dis*c[p];
31             if(!du[i->to]) q.push(i->to); 
32         }
33     } 
34 }
35 int main()
36 {
37     scanf("%d%d",&n,&p);
38     for(int i=1;i<=n;++i) scanf("%d%d",&c[i],&u[i]);
39     for(int i=1,x,y,z;i<=p;++i) scanf("%d%d%d",&x,&y,&z),add(x,y,z),++du[y];
40     topo();
41     bool flag=0;
42     for(int i=1;i<=n;++i) if(v[i]&&c[i]>0) printf("%d %d\n",i,c[i]),flag=1;
43     if(!flag) printf("NULL");//无输出需要输出NULL 
44     return 0;
45 }

 

转载于:https://www.cnblogs.com/yu-xing/p/10315908.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值