loj115 无源汇有上下界可行流

link

 

题意&题解

 

code:

 1 #include<bits/stdc++.h>
 2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
 3 #define ll long long
 4 #define inf 1000000001
 5 #define y1 y1___
 6 using namespace std;
 7 char gc(){
 8     static char buf[100000],*p1=buf,*p2=buf;
 9     return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
10 }
11 #define gc getchar
12 ll read(){
13     char ch=gc();ll x=0;int op=1;
14     for (;!isdigit(ch);ch=gc()) if (ch=='-') op=-1;
15     for (;isdigit(ch);ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
16     return x*op;
17 }
18 #define N 205
19 #define M 10205+N<<1
20 int n,m,cnt=1,s,t,head[N],d[N],vis[N],cur[N];
21 struct edge{int to,nxt,d,c;}e[M];
22 void adde(int x,int y,int d,int c){
23     e[++cnt].to=y;e[cnt].nxt=head[x];head[x]=cnt;
24     e[cnt].d=d;e[cnt].c=c;//d:原图下界;c:新图容量
25 }
26 void ins(int x,int y,int d,int z){
27     adde(x,y,d,z);adde(y,x,d,0);
28 }
29 bool bfs(){
30     queue<int> q;q.push(s);
31     rep (i,1,t) vis[i]=-1;vis[s]=1;
32     while (!q.empty()){
33         int u=q.front();q.pop();
34         for (int i=head[u];i;i=e[i].nxt){
35             int v=e[i].to;
36             if (e[i].c&&vis[v]==-1) vis[v]=vis[u]+1,q.push(v);
37         }
38     }
39     return vis[t]!=-1;
40 }
41 int dfs(int u,int flow){
42     if (u==t) return flow;
43     int w,used=0;
44     for (int &i=cur[u];i;i=e[i].nxt){
45         int v=e[i].to;
46         if (e[i].c&&vis[v]==vis[u]+1){
47             w=dfs(v,min(flow-used,e[i].c));
48             e[i].c-=w,e[i^1].c+=w,used+=w;
49             if (used==flow) return used;
50         }
51     }
52     if (!used) vis[u]=-1;
53     return used;
54 }
55 int dinic(){
56     int ret=0;
57     while (bfs()){
58         memcpy(cur,head,sizeof(cur));//当前弧优化
59         ret+=dfs(s,inf);
60     }
61     return ret;
62 }
63 int main(){
64     n=read(),m=read();
65     rep (i,1,m){
66         int x=read(),y=read(),a=read(),b=read();
67         ins(x,y,a,b-a);d[x]-=a,d[y]+=a;
68     }
69     s=n+1,t=n+2;
70     rep (i,1,n) if (d[i]>0) ins(s,i,0,d[i]);else ins(i,t,0,-d[i]);
71     dinic();
72     for (int i=head[s];i;i=e[i].nxt) if (vis[e[i].to]!=-1){puts("NO");exit(0);}
73     puts("YES");
74     rep (i,1,m) printf("%d\n",e[i*2+1].d+e[i*2+1].c);
75     return 0;
76 }
View Code
posted @ 2018-07-16 23:08  bestfy  阅读(292)  评论(0编辑  收藏  举报