推荐看这里
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, uu, vv, ww, cc, hea[225], cnt, ss, tt, maxFlow, lev[225], tot, T;
const int oo=0x3f3f3f3f;
struct Edge{
int too, nxt, cap, lim;
}edge[500005];
queue<int> d;
void add_edge(int fro, int too, int cap, int lim){
edge[cnt].nxt = hea[fro];
edge[cnt].too = too;
edge[cnt].cap = cap;
edge[cnt].lim = lim;
hea[fro] = cnt++;
}
bool bfs(){
memset(lev, 0, sizeof(lev));
d.push(ss);
lev[ss] = 1;
while(!d.empty()){
int x=d.front();
d.pop();
for(int i=hea[x]; i!=-1; i=edge[i].nxt){
int t=edge[i].too;
if(!lev[t] && edge[i].cap>0){
lev[t] = lev[x] + 1;
d.push(t);
}
}
}
return lev[tt]!=0;
}
int dfs(int x, int lim){
if(x==tt) return lim;
int addFlow=0;
for(int i=hea[x]; i!=-1 && addFlow<lim; i=edge[i].nxt){
int t=edge[i].too;
if(lev[t]==lev[x]+1 && edge[i].cap>0){
int tmp=dfs(t, min(lim-addFlow, edge[i].cap));
edge[i].cap -= tmp;
edge[i^1].cap += tmp;
addFlow += tmp;
}
}
return addFlow;
}
void dinic(){
while(bfs()) maxFlow += dfs(ss, oo);
}
int main(){
cin>>T;
while(T--){
cin>>n>>m;
cnt = tot = maxFlow = 0;
memset(hea, -1, sizeof(hea));
ss = 0; tt = n + 1;
for(int i=1; i<=m; i++){
scanf("%d %d %d %d", &uu, &vv, &cc, &ww);
add_edge(uu, vv, ww-cc, cc);
add_edge(vv, uu, 0, cc);
add_edge(ss, vv, cc, cc);
add_edge(vv, ss, 0, cc);
add_edge(uu, tt, cc, cc);
add_edge(tt, uu, 0, cc);
tot += cc;
}
dinic();
if(maxFlow<tot) printf("NO\n");
else{
printf("YES\n");
for(int i=0; i<cnt; i+=6)
printf("%d\n", edge[i^1].cap+edge[i].lim);
}
printf("\n");
}
return 0;
}