洛谷P3381 最小费用最大流
费用流板子
还是一道板子题。。先练练手
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 5005;
const int M = 50005;
int n, m, s, t, head[N], cnt, inq[N], pre[N], incf[N], dist[N], ans, mf;
struct Edge { int v, next, f, cost; } edge[M<<1];
void addEdge(int a, int b, int f, int cost){
edge[cnt].v = b, edge[cnt].f = f, edge[cnt].cost = cost, edge[cnt].next = head[a], head[a] = cnt ++;
edge[cnt].v = a, edge[cnt].f = 0, edge[cnt].cost = -cost, edge[cnt].next = head[b], head[b] = cnt ++;
}
bool spfa(){
full(dist, INF), full(inq, false);
full(pre, 0), full(incf, INF);
queue<int> q;
q.push(s);
dist[s] = 0, inq[s] = true;
while(!q.empty()){
int cur = q.front(); q.pop();
inq[cur] = false;
for(int i = head[cur]; i != -1; i = edge[i].next){
int u = edge[i].v;
if(edge[i].f > 0 && dist[u] > dist[cur] + edge[i].cost){
dist[u] = dist[cur] + edge[i].cost;
pre[u] = i, incf[u] = min(incf[cur], edge[i].f);
if(!inq[u]) q.push(u), inq[u] = true;
}
}
}
return dist[t] != INF;
}
void mcmf(){
while(spfa()){
int cur = t;
while(cur != s){
int i = pre[cur];
edge[i].f -= incf[t], edge[i^1].f += incf[t];
cur = edge[i^1].v;
}
mf += incf[t];
ans += dist[t] * incf[t];
}
}
int main(){
full(head, -1), cnt = 2;
n = read(), m = read(), s = read(), t = read();
for(int i = 0; i < m; i ++){
int u = read(), v = read(), w = read(), c = read();
addEdge(u, v, w, c);
}
mcmf();
printf("%d %d\n", mf, ans);
return 0;
}
洛谷P3381 最小费用最大流的更多相关文章
- 洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...
- 洛谷 [P3381] 最小费用最大流模版
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 【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]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
随机推荐
- IIS配置Url重写实现http自动跳转https的重定向方法(100%解决)
引言 本文推荐阅读地址:https://www.52abp.com/BlogDetails/10008 这种文章网上可以说一搜一大把,但是我为什么还要写呢,因为一搜一把没把我气死,都是东抄西挪的东西, ...
- docker环境搭建
参考地址:https://www.imooc.com/article/details/id/25228 操作系统Centos7 1.替换yum源为阿里云yum源: //备份yum源 mv /etc/y ...
- ARC 066D Xor Sum AtCoder - 2272 (打表找规律)
Problem Statement You are given a positive integer N. Find the number of the pairs of integers u and ...
- 我们为什么要使用List和Set(List,Set详解)
1.集合概述 类图 集合和数组的区别? 集合基本方法 集合特有的遍历方式? public static void main(String[] args) { //创建集合对象 Collection c ...
- Shell脚本2
5 Shell传递参数 我们可以在执行 Shell 脚本时,向脚本传递参数, 脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推…… ...
- IdentityServer4【QuickStart】之设置和概述
设置和概述 有两个基本的方式来开启一个新的IdentityServer项目: 从头开始 从asp.net Identity模板开始 如果你从头开始,我们提供了一些基于内存中构建的存储,所以你不必一开始 ...
- C# Note18: 使用wpf制作about dialog(关于对话框)
前言 基本上任何software或application都会在help菜单中,有着一个关于对话框,介绍产品的版权.版本等信息,还有就是对第三方的引用(add author credits). 首先,看 ...
- 校园电商项目3(基于SSM)——配置Maven
步骤一:添加必要文件夹 先在src/main/resources下添加两个文件夹 接着在webapp文件夹下添加一个resources文件夹存放我们的静态网页内容 WEB-INF里的文件是不会被客户端 ...
- countByValue
[1,2,3,3]的RDD rdd.foreach(println)---------------------1 2 3 3
- mybatis generator的maven插件,找不到properties的配置文件错误的解决
第一次运行的时候,maven插件是正确运行了的 但后面对 maven 的 build节点做了一点修改,就开始报错,找不到 properties标签指定的的数据库连接配置文件了 修改部分如下: 这个操作 ...