->题目链接
先存图,输入时对每个输入节点,把序号推入队列,非输入点,用他的活跃度直接减去它的阈值,然后处理队列;
处理队列,也就是拓扑,每次拿出一个节点,处理他的每条边,更新边的尾节点的活跃度和推入队列,最后排序输出;
#include<queue>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define N 10200
#define M N*N
using namespace std;
queue<int> q;
struct edge{
int to,val,from,next;
}e[M];
struct answ{
int num,val;
}ans[N];
int h,m,n,t,u,v,w,x,c[N],hd[N];
bool out[N],vis[N];
bool cmp(answ a, answ b)
{
return a.num < b.num;
}
int cnt, tot;
void build(int u, int v, int w)
{
cnt++;
e[cnt].from = u;
e[cnt].to = v;
e[cnt].val = w;
e[cnt].next = hd[u];//连接(同一头结点的)边
hd[u] = cnt;//最后会记录当前头结点的最后一条边的序号
//每次next到当前头节点的上一条边
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++){
scanf("%d%d", &c[i], &x);
if(c[i])
q.push(i),vis[i] = true;
else c[i] -= x, vis[i] = false;
}
for(int i = 1; i <= m; i++){
scanf("%d%d%d", &u ,&v, &w);
build(u, v, w);
out[u] = true;
}
while(!q.empty()){
h = q.front();
q.pop();
vis[h] = false;
for(int i = hd[h]; i; i = e[i].next){//初始化到以此点为头结点的最一条边
if(c[ e[i].from ] <= 0)
break;
t = e[i].to;
c[t] += e[i].val * c[h];
if(!vis[t])
q.push(t), vis[t] = true;
}
}
for(int i = 1; i <= n; i++){
if(c[i] && !out[i]){//只有最后没有出边的节点
ans[tot].num = i;
ans[tot++].val = c[i];
}
}
if(tot == 0){
cout << "NULL" <<endl;
return 0;
}
sort(ans, ans + tot, cmp);
for(int i = 0; i < tot; i++)
if(ans[i].val > 0)
cout << ans[i].num << ' ' << ans[i].val << endl;
return 0;
}