题目

题解:

拓扑排序,难度挺低,但是细节很多。
其中一个注意点:c[i]即便是负数,也要进队,不然有些点入度始终大于0,更新不了

标程:

#include<bits/stdc++.h>
using namespace std;
struct kk{
    int x,y;
}a[103];
struct node{
    int to,ne,w;
}e[10003];
int n,m,i,j,x,y,z,num,out[103],in[103],u,q[103],c[103],U[103],head[103],tot,h,t,v;
void add(int x,int y,int z){
    e[++tot]=(node){y,head[x],z};
    head[x]=tot;
}
int main(){
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;i++){
        scanf("%d%d",&c[i],&U[i]);
        if (c[i]) q[++t]=i;
    }
    for (i=1;i<=m;i++){
        scanf("%d%d%d",&x,&y,&z);
        in[y]++;out[x]++;
        add(x,y,z);
    }
    while (h<t){
        u=q[++h];
        for (i=head[u];i;i=e[i].ne){
            v=e[i].to;
            if (c[u]>0) c[v]+=e[i].w*c[u];
            in[v]--;
            if (!in[v]) c[v]-=U[v],q[++t]=v;
        }
    }
    for (i=1;i<=n;i++)
        if (!out[i] && c[i]>0) num++,printf("%d %d\n",i,c[i]);//出度为0的就是输出层
    if (!num) printf("NULL");
}