# 最小费用最大流模板

  1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 #include<cmath>
6 #include<queue>
7 #define ll long long
8 #define inf 50000000
9 #define re register
10 using namespace std;
11 struct po
12 {
13     int from,to,dis,nxt,w;
14 }edge[250001];
16 int dis[5001],b[5001],xb[5001],flow[5001];
18 {
19     int x=0,c=1;
20     char ch=' ';
21     while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
22     while(ch=='-')c*=-1,ch=getchar();
23     while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
24     return x*c;
25 }
26 inline void add_edge(int from,int to,int w,int dis)
27 {
29     edge[num].from=from;
30     edge[num].to=to;
31     edge[num].w=w;
32     edge[num].dis=dis;
34 }
35 inline void add(int from,int to,int w,int dis)
36 {
39 }
40 inline bool bfs()
41 {
42     memset(dis,100,sizeof(dis));
43     memset(b,0,sizeof(b));
44     queue<int> q;
45     while(!q.empty())
46     q.pop();
47     for(re int i=1;i<=n;i++)
48     {
49         fa[i]=-1;
50     }
51     b[s]=1;dis[s]=0;fa[s]=0;
52     flow[s]=inf;q.push(s);
53     while(!q.empty())
54     {
55         int u=q.front();
56         q.pop();
57         b[u]=0;
59         {
60             int v=edge[i].to;
61             if(edge[i].w>0&&dis[v]>dis[u]+edge[i].dis)
62             {
63                 dis[v]=dis[u]+edge[i].dis;
64                 fa[v]=u;
65                 xb[v]=i;
66                 flow[v]=min(flow[u],edge[i].w);
67                 if(!b[v]){b[v]=1,q.push(v);}
68             }
69         }
70     }
71     return dis[t]<inf;
72 }
73 inline void max_flow()
74 {
75     while(bfs())
76     {
77         int k=t;
78         while(k!=s)
79         {
80             edge[xb[k]].w-=flow[t];
81             edge[xb[k]^1].w+=flow[t];
82             k=fa[k];
83         }
84         tot+=flow[t];
85         sum+=flow[t]*dis[t];
86     }
87 }
88 int main()
89 {
92     for(re int i=1;i<=m;i++)
93     {
97     }
98     max_flow();
99     cout<<tot<<" "<<sum;
100 }

#include <cstdio>
#include <cstring>
using namespace std;
const int maxint=~0U>>1;

int n,m,pi1,cost=0;
bool v[550];
struct etype
{
int t,c,u;
etype *next,*pair;
etype(){}
etype(int t_,int c_,int u_,etype* next_):
t(t_),c(c_),u(u_),next(next_){}
void* operator new(unsigned,void* p){return p;}
} *e[550];

int aug(int no,int m)
{
if(no==n)return cost+=pi1*m,m;
v[no]=true;
int l=m;
for(etype *i=e[no];i;i=i->next)
if(i->u && !i->c && !v[i->t])
{
int d=aug(i->t,l<i->u?l:i->u);
i->u-=d,i->pair->u+=d,l-=d;
if(!l)return m;
}
return m-l;
}

bool modlabel()
{
int d=maxint;
for(int i=1;i<=n;++i)if(v[i])
for(etype *j=e[i];j;j=j->next)
if(j->u && !v[j->t] && j->c<d)d=j->c;
if(d==maxint)return false;
for(int i=1;i<=n;++i)if(v[i])
for(etype *j=e[i];j;j=j->next)
j->c-=d,j->pair->c+=d;
pi1 += d;
return true;
}

int main()
{
freopen("costflow.in","r",stdin);
freopen("costflow.out","w",stdout);
scanf("%d %d",&n,&m);
etype *Pe=new etype[m+m];
while(m--)
{
int s,t,c,u;
scanf("%d%d%d%d",&s,&t,&u,&c);
e[s]=new(Pe++)etype(t, c,u,e[s]);
e[t]=new(Pe++)etype(s,-c,0,e[t]);
e[s]->pair=e[t];
e[t]->pair=e[s];
}
do do memset(v,0,sizeof(v));
while(aug(1,maxint));
while(modlabel());
printf("%d\n",cost);
return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
#define inf 50000000
#define re register
using namespace std;
struct po
{
int from,to,dis,nxt,w;
}edge[250001];
int dis[5001],b[5001],xb[5001],flow[5001];
{
int x=0,c=1;
char ch=' ';
while((ch>'9'||ch<'0')&&ch!='-')ch=getchar();
while(ch=='-')c*=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-'0',ch=getchar();
return x*c;
}
inline void add_edge(int from,int to,int w,int dis)
{
edge[num].to=to;
edge[num].w=w;
edge[num].dis=dis;
}
inline void add(int from,int to,int w,int dis)
{
}
inline bool spfa()
{
memset(b,0,sizeof(b));
for(re int i=0;i<=n;i++) dis[i]=inf;
dis[t]=0;b[t]=1;
deque<int> q;
q.push_back(t);
while(!q.empty())
{
int u=q.front();
b[u]=0;
q.pop_front();
{
int v=edge[i].to;
if(edge[i^1].w>0&&dis[v]>dis[u]-edge[i].dis)
{
dis[v]=dis[u]-edge[i].dis;
if(!b[v])
{
b[v]=1;
if(!q.empty()&&dis[v]<dis[q.front()])
q.push_front(v);
else
q.push_back(v);
}
}
}
}
return dis[s]<inf;
}
inline int dfs(int u,int low)
{
if(u==t)
{
b[t]=1;
return low;
}
int diss=0;
b[u]=1;
{
int v=edge[i].to;
if(!b[v]&&edge[i].w!=0&&dis[u]-edge[i].dis==dis[v])
{
int check=dfs(v,min(edge[i].w,low));
if(check>0)
{
tot+=check*edge[i].dis;
edge[i].w-=check;
edge[i^1].w+=check;
low-=check;
diss+=check;
if(low==0) break;
}
}
}
return diss;
}
inline int max_flow()
{
int ans=0;
while(spfa())
{
b[t]=1;
while(b[t])
{
memset(b,0,sizeof(b));
ans+=dfs(s,inf);
}
}
return ans;
}
int main()
{
for(re int i=1;i<=m;i++)
{
}
cout<<max_flow()<<" ";
cout<<tot;
}
View Code

zkw大神的博客

posted @ 2018-02-07 14:21  ~victorique~  阅读(1597)  评论(1编辑  收藏
Live2D