明显的有上下界的网络流,且无源汇,最简单的形式 代码: #include<stdio.h> #include<string.h> #include<memory.h> #define MAX 205 #define inf 1<<30 struct node2 { int v,c,next; }g[100000]; int dis[MAX],cur[MAX],num[MAX],pre[MAX],adj[MAX]; int D[MAX],f[MAX][MAX],from[MAX*MAX],to[MAX*MAX],low[MAX*MAX],up[MAX*MAX]; int s,t,e,m,n,vn; void add(int u,int v,int c) { g[e].v=v; g[e].c=c; g[e].next=adj[u]; adj[u]=e++; g[e].v=u; g[e].c=0; g[e].next=adj[v]; adj[v]=e++; } int sap() { int i,u,v,flag,aug=inf,flow=0; for(i=0;i<=vn;i++) { dis[i]=num[i]=0; cur[i]=adj[i]; } num[0]=vn; pre[s]=u=s; while(dis[s]<vn) { flag=0; for(i=cur[u];i!=-1;i=g[i].next) { v=g[i].v; if(g[i].c&&dis[u]==dis[v]+1) { flag=1; pre[v]=u; cur[u]=i; aug=aug<g[i].c?aug:g[i].c; u=v; if(u==t) { flow+=aug; while(u!=s) { f[pre[u]][u]+=aug; f[u][pre[u]]-=aug; u=pre[u]; g[cur[u]].c-=aug; g[cur[u]^1].c+=aug; } aug=inf; } break; } } if(flag) continue; if(--num[dis[u]]==0) break; for(dis[u]=vn,i=adj[u];i!=-1;i=g[i].next) { v=g[i].v; if(g[i].c&&dis[v]<dis[u]) { dis[u]=dis[v]; cur[u]=i; } } dis[u]++; num[dis[u]]++; u=pre[u]; } return flow; } int main() { int i,j,N; scanf("%d",&N); while(N--) { scanf("%d%d",&n,&m); s=0; t=n+1; vn=t+1; e=0; memset(adj,-1,sizeof(adj)); memset(D,0,sizeof(D)); memset(f,0,sizeof(f)); for(i=1;i<=m;i++) { scanf("%d%d%d%d",&from[i],&to[i],&low[i],&up[i]); add(from[i],to[i],up[i]-low[i]); D[from[i]]-=low[i]; D[to[i]]+=low[i]; } for(i=1;i<=n;i++) { if(D[i]>0) add(s,i,D[i]); else if(D[i]<0) add(i,t,-D[i]); } sap(); int flag=1; for(i=adj[s];i!=-1;i=g[i].next) { if(i%2==0&&g[i].c) { flag=0; break; } } if(!flag) puts("NO"); else { puts("YES"); for(i=1;i<=m;i++) { printf("%d/n",low[i]+f[from[i]][to[i]]); } puts(""); } } return 0; }