洛谷 [P3381] 最小费用最大流模版
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
const int MAXN = 5005;
int init() {
int rv = 0, fh = 1;
char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') fh = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
rv = (rv<<1) + (rv<<3) + c - '0';
c = getchar();
}
return fh * rv;
}
int head[MAXN], nume, n, m, mincost, maxflow, ss, tt, delta, dis[MAXN], pre[MAXN];
bool f[MAXN];
struct edge{
int to, nxt, flow, cap, cost;
}e[MAXN * 30];
void adde(int from, int to, int cap, int cost) {
e[++nume].to = to;
e[nume].cap = cap;
e[nume].cost = cost;
e[nume].nxt = head[from];
head[from] = nume;
}
queue <int> q;
bool SPFA() {
memset(dis, 0x3f, sizeof(dis));
memset(pre, 0, sizeof(pre));
q.push(ss); dis[ss] = 0; pre[ss] = 0; f[ss] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
f[u] = 0;
for(int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if(e[i].flow < e[i].cap && dis[v] > dis[u] + e[i].cost) {
dis[v] = dis[u] + e[i].cost;
pre[v] = i;
if(!f[v]) {
q.push(v); f[v] = 1;
}
}
}
}
return dis[tt] != 0x3f3f3f3f;
}
void MCMF() {
while(SPFA()) {
delta = 0x3f3f3f3f;
for(int i = pre[tt]; i; i = pre[e[(((i - 1) ^ 1) + 1)].to])
delta = min(delta, e[i].cap - e[i].flow);
for(int i = pre[tt]; i; i = pre[e[((i - 1) ^ 1) + 1].to]) {
e[i].flow += delta;
e[((i - 1) ^ 1) + 1].flow -= delta;
mincost += delta * e[i].cost;
}
maxflow += delta;
}
}
int main() {
n = init(); m = init(); ss = init(); tt = init();
for(int i = 1; i <= m; i++) {
int u = init(), v = init(), cap = init(), cost = init();
adde(u, v, cap, cost); adde(v, u, 0, -cost);
}
MCMF();
printf("%d %d\n", maxflow, mincost);
return 0;
}
洛谷 [P3381] 最小费用最大流模版的更多相关文章
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
- 洛谷P3381 最小费用最大流
费用流板子 还是一道板子题..先练练手 #include <bits/stdc++.h> #define INF 0x3f3f3f3f #define full(a, b) memset( ...
- 【Luogu】P3381最小费用最大流模板(SPFA找增广路)
题目链接 哈 学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...
- 【luogu P3381 最小费用最大流】 模板
题目链接:https://www.luogu.org/problemnew/show/P3381 把bfs变成spfa #include <queue> #include <cstd ...
- 洛谷P3381 - 【模板】最小费用最大流
原题链接 题意简述 模板题啦~ 题解 每次都以费用作为边权求一下最短路,然后沿着最短路增广. Code //[模板]最小费用最大流 #include <cstdio> #include & ...
- 洛谷 P4307 [JSOI2009]球队收益 / 球队预算(最小费用最大流)
题面 luogu 题解 最小费用最大流 先假设剩下\(m\)场比赛,双方全输. 考虑\(i\)赢一局的贡献 \(C_i*(a_i+1)^2+D_i*(b_i-1)^2-C_i*a_i^2-D_i*b_ ...
- 洛谷 P2053 [SCOI2007]修车(最小费用最大流)
题解 最小费用最大流 n和m是反着的 首先, \[ ans = \sum{cost[i][j]}*k \] 其中,\(k\)为它在当前技术人员那里,排倒数第\(k\)个修 我们可以对于每个技术人员进行 ...
- 洛谷 P4016 负载平衡问题 【最小费用最大流】
求出平均数sum,对于大于sum的点连接(s,i,a[i]-sum,0),表示这个点可以流出多余的部分,对于小于sum的点连接(i,t,sum-a[i],0)表示这个点可以接受少的部分,然后每个点向相 ...
- 洛谷 P4015 运输问题 【最小费用最大流+最大费用最大流】
s向仓库i连ins(s,i,a[i],0),商店向t连ins(i+m,t,b[i],0),商店和仓库之间连ins(i,j+m,inf,c[i][j]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
随机推荐
- oc block排序
NSArray *sortArr=[arr sortedArrayUsingSelector:@selector(compareWithClassAndName:)]; //数组排序--block N ...
- Vue源码学习一 ———— Vue项目目录
Vue 目录结构 可以在 github 上通过这款 Chrome 插件 octotree 查看Vue的文件目录.也可以克隆到本地.. Vue 是如何规划目录的 scripts ------------ ...
- swift 循环语句
// // main.swift // switch // // Created by lanou on 16/10/21. // Copyright (c) 2016年 lanou. All rig ...
- 新装Ubuntu后的一些配置
一:Ubuntu 16.04 开启root用户和使用root用户登陆 1. 编辑/etc/lightdm/lightdm.conf autologin-guest=false autologin-us ...
- mysql 存储过程 例子
DROP PROCEDURE IF EXISTS variable_demo; delimiter // CREATE PROCEDURE variable_demo() BEGIN select ' ...
- mybatis的环境搭建
mybatis是一个持久层框架,其主要思想就是想将程序中大量的SQL语句剥离出来,配置在配置文件中,实现SQL的灵活配置. 使得SQL与程序代码分离,即在不修改程序代码的情况下,直接在配置文件中修改S ...
- PHP获取接下来一周的日期
//获取接下来一周的日期 function GetWeeks() { $i=0; $weeks=[]; for ($i;$i<=7;$i++){ $month=date('m',time()+8 ...
- Scrapy用pipelines把字典保存为csv格式
import csv class MyProjectPipeline(object): # 保存为csv格式 def __init__(self): # 打开文件,指定方式为写,利用第3个参数把csv ...
- Python 交互模式中 Delete/Backspace 键乱码问题
进入 Python 交互模式,按下 Delete/Backspace 键,会出现 ^H 字符 解决方式: 1. 进到 Python 的Modules目录 [root@cyt-test Python-2 ...
- Diycode开源项目 SettingActivity分析
1.整体效果预览及布局分析 1.1.设置界面预览 1.2.主体对应关系 注意这里的线条用ImageView来实现 有一个TextView是检查更新,默认隐藏,具体出现时间还得之后确认. 最后一个Lin ...