首页 > 其他分享 >费用流模板--EK

费用流模板--EK

时间:2022-12-26 15:44:44浏览次数:57  
标签:ci EK -- flow tot int now 模板 dis

费用流

#include <bits/stdc++.h>
using namespace std;
const int N=5005,M=1e6+5,inf=1e9;

int h[N],ne[M],e[M],w[M],c[M],tot=1;
void add(int from,int to,int w1,int w2,int ci) {
    e[++tot]=to;  w[tot]=w1;  c[tot]=ci;  ne[tot]=h[from];  h[from]=tot;
    e[++tot]=from;w[tot]=w2;  c[tot]=-ci; ne[tot]=h[to];    h[to]=tot;
}

int S,T;
bool st[N];
int dis[N],pre[N],flow[N];
bool spfa() {
    memset(dis,0x3f,sizeof(dis));
    memset(flow,0,sizeof(flow));
    queue<int>q;
    q.push(S);
    flow[S]=inf;
    dis[S]=0;
    st[S]=1;
    while(!q.empty()) {
        int now=q.front();
        q.pop();
        st[now]=0;
        for(int i=h[now];i;i=ne[i]) {
            int to=e[i];
            if(w[i]>0&&dis[to]>dis[now]+c[i]) {
                dis[to]=dis[now]+c[i];
                pre[to]=i;
                flow[to]=min(w[i],flow[now]);
                if(st[to]==0)q.push(to),q.push(to);
            }
        }
    }
    return flow[T];
}

void EK() {
    int ans1=0,ans2=0;
    while(spfa()) {
        int tmp=flow[T];
        ans1+=tmp,ans2+=tmp*dis[T];
        for(int i=T;i!=S;i=e[pre[i]^1]) {
            w[pre[i]]-=tmp;
            w[pre[i]^1]+=tmp;
        }
    }
    cout<<ans1<<' '<<ans2<<endl;
}

int main() {
    int n,m;
    cin>>n>>m>>S>>T;
    while(m--) {
        int x,y,wi,ci;
        cin>>x>>y>>wi>>ci;
        add(x,y,wi,0,ci);
    }
    EK();
    return 0;
}

标签:ci,EK,--,flow,tot,int,now,模板,dis
From: https://www.cnblogs.com/basicecho/p/17005952.html

相关文章

  • nginx在二级目录访问需要加/才能访问的解决办法
    nginx为什么在二级目录访问的时候要加/才能访问到比如https://scout.dhhb.com:9443/bigScreen要这样https://scout.dhhb.com:9443/bigScreen/才能访问到不加最后那个/无......
  • 实验6
    #define_CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h>intmain(){intcount=0;FILE*fp;fp=fopen("C:\\Users\\yuyee\\Desktop......
  • CSS自适应宽度按钮
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""​​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd​​​"><htmlxmlns="​​​http://ww......
  • 基于加权人工鱼群算法的深空天线定位PID控制器优化设计附matlab代码
    ✅作者简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,matlab项目合作可私信。......
  • Redis--数据结构--命令汇总
    Redis--数据结构--命令汇总​​1.String​​​​2.Hash​​​​3.List​​​​4.Set​​​​5.SortedSet​​​​6.其他​​​​6.1获取全部的key​​​​6.2key是......
  • 技嘉965P-DS3 + E6300如何超频的傻…
    1、开机进入BIOS,按CTRL+F1,打开超频隐藏选项;2、进入MB Intelligent Tweaker(M.I.T)3、将CPU Host Clock Control设置为Enable;4、将CPU ......
  • 泪流满面的404页面
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""​​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd​​​"><htmlxmlns="​​​http://ww......
  • 兼容IE6和Firefox的PNG背景透明CSS…
    <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""​​http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd​​​"><htmlxmlns="​​​http://ww......
  • daily study 3
    学习goto语句,可以直接跳到需要的位置。学习函数,分为库函数和自定函数,学习函数的参数,调用,嵌套调用和链式调用,函数的声明和定义,函数的递归c语言库函数:io函数,字符串操作函数,字......
  • 内网大文件上传详解及实例代码
    ​ 前言文件上传是一个老生常谈的话题了,在文件相对比较小的情况下,可以直接把文件转化为字节流上传到服务器,但在文件比较大的情况下,用普通的方式进行上传,这可不是一个好......