最小费用最大流(费用流)模板

P3381 【模板】最小费用最大流
题目描述
如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用。

输入格式
第一行包含四个正整数N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。

接下来M行每行包含四个正整数ui、vi、wi、fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi。

输出格式
一行,包含两个整数,依次为最大流量和在最大流量情况下的最小费用。

输入输出样例
输入 #1复制
4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5
输出 #1复制
50 280
说明/提示
时空限制:1000ms,128M

(BYX:最后两个点改成了1200ms)

数据规模:

对于30%的数据:N<=10,M<=10

对于70%的数据:N<=1000,M<=1000

对于100%的数据:N<=5000,M<=50000

样例说明:

如图,最优方案如下:

第一条流为4–>3,流量为20,费用为3*20=60。

第二条流为4–>2–>3,流量为20,费用为(2+1)*20=60。

第三条流为4–>2–>1–>3,流量为10,费用为(2+9+5)*10=160。

故最大流量为50,在此状况下最小费用为60+60+160=280。

故输出50 280。

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f

using namespace std;
const int N=1e5+5;//两倍
struct edge
{
    int to,next,flow,dis;//flow流量 dis费用
}a[N];//正反向边
int head[N],num=0,vis[N];
int n,m,s,t,u,v,f,w,dis[N],pre[N],last[N],flow[N],maxflow,mincost;
//dis最小费用 pre每个点的前驱
//last每个点所连的前一条边的编号 flow源点到此处点的流量

void add(int from,int to,int flow,int dis)
{
    a[num].next=head[from];
    a[num].to=to;
    a[num].flow=flow;
    a[num].dis=dis;
    head[from]=num++;
    a[num].next=head[to];
    a[num].to=from;
    a[num].flow=0;
    a[num].dis=-dis;
    head[to]=num++;
}
int spfa(int s,int t)//正向
{
    memset(dis,inf,sizeof(dis));
    memset(flow,inf,sizeof(flow));
    memset(vis,0,sizeof(vis));
    queue<int> q;
    q.push(s);
    vis[s]=1;
    dis[s]=0;
    pre[t]=-1;
    while(!q.empty())
    {
        int now=q.front();
        q.pop();
        vis[now]=0;
        for(int i=head[now];i!=-1;i=a[i].next)
        {
            if(a[i].flow>0&&dis[a[i].to]>dis[now]+a[i].dis)
            {
                dis[a[i].to]=dis[now]+a[i].dis;
                pre[a[i].to]=now;
                last[a[i].to]=i;//边的编号
                flow[a[i].to]=min(flow[now],a[i].flow);
                if(vis[a[i].to]==0)
                {
                    vis[a[i].to]=1;
                    q.push(a[i].to);
                }
            }
        }
    }
    return pre[t]!=-1;
}
void MCMF()//逆向
{
    while(spfa(s,t))
    {
        int now=t;
        maxflow+=flow[t];
        mincost+=flow[t]*dis[t];
        while(now!=s)
        {//从汇点回溯到源点
            a[last[now]].flow-=flow[t];
            a[last[now]^1].flow+=flow[t];
            now=pre[now];
        }
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(head,-1,sizeof(head));
    num=0;//边数
    cin >> n >> m >> s >> t;
    while(m--)
    {
        cin >> u >> v >> f >> w;
        add(u,v,f,w);//反向边流量为0,费用为相反数
    }
    maxflow=0,mincost=0;
    MCMF();
    printf("%d %d\n",maxflow,mincost);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值