ISAP好长啊。。。居然打了20分钟。。。。
#include <cstdio> #include <cstring> #include <cstdlib> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cmath> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #include <time.h>; #define cler(arr,val) memset(arr,val,sizeof(arr)) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define IN freopen ("in.txt","r",stdin); #define OUT freopen ("out.txt","w",stdout); typedef long long LL; const int MAXN = 10010; const int MAXM = 44224; const int INF = 0x3f3f3f3f; const int mod = 1000000007; struct Edge { int to,next,cap,flow; }edge[MAXM]; int tol,head[MAXN]; int gap[MAXN],dep[MAXN],cur[MAXN]; void init() { tol=0; cler(head,-1); } void addedge(int u,int v,int w,int rw=0) { edge[tol].to=v,edge[tol].cap=w,edge[tol].flow=0,edge[tol].next=head[u]; head[u]=tol++; edge[tol].to=u,edge[tol].cap=rw,edge[tol].next=head[v]; head[v]=tol++; } int Q[MAXN]; void BFS(int star,int end) { cler(dep,-1); cler(gap,0); gap[0]=-1; int front= 0,rear = 0; dep[end]=0; Q[rear++] = end; while(front!= rear) { int u=Q[front++]; for(int i = head[u];i!=-1;i=edge[i].next) { int v=edge[i].to; if(dep[v]!= -1) continue; Q[rear++]= v; dep[v]=dep[u]+1; gap[dep[v]]++; } } } int S[MAXN]; int sap(int star,int end,int N) { BFS(star,end); memcpy(cur,head,sizeof(head)); int top=0,u=star,ans=0; while(dep[star]<N) { if(u==end) { int Min=INF; int inser; for(int i=0;i<top;i++) if(Min>edge[S[i]].cap-edge[S[i]].flow) { Min=edge[S[i]].cap-edge[S[i]].flow; inser=i; } for(int i=0;i<top;i++) { edge[S[i]].flow+=Min; edge[S[i]^1].flow-=Min; } ans+=Min; top=inser; u=edge[S[top]^1].to; continue; } bool flag = false; int v; for(int i=cur[u];i!=-1;i=edge[i].next) { v=edge[i].to; if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u]) { flag = true; cur[u]=i; break; } } if(flag) { S[top++] = cur[u]; u=v; continue; } int Min=N; for(int i= head[u];i!=-1;i=edge[i].next) if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min) { Min=dep[edge[i].to]; cur[u]=i; } gap[dep[u]]--; if(!gap[dep[u]]) return ans; dep[u]=Min+1; gap[dep[u]]++; if(u!=star) u=edge[S[--top]^1].to; } return ans; } int low[MAXN]; int u,v,c[MAXM],r[MAXM]; int main() { #ifndef ONLINE_JUDGE freopen("in.txt",stdin); //freopen("out.txt",stdout); #endif int n,m; while(scanf("%d%d",&n,&m)!=EOF) { cler(low,0); init(); for(int i=0;i<m;i++) { scanf("%d%d%d%d",&u,&v,&c[i],&r[i]); addedge(u,r[i]-c[i]); low[v]+=c[i]; low[u]-=c[i]; } int star=0,end=n+1,sum=0; for(int i=1;i<=n;i++) { if(low[i]>0) addedge(star,i,low[i]),sum+=low[i]; else addedge(i,end,-low[i]); } // cout<<sum<<" "<<sap(star,n+2)<<endl; if(sap(star,n+2)==sum) { puts("YES"); for(int i=0;i<2*m;i+=2) printf("%d\n",edge[i].flow+c[i/2]); } else puts("NO"); } return 0; }