loj 102 最小费用流

补一发费用流的代码

%%%棒神

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #include<queue>
 8 #include<vector>
 9 #include<map>
10 #define ll long long
11 #define inf 2139062143
12 #define MAXN 420
13 using namespace std;
14 inline int read()
15 {
16     int x=0,f=1;char ch=getchar();
17     while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
18     while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
19     return x*f;
20 }
21 int n,m,ans;
22 struct ZKW
23 {
24     int fst[MAXN],to[MAXN*80],nxt[MAXN*80],val[MAXN*80],cos[MAXN*80],cnt;
25     int dis[MAXN],s,t,vis[MAXN];
26     ZKW() {ans=0,cnt=2;}//memset(fst,0xff,sizeof(fst));}
27     void add(int u,int v,int w,int c) {nxt[cnt]=fst[u],fst[u]=cnt,to[cnt]=v,val[cnt]=w,cos[cnt++]=c;}
28     int spfa()
29     {
30         memset(dis,127,sizeof(dis));
31         queue<int> q;
32         dis[t]=0,vis[t]=1;
33         q.push(t);
34         while(!q.empty())
35         {
36             int x=q.front();q.pop();vis[x]=0;
37             for(int i=fst[x];i;i=nxt[i])
38                 if(val[i^1]&&dis[to[i]]>dis[x]-cos[i])
39                 {
40                     dis[to[i]]=dis[x]-cos[i];
41                     if(!vis[to[i]]) {q.push(to[i]);vis[to[i]]=1;}
42                 }
43         }
44         memset(vis,0,sizeof(vis));
45         return dis[s]!=inf;
46     }
47     int dfs(int x,int a)
48     {
49         if(x==t||!a) {ans+=dis[s]*a;return a;}
50         if(vis[x])return 0;
51         vis[x]=1;
52         int res=0,f;
53         for(int i=fst[x];i&&a;i=nxt[i])
54             if(dis[to[i]]==dis[x]-cos[i]&&(f=dfs(to[i],min(a,val[i]))))
55                 res+=f,val[i]-=f,val[i^1]+=f,a-=f;
56         vis[x]=0;
57         return res;
58     }
59     int solve()
60     {
61         int f=0;
62         while(spfa()) f+=dfs(s,2147483647);
63         return f;
64     }
65 }Z;
66 int main()
67 {
68     n=read(),m=read();int a,b,c,d;
69     while(m--)
70     {
71         a=read(),b=read(),c=read(),d=read();
72         Z.add(a,b,c,d);
73         Z.add(b,a,0,-d);
74     }
75     Z.s=1,Z.t=n;
76     printf("%d ",Z.solve());
77     printf("%d",ans);
78 }
View Code

 

posted @ 2018-02-04 20:26  jack_yyc  阅读(391)  评论(0编辑  收藏  举报