费用流

zkw费用流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
#include<map>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
#define debug(x) cout<<#x<<"="<<x<<'\n'
int n,m,s,t,tot=1,ans1,ans2,q[600005],l,r,dis[500005];
int head[500005],to[500005],nxt[500005],flow[500005],cost[500005];
bool vis[500005];
void add(int x,int y,int ff,int co)
{
nxt[++tot]=head[x];
head[x]=tot;to[tot]=y;flow[tot]=ff;cost[tot]=co;
}
bool spfa()
{
memset(vis,0,sizeof(vis));
memset(dis,127/3,sizeof(dis));
l=1;r=0;
R int INF=dis[1];dis[t]=0;
q[++r]=t;
while(l<=r)
{
R int x=q[l++];vis[x]=0;
for(int i=head[x];i;i=nxt[i])
if(flow[i^1]&&dis[to[i]]>dis[x]+cost[i^1])
{
dis[to[i]]=dis[x]+cost[i^1];
if(!vis[to[i]])vis[to[i]]=1,q[++r]=to[i];
}
}
return dis[s]!=INF;
}
int dfs(int x,int want)
{
vis[x]=1;
if(x==t||!want)return want;
int sum=0;
for(int i=head[x];i;i=nxt[i])
if(!vis[to[i]]&&flow[i]&&dis[x]-cost[i]==dis[to[i]])
{
R int f=dfs(to[i],min(flow[i],want));
if(!f)continue;
ans2+=f*cost[i];sum+=f;want-=f;
flow[i]-=f;flow[i^1]+=f;
}
return sum;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
fo(i,1,m)
{
R int x,y,ff,co;
scanf("%d%d%d%d",&x,&y,&ff,&co) ;
add(x,y,ff,co);add(y,x,0,-co);
}
while(spfa())
{
vis[t]=1;
while(vis[t])
{
memset(vis,0,sizeof(vis));
ans1+=dfs(s,1e9);
}
}
printf("%d %d",ans1,ans2);
return 0;
}

下面的弃用了。。。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<cmath>
#include<stack>
using namespace std;
#define R register
#define ll long long
#define fo(i,a,b) for(R int (i)=(a);(i)<=(b);++(i))
queue<int>q;
struct node
{
int to,next,cost,cup;
}a[5005*20];
int head[5005],cnt=1,dis[5005],flow[5005],pre[5005],preid[5005];
bool vis[5005];
int n,m,s,t,ans1,ans2;
void add(int x,int y,int cup,int cost)
{
a[++cnt].next=head[x];
head[x]=cnt;
a[cnt].to=y;a[cnt].cup=cup,a[cnt].cost=cost;
}
bool spfa()
{
memset(dis,127/3,sizeof(dis));
R int INF=dis[0];
flow[s]=1e9;dis[s]=0;q.push(s);
while(!q.empty())
{
R int x=q.front();q.pop();vis[x]=0;
for(int i=head[x];i;i=a[i].next)
{
R int to=a[i].to;
if(a[i].cup>0&&dis[to]>dis[x]+a[i].cost)
{
dis[to]=dis[x]+a[i].cost;
flow[to]=min(flow[x],a[i].cup);
pre[to]=x;preid[to]=i;
if(!vis[to])vis[to]=1,q.push(to);
}
}
}
return dis[t]!=INF;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&s,&t);
fo(i,1,m)
{
R int x,y,cup,cost;
scanf("%d%d%d%d",&x,&y,&cup,&cost);
add(x,y,cup,cost);add(y,x,0,-cost);
}
while(spfa())
{
for(int i=t;i!=s;i=pre[i])
a[preid[i]].cup-=flow[t],
a[preid[i]^1].cup+=flow[t];
ans1+=flow[t];
ans2+=flow[t]*dis[t];
}
cout<<ans1<<" "<<ans2;
return 0;
}
显示 Gitment 评论