zoukankan      html  css  js  c++  java
  • 洛谷P1038神经网络

    传送门啦

    一个拓扑排序的题,感觉题目好难懂。。。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    const int maxn = 105;
    
    inline int read(){char ch = getchar();int f = 1 , x = 0;while(ch > '9' || ch < '0'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + ch - '0';ch = getchar();}return x * f;}
    
    int n,p,c[maxn],u[maxn],x,y,z;
    int head[maxn],tot;
    int in[maxn],out[maxn];
    bool vis[maxn],flag;
    
    struct Edge{
    	int from,to,val,next;
    }edge[maxn << 1];
    
    void add(int u,int v,int w){
    	edge[++tot].from = u;
    	edge[tot].to = v;
    	edge[tot].val = w;
    	edge[tot].next = head[u];
    	head[u] = tot;
    }
    
    void dfs(int x){
    	for(int i=head[x];i;i=edge[i].next){
    		int v = edge[i].to;
    		c[v] += edge[i].val * c[x];
    		in[v]--;
    		if(!in[v] && c[v] - u[v] > 0){
    			c[v] -= u[v];
    			dfs(v);
    		}
    	}
    }
    
    int main(){
    	n = read(); p = read();
    	for(int i=1;i<=n;i++) {
    		c[i] = read(); u[i] = read();
    		if(c[i] > 0)  vis[i] = true;
    	}
    	for(int i=1;i<=p;i++){
    		x = read(); y = read(); z = read();
    		add(x , y , z);
    		in[y]++; out[x]++;
    	}
    	for(int i=1;i<=n;i++)
    		if(vis[i])  dfs(i);
    	for(int i=1;i<=n;i++)
    		if(out[i] == 0 && c[i] > 0){
    			printf("%d %d
    ",i,c[i]);
    			flag = true;
    		}
    	if(!flag)  printf("NULL
    ");
    	return 0;
    }
    顺风不浪,逆风不怂。
  • 相关阅读:
    [转]Spring的IOC原理[通俗解释一下]
    自我介绍
    什么是存储过程
    Linux 之 最常用的20条命令
    [转]sql语句中出现笛卡尔乘积 SQL查询入门篇
    mysql 多表连接
    正则表达式
    postman 测试API
    [转]mysql 视图
    数据库 修改统一显示时间
  • 原文地址:https://www.cnblogs.com/Stephen-F/p/9907269.html
Copyright © 2011-2022 走看看