http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
const int maxn=;
int sp,tp,cnt=,head[],nxt[maxn],to[maxn],cap[maxn],dis[],low[maxn],def[],m,n;
inline int read(){
int ans=; char last=' ',ch=getchar();
while(ch<'' || ch>'')last=ch,ch=getchar();
while(ch>='' && ch<='')ans=ans*+ch-'',ch=getchar();
if(last=='-')ans=-ans; return ans;
}
inline void add(int u,int v,int p){
nxt[cnt]=head[u],to[cnt]=v,cap[cnt]=p,head[u]=cnt++;
nxt[cnt]=head[v],to[cnt]=u,cap[cnt]=,head[v]=cnt++;
}
inline bool bfs(){
int u,e,v;
queue<int> que;
memset(dis,-,sizeof(dis));
que.push(sp),dis[sp]=;
while(!que.empty()){
u=que.front(),que.pop();
for(int e=head[u];~e;e=nxt[e]){
if(cap[e]>&&dis[v=to[e]]==-){
dis[v]=dis[u]+,que.push(v);
if(v==tp) return true;
}
}
}
return false;
}
inline int dfs(const int &u,const int &flow){
if(u==tp) return flow;
int res=,v,flw;
for(int e=head[u];~e;e=nxt[e]){
if(cap[e]>&&dis[u]<dis[v=to[e]]){
flw=dfs(v,min(cap[e],flow-res));
if(flw==) dis[v]=-;
cap[e]-=flw,cap[e^]+=flw;
res+=flw;
if(res==flow) break;
}
}
return res;
}
inline int dinic(int sp,int tp){
int ans=;
while(bfs()) {
ans+=dfs(sp,<<);
}
return ans;
}
int main(){
int t;
t=read();
while(t--)
{
cnt=;
memset(def,,sizeof(def));
memset(head,-,sizeof(head));
n=read(),m=read();
int s,t,up,down,sum=;
for(int i=;i<=m;i++){
s=read(),t=read(),down=read(),up=read();
add(s,t,up-down);
low[i]=down,def[s]+=down,def[t]-=down;
}
sp=n+,tp=n+;
for(int i=;i<=n;i++){
if(def[i]>) sum+=def[i],add(i,tp,def[i]);
if(def[i]<) add(sp,i,-def[i]);
}
if(dinic(sp,tp)==sum){
cout<<"YES"<<endl;
for(int i=;i<=m;i++){
cout<<cap[((i-)*)^]+low[i]<<endl;
}
}
else cout<<"NO"<<endl;
}
return ;
}
04-30 12:27