洛谷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]).建两次图分别跑最小费用最大流和最大费用最大流即可 ...
随机推荐
- 通用权限管理系统多语言开发标准接口 - java,php 调用标准接口程序参考
1:公司里有多个业务系统,需要进行统一重构,有PHP的.有Java的.有.NET的,甚至还有 Delphi 的. 2:公司里有多个数据库系统,有mysql的.有sqlserver的.还有oracel的 ...
- outlook署名最后一行没换行
Outlook のプレーン テキスト形式での投稿で改行が削除されます 1.通过更改outlook默认设置可以解决 https://support.microsoft.com/ja-jp/help/28 ...
- Mike and strings CodeForces - 798B (又水又坑)
题目链接 题意:英语很简单,自己取读吧. 思路: 既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞. 本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行) ...
- Redis集群之Jedis的使用
maven依赖 <!-- Redis客户端 --> <dependency> <groupId>redis.clients</groupId> < ...
- 打开指定测试App的指定Activity
那究竟应该如何让appium去自动找到指定的APP和指定的Activity呢?想要打开指定的App,需要知道App的包名,同样想要打开指定Activity也需要知道其名,如何获取? 1.问公司的开发人 ...
- 福州大学软件工程1816 | W班 第10次作业[软件工程实践总结]
作业链接 个人作业--软件工程实践总结 评分细则 本次由五个问题(每个十分)+创意照片(五分)+附加题(十分)组成 评分统计图 千帆竞发图 汇总成绩排名链接 汇总链接
- 关于Fatal error: Paletter image not supported by webp 报错
报错提示 Fatal error: Paletter image not supported by webp 原因是由于图片被非法编辑过(相对PHP来说)造成, 有可能是某些编辑图片的软件的格式与PH ...
- [转帖]Linux 下如何知道是否有人在使坏?
Linux 下如何知道是否有人在使坏? 学到了两个最简单的命令 usermod -L username 锁定账户 passwd -s username 查看用户状态. 自己linux 知道的还是少 需 ...
- 《Effective C++》让自己习惯C++:条款1-条款4
条款1:视C++为一个语言联邦 可以将C++分为4个层次: 1.C:C++实在C语言的基础上发展而来的. 2:Object-Oriented C++:C++面向对象. 3:Template C++:C ...
- eclipse中添加tomcat
https://blog.csdn.net/Forlogen/article/details/54090335(copy) 为了Java Web的开发,下面我们来安装一下Tomcat服务器,并将其配置 ...