目录

P1038 [NOIP2003 提高组] 神经网络

目录

Link

当点/边权值为负时就不再传递(坑

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct EDGE{
  int to, next, w;
}edge[1001];
int head[1001], cnt = 0;
void addedge(int from, int to, int w)
{
  edge[ ++cnt].  to = to;
  edge[cnt].  next = head[from];
  edge[cnt].  w = w;
  head[from] = cnt;
}

int visited[1001], in[1001], c[1001];
bool out[1001];
int main()
{
  int n, m;
  queue <int> q;
  cin >> n >> m;
  for(int i = 1;i <= n;i++)
    {
      int u;
      visited[i] = out[i] = 0;
      cin >> c[i] >> u;
      if(c[i] > 0)
	{
	  q.push(i);
	  visited[i] = 1;
	}
      else
	c[i] -= u;
    }
  for(int i = 1;i <= m;i++)
    {
      int from, to, w;
      cin >> from >> to >> w;
      addedge(from, to, w);
      out[from] = 1;
    }
  while(!q.empty())
    {
      int k = q.front();
      q.pop();
      if(c[k] <= 0) // can output
	continue;
      for(int i = head[k];i != 0;i = edge[i].  next)
	{
	  int to = edge[i].  to;
	  c[to] += edge[i].  w * c[k];
	  if(!visited[to])
	    {
	      q.push(to);
	      visited[to] = 1;
	    }
	}
    }
  bool yay = 0;
  for(int i = 1;i <= n;i++)
    {
      if(!out[i] && c[i] > 0) // out layer and excited
	{
	  cout << i << " " << c[i] << endl;
	  yay = 1;
	}
    }
  if(yay == 0)
    {
      cout << "NULL" << endl;
    }
  return 0;
}