洛谷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]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
随机推荐
- Microsoft Tech Summit 2018 课程简述:利用 Windows 新特性开发出更好的手绘视频应用
概述 Microsoft Tech Summit 2018 微软技术暨生态大会将于10月24日至27日在上海世博中心举行,这也会是国内举办的最后一届 Tech Summit,2019 年开始会以 Mi ...
- list tuple
----------------------------善始者善终,笑到最后的,才是最good的.--------------------------------------------------- ...
- i++ 相比 ++i 哪个更高效?为什么?
++i的效率高些,++i在运算过程中不产生临时对象,返回的就是i,是个左值,类似++i=1这样的表达式是合法的,而i++在运算的过程中会产生临时对象,返回的是零时对象的值,是个右值,像i++=1这样的 ...
- Really Big Numbers CodeForces - 817C (数学规律+二分)
C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...
- Day10 Python基础之特殊函数(八)
一些特殊函数 1.递归函数(recursion) 递归函数的定义:在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 递归函数的优点:是定义简单,逻辑清晰.理论上,所 ...
- iOS开发之一句代码检测APP版本的更新
提示更新效果图如下,当然也是可以自定义类似与AlertView相似的自定义view,如京东.网易云音乐都是自定义了这种提示框的view.以下只展示,从App Store获取到app信息.并解析app信 ...
- iOS原生实现二维码拉近放大
http://www.cocoachina.com/ios/20180416/23033.html 2018-04-16 15:34 编辑: yyuuzhu 分类:iOS开发 来源:程序鹅 8 300 ...
- Druid Monitor开启登录界面
<!-- druid --> <filter> <filter-name>druidWebStatFilter</filter-name> <fi ...
- rem 适配
postcss-pxtorem 是一款 postcss 插件,用于将单位转化为 rem lib-flexible 用于设置 rem 基准值 一.webpact postcss 插件将px转化为rem单 ...
- C#实现,C++实现,JS实现 阿拉伯数字金额转换为中文大写金额
推荐在线编译器 ideone 1. C#实现 :带有负数处理 //把数字金额转换成中文大写数字的函数 //带有负值处理 function changeNumMoneyToChinese(money) ...