WA3:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
inline void print(int x){
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
const int MAXN=1e2+5;
int n,p,c[MAXN],u[MAXN];
struct node{
int to,next,w;
}e[MAXN*MAXN];
int head[MAXN],cnt,in[MAXN],out[MAXN],sum[MAXN];
void add(int x,int y,int z){
e[++cnt].to=y;
e[cnt].next=head[x];
e[cnt].w=z;
head[x]=cnt;
}
queue<int>q;
bool vis[MAXN];
int main(){
n=read(),p=read();
for(int i=1;i<=n;++i){
c[i]=read(),u[i]=read();
}
for(int i=1;i<=p;++i){
int u=read(),v=read(),w=read();
add(u,v,w);
in[v]++;
out[u]++;
}
for(int i=1;i<=n;++i){
if(!in[i])q.push(i),vis[i]=1;
}
while(!q.empty()){
int x=q.front();
q.pop();
if(!vis[x])c[x]=sum[x]-u[x];
if(c[x]<=0)continue;
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
sum[y]+=e[i].w*c[x];
in[y]--;
if(!in[y])q.push(y);
}
}
bool f=0;
for(int i=1;i<=n;++i){
if(!out[i]){
if(c[i]>0){
f=1;
print(i),printf(" "),print(c[i]),puts("");
}
}
}
if(!f)puts("NULL");
return 0;
}
AC:
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=getchar();
}
return x*f;
}
inline void print(int x){
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);
putchar(x%10^48);
}
const int MAXN=1e2+5;
int n,p,c[MAXN],u[MAXN];
struct node{
int to,next,w;
}e[MAXN*MAXN];
int head[MAXN],cnt,in[MAXN],out[MAXN],sum[MAXN];
void add(int x,int y,int z){
e[++cnt].to=y;
e[cnt].next=head[x];
e[cnt].w=z;
head[x]=cnt;
}
queue<int>q;
bool vis[MAXN];
int main(){
n=read(),p=read();
for(int i=1;i<=n;++i){
c[i]=read(),u[i]=read();
}
for(int i=1;i<=p;++i){
int u=read(),v=read(),w=read();
add(u,v,w);
in[v]++;
out[u]++;
}
for(int i=1;i<=n;++i){
if(!in[i])q.push(i),vis[i]=1;
}
while(!q.empty()){
int x=q.front();
q.pop();
if(!vis[x])c[x]=sum[x]-u[x];
if(c[x]<=0){
for(int i=head[x];i;i=e[i].next){
in[e[i].to]--;
if(!in[e[i].to])q.push(e[i].to);
}
continue;
}
for(int i=head[x];i;i=e[i].next){
int y=e[i].to;
sum[y]+=e[i].w*c[x];
in[y]--;
if(!in[y])q.push(y);
}
}
bool f=0;
for(int i=1;i<=n;++i){
if(!out[i]){
if(c[i]>0){
f=1;
print(i),printf(" "),print(c[i]),puts("");
}
}
}
if(!f)puts("NULL");
return 0;
}
区别在于要把 ci≤0 的点所连的点的入读也减去 1。