topo sort

P1038 神经网络,拓扑排序板子题

include <bits/stdc++.h>

using namespace std;
const int N=1e2+10;
struct node
{
int v,w;
};
vector g[N];
queue q;
int n,m;
bool flag=true;
int c[N],in[N],out[N],vis[N];
void topsort()
{
while(!q.empty())
{
int u=q.front();
q.pop();
if(c[u]<=0) continue;
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i].v;
c[v]+=c[u]*g[u][i].w;
if(!(--in[v]))
{
q.push(v);
// vis[v]=1;
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("xxx.in","r",stdin);
//freopen("xxx.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
int x;
cin>>c[i]>>x;
if(c[i]>0)
{
q.push(i);
vis[i]=1;
}
else
{
c[i]-=x;
}
}
for(int i=1;i<=m;i++)
{
int u,v,w;
cin>>u>>v>>w;
g[u].push_back({v,w});
out[u]=1;
in[v]++;
}
topsort();
for(int i=1;i<=n;i++)
{
if(c[i]>0&&!out[i])
{
flag=false;
cout<<i<<' '<<c[i]<<'\n';
}
}
if(flag) cout<<"NULL"<<endl;
return 0;
}

本文作者:srzsrz

本文链接:https://www.cnblogs.com/After17/p/18448841

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   srzsrz  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起
  1. 1 404 not found REOL
404 not found - REOL
00:00 / 00:00
An audio error has occurred.