原题链接

题意简述

模板题啦~

题解

每次都以费用作为边权求一下最短路,然后沿着最短路增广。

Code

//【模板】最小费用最大流
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0,f=1; char ch=gc();
while(ch<'0'||'9'<ch) {if(ch=='-') f=-1; ch=gc();}
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x*f;
}
int const N=5e3+10;
int const M=5e4+10;
int const INF=0x3F3F3F3F;
int n,m,s,t;
int cnt,h[N];
struct edge{int v,c,w,nxt;} ed[M<<1];
void edAdd(int u,int v,int c,int w)
{
++cnt; ed[cnt].v=v,ed[cnt].c=c,ed[cnt].w=w; ed[cnt].nxt=h[u],h[u]=cnt;
++cnt; ed[cnt].v=u,ed[cnt].c=0,ed[cnt].w=-w; ed[cnt].nxt=h[v],h[v]=cnt;
}
int dst[N],pre[N]; int q[N],op,cl; bool in[N];
bool SPFA()
{
op=cl=0;
memset(dst,0x3F,sizeof dst);
memset(pre,0,sizeof pre);
dst[s]=0; in[q[++cl%N]=s]=true;
while(op<cl)
{
int u=q[++op%N]; in[u]=false;
for(int i=h[u];i;i=ed[i].nxt)
{
int v=ed[i].v,w=ed[i].w;
if(dst[u]+w<dst[v] && ed[i].c)
{
pre[v]=i; dst[v]=dst[u]+w;
if(!in[v]) in[q[++cl%N]=v]=true;
}
}
}
return dst[t]<INF;
}
int main()
{
n=read(),m=read(); s=read(),t=read();
cnt=1;
for(int i=1;i<=m;i++)
{
int u=read(),v=read(),c=read(),w=read();
edAdd(u,v,c,w);
}
int flow=0,cost=0;
while(SPFA())
{
int fl=INF,u=t;
while(pre[u]) fl=min(fl,ed[pre[u]].c),u=ed[pre[u]^1].v;
flow+=fl; cost+=fl*dst[t];
u=t;
while(pre[u]) ed[pre[u]].c-=fl,ed[pre[u]^1].c+=fl,u=ed[pre[u]^1].v;
}
printf("%d %d\n",flow,cost);
return 0;
}

洛谷P3381 - 【模板】最小费用最大流的更多相关文章

  1. 洛谷P3381 (最小费用最大流模板)

    记得把数组开大一点,不然就RE了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 ...

  2. 洛谷.3381.[模板]最小费用最大流(zkw)

    题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...

  3. P3381 [模板] 最小费用最大流

    EK  + dijkstra (2246ms) 开氧气(586ms) dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381 ...

  4. 【洛谷 p3381】模板-最小费用最大流(图论)

    题目:给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 解法:在Dinic的基础下做spfa算法. 1 #include<cst ...

  5. 洛谷.4897.[模板]最小割树(Dinic)

    题目链接 最小割树模板.具体见:https://www.cnblogs.com/SovietPower/p/9734013.html. ISAP不知为啥T成0分了.. Dinic: //1566ms ...

  6. 网络流_spfa最小费用最大流

    最大流: 不断搜索增广路,寻找最小的容量-流量,得到最大流量,但最大流量在有花费时不一定是最小花费. 最小费用最大流 算法思想: 采用贪心的思想,每次找到一条从源点到达汇点的花费最小的路径,增加流量, ...

  7. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  8. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  9. 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)

    题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...

随机推荐

  1. POI--HSSFRow类

    用POI在工作表里作成一个行,可以用「HSSFRow」类,它的构造方法有三个. protected HSSFRow() protected HSSFRow(Workbook book, Sheet s ...

  2. js promise看这篇就够了

    一.背景 大家都知道nodejs很快,为什么会这么快呢,原因就是node采用异步回调的方式来处理需要等待的事件,使得代码会继续往下执行不用在某个地方等待着.但是也有一个不好的地方,当我们有很多回调的时 ...

  3. html中的title和alt

    alt是html标签的属性,而title既是html标签,又是html属性. title标签这个不用多说,网页的标题就是写在<title></title>这对标签之内的.tit ...

  4. ferror,clearerr和EOF含义

    1.我们并不是实时操纵文件,也不是实时生效,它依赖于缓冲区.非缓冲模式编程与常规区别,就是实时与不实时的区别. 2.//fgetc fputc, fgets fputs, fgetwc fputwc, ...

  5. 前端自动化测试神器-Katalon进阶用法

    前言 上一篇介绍了Katalon的基础用法,本篇继续介绍一些进阶的用法. Keyword 和 Method Call Statement Keyword Keyword就是自定义方法,该方法在当前项目 ...

  6. linux下的打包与压缩

    linux压缩或解压缩工具有很多,除了已经很少有人使用的compress外,现在常用的还有tar,bzip2,xz 和gziplinux压缩或解压缩工具有很多,除了已经很少有人使用的compress外 ...

  7. xBIM WeXplorer

    目录 基础 xBIM WeXplorer 简要介绍 xBIM WeXplorer xViewer 基本应用 xBIM WeXplorer xViewer 浏览器检查 xBIM WeXplorer xV ...

  8. python 3.x 爬虫基础---http headers详解

    前言 上一篇文章 python 爬虫入门案例----爬取某站上海租房图片 中有对headers的讲解,可能是对爬虫了解的不够深刻,所以老觉得这是一项特别简单的技术,也可能是简单所以网上对爬虫系统的文档 ...

  9. Codeforces Round #395 (Div. 2)(未完)

    2.2.2017 9:35~11:35 A - Taymyr is calling you 直接模拟 #include <iostream> #include <cstdio> ...

  10. asp.net core 中 sql server 2017 数据库连接测试

    使用sql server 2017 进行连接: 配置appsettings.json文件 { "ConnectionStrings": { "DefaultConnect ...