AcWing 477. 神经网络 JAVA
原题链接
简单
作者:
whx
,
2020-09-11 14:30:43
,
所有人可见
,
阅读 428
import java.util.*;
public class Main{
static int N = 110;
static int n,m;
static int[] h = new int[N], e = new int[N], ne = new int[N], w = new int[N];
static int[] f = new int[N], u = new int[N];
static int[] din = new int[N], dout = new int[N];
static int idx = 0;
static Queue<Integer> que = new LinkedList();
public static void main(String[] agrs){
Scanner in = new Scanner(System.in);
n = in.nextInt(); m = in.nextInt();
for(int i = 1; i <= n; i ++){
int a = in.nextInt(); int b = in.nextInt();
f[i] = a; u[i] = b;
if(f[i] == 0) f[i] -= b;
}
Arrays.fill(h, -1);
for(int i = 0; i < m; i++){
int a, b, c;
a = in.nextInt(); b = in.nextInt(); c = in.nextInt();
din[b]++; dout[a]++;
add(a,b,c);
}
toposort();
while(!que.isEmpty()){
int t = que.poll();
if(f[t] > 0){
for(int i = h[t]; i != -1; i = ne[i]){
f[e[i]] += f[t] * w[i];
}
}
}
boolean flag = true;
for(int i = 1; i <= n; i ++){
if(dout[i] == 0 && f[i] > 0){
System.out.println(i + " "+ f[i]);
flag = false;
}
}
if(flag) System.out.println("NULL");
}
static void add(int a, int b, int c){
e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++;
}
static void toposort(){
Queue<Integer> q = new LinkedList();
for(int i = 1; i <= n; i++){
if(din[i] == 0){
q.add(i);
que.offer(i);
}
}
while(!q.isEmpty()){
int t = q.poll();
for(int i = h[t]; i != -1; i = ne[i]){
int j = e[i];
if(--din[j] == 0){
q.offer(j);
que.offer(j);
}
}
}
}
}