#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define MAXN 200
#define MAXM 40000
#define INF 2000000000
struct node{
int v,c,id;
node *next,*back;
}edge[MAXM*2+10],*adj[MAXN+10],*ecnt=&edge[0];
int n,m,b[MAXM+10],du[MAXN+10],d[MAXN+10],vd[MAXN+10],s,t;
void addedge(int u,int v,int c,int id)
{
node *p=++ecnt;
p->v=v;
p->c=c,p->id=id;
p->next=adj[u],p->back=ecnt+1;
adj[u]=p;
p=++ecnt;
p->v=u;
p->c=0,p->id=0;
p->next=adj[v],p->back=ecnt-1;
adj[v]=p;
}
void read()
{
int x,y,blow,cap;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d%d%d",&x,&y,&blow,&cap);
b[i]=blow;
addedge(x,y,cap-blow,i);
du[x]-=blow;
du[y]+=blow;
}
s=0,t=n+1;
for(int i=1;i<=n;i++){
//注意sigma du[i](du[i]>0) + sigma du[i](du[i]<0)
if(du[i]>0) addedge(s,i,du[i],0);
if(du[i]<0) addedge(i,t,-du[i],0);
}
}
int aug(int u,int augc)
{
int augco=augc,mind=t,delta,v;
if(u==t)
return augc;
for(node *p=adj[u];p;p=p->next){
v=p->v;
if(p->c>0){
if(d[u]==d[v]+1){
delta=aug(v,min(augco,p->c));
p->c-=delta;
p->back->c+=delta;
augco-=delta;
if(d[s]>=t+1)
return augc-augco;
if(!augco)
break;
}
mind=min(mind,d[v]);
}
}
if(augco==augc){
vd[d[u]]--;
if(!vd[d[u]])
d[s]=t+1;
d[u]=mind+1;
vd[d[u]]++;
}
return augc-augco;
}
void Isap()
{
int flow=0;
memset(d,0,sizeof d);
memset(vd,0,sizeof vd);
vd[0]=t+1;
while(d[s]<t+1)
flow+=aug(s,INF);
}
bool Judge() // 或者flow==sigma(du[i]) | du[i]>0
{
for(node *p=adj[t];p;p=p->next)
if(p->back->c)
return false;
return true;
}
int main()
{
read();
Isap();
if(!Judge())
printf("NO\n");
else{
printf("YES\n");
for(int i=1;i<=m;i++)
printf("%d\n",b[i]+edge[2*i].c);
//edge[2*i].c : 若在线的记录要注意流返回的情况。
}
}
将边权改为c[u]-b[u]之后(上下界限制),流量不平衡,为了保证流量平衡,强制的建立ss,tt ,把填不平的sigma b[]之差 添到图上。