-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1038.cpp
60 lines (60 loc) · 1.02 KB
/
P1038.cpp
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
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
const int N=110,M=N*N;
int n,m;
int f[N],u[N];
int h[N],e[M],w[M],ne[M],idx;
int din[N],dout[N];
int q[N];
void add(int a,int b,int c){
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void topsort(){
int hh=0,tt=-1;
for(int i=1;i<=n;i++)
if(!din[i]){
q[++tt]=i;
}
while(hh<=tt){
int t=q[hh++];
for(int i=h[t];~i;i=ne[i]){
int j=e[i];
if(--din[j]==0)
q[++tt]=j;
}
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d%d",&f[i],&u[i]);
memset(h,-1,sizeof h);
for(int i=0;i<m;i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
dout[a]++,din[b]++;
}
for(int i=1;i<=n;i++){
if(din[i]){
f[i]-=u[i];
}
}
topsort();
for(int i=0;i<n;i++){
int j=q[i];
if(f[j]>0)
for(int k=h[j];~k;k=ne[k])
f[e[k]]+=f[j]*w[k];
}
bool has_print=false;
for(int i=1;i<=n;i++)
if(!dout[i]&&f[i]>0){
printf("%d %d\n",i,f[i]);
has_print=true;
}
if(!has_print) puts("NULL");
return 0;
}