Luogu1038 神经网络 (拓扑排序)

拓扑排序,裸的,水的。
第一发:题读错,输出错,输入错,到处错 (longrightarrow) 40pts (excuse me ?)
第二发:漏了输入层特判 (longrightarrow) 60pts (我操我自己)
第三发:(A)得毫无快感,毫无尊严,看着”普及+/提高“沉默良久

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("

----------

")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;
#include <queue>

const int N = 1007;

int n;

int C[N],U[N];

struct Edge{
	int nxt, pre, w;
}e[N];
int head[N], cntEdge;
inline void add(int u, int v, int w){
	e[++cntEdge] = (Edge){ head[u], v, w}, head[u] = cntEdge;
	
}

int in[N],out[N];

int q[N], top;
inline void Topsort(){
	R(i,1,n){
		if(!in[i])
			q[++top] = i;
	}
	while(top){
		int u = q[top--];
		for(register int i = head[u]; i; i = e[i].nxt){
			int v = e[i].pre;
			--in[v];
			if(C[u] > 0) C[v] += C[u] * e[i].w;
			if(!in[v])
				q[++top] = v;
		}
	}
}
int main(){
	//FileOpen();
	
	int m;
    io >> n >> m;
    R(i,1,n){
        io >> C[i] >> U[i];
        if(C[i] == 0)
			C[i] -= U[i];
    }
    
    R(i,1,m){
    	int u, v, w;
    	io >> u >> v >> w;
    	add(u, v, w);
    	++out[u], ++in[v];
    }
    
    Topsort();
    
    int flag = 0;
    R(i,1,n)
		if(out[i] == 0 && C[i] > 0){
			printf("%d %d
", i, C[i]);
			flag = 1;
		}
    if(!flag)
		printf("NULL
");
		
	return 0;
}

原文地址:https://www.cnblogs.com/bingoyes/p/11228671.html