NOIP 2003 神经网络 解题报告
用列队,按顺序一个个发送信号,然后当一个节点所有信号都接受了c>0就加入列队,然后继续。。。。
网上题解应该比较多,我直接上代码吧:
#include <stdio.h> #include <assert.h> #include <stdlib.h> #define QMAX 1000 int dot[200]; int map[200][200]; int link[200][200]; int count[200]; int queue[QMAX]; int rear, head; int in[200], out[200]; void enqueue(int k) { int t = (rear + 1) % QMAX; assert(t != head); queue[rear] = k; rear = t; } int exqueue(void) { int t; assert(head != rear); t = queue[head]; head = (head + 1) % QMAX; return t; } int main(int argc, char **argv) { int i, j; int n, p; int a, b, c; scanf("%d%d", &n, &p); for(i = 0; i < n; i++){ scanf("%d%d", &a, &b); if(a != 0){ dot[i] = a; enqueue(i); }else{ dot[i] = -b; } } for(i = 0; i < p; i++){ scanf("%d%d%d", &a, &b, &c); a--, b--; map[a][count[a]] = c; link[a][count[a]] = b; count[a]++; out[a]++, in[b]++; } while(head != rear){ i = exqueue(); for(j = 0; j < count[i]; j++){ dot[link[i][j]] += map[i][j] * dot[i]; in[link[i][j]]--; if(in[link[i][j]] == 0 && dot[link[i][j]] > 0){ enqueue(link[i][j]); } } } for(i = 0, c = 1; i < n; i++){ if(out[i] == 0 && dot[i] > 0){ c = 0; printf("%d %d\n", i + 1, dot[i]); } } if(c){ printf("NULL\n"); } return 0; }