P3381 【模板】最小费用最大流(spfa板子)
#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL; const int maxm = 5e5+;
const int INF = 0x3f3f3f3f; struct edge{
int u, v, cap, flow, cost, nex;
} edges[maxm]; int head[maxm], cur[maxm], cnt, fa[maxm], d[maxm], n;
bool inq[maxm]; void init() {
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost) {
edges[cnt] = edge{u, v, cap, , cost, head[u]};
head[u] = cnt++;
} void addedge(int u, int v, int cap, int cost) {
add(u, v, cap, cost), add(v, u, , -cost);
} bool spfa(int s, int t, int &flow, LL &cost) {
for(int i = ; i <= n+; ++i) d[i] = INF; //init()
memset(inq, false, sizeof(inq));
d[s] = , inq[s] = true;
fa[s] = -, cur[s] = INF;
queue<int> q;
q.push(s);
while(!q.empty()) {
int u = q.front();
q.pop();
inq[u] = false;
for(int i = head[u]; i != -; i = edges[i].nex) {
edge& now = edges[i];
int v = now.v;
if(now.cap > now.flow && d[v] > d[u] + now.cost) {
d[v] = d[u] + now.cost;
fa[v] = i;
cur[v] = min(cur[u], now.cap - now.flow);
if(!inq[v]) {q.push(v); inq[v] = true;}
}
}
}
if(d[t] == INF) return false;
flow += cur[t];
cost += 1LL*d[t]*cur[t];
for(int u = t; u != s; u = edges[fa[u]].u) {
edges[fa[u]].flow += cur[t];
edges[fa[u]^].flow -= cur[t];
}
return true;
} int MincostMaxflow(int s, int t, LL &cost) {
cost = ;
int flow = ;
while(spfa(s, t, flow, cost));
return flow;
} void run_case() {
init();
int m, s, t, u, v, w, f;
cin >> n >> m >> s >> t;
for(int i = ; i < m; ++i) {
cin >> u >> v >> w >> f;
addedge(u, v, w, f);
}
LL cost = ;
int flow = MincostMaxflow(s, t, cost);
cout << flow << " " << cost;
} int main() {
ios::sync_with_stdio(false), cin.tie();
run_case();
cout.flush();
return ;
}
P3381 【模板】最小费用最大流(spfa板子)的更多相关文章
- P3381 [模板] 最小费用最大流
EK + dijkstra (2246ms) 开氧气(586ms) dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381 ...
- 【洛谷 p3381】模板-最小费用最大流(图论)
题目:给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 解法:在Dinic的基础下做spfa算法. 1 #include<cst ...
- 洛谷P3381 (最小费用最大流模板)
记得把数组开大一点,不然就RE了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 ...
- 洛谷.3381.[模板]最小费用最大流(zkw)
题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...
- 最小费用最大流spfa
#include <iostream> #include <cstring> #include <cstdio> #include <queue> #d ...
- [SDOI2009]晨跑[最小费用最大流]
[SDOI2009]晨跑 最小费用最大流的板子题吧 令 \(i'=i+n\) \(i -> i'\) 建一条流量为1费用为0的边这样就不会对答案有贡献 其次是对 \(m\) 条边建 \(u'-& ...
- 费用流+SPFA ||Luogu P3381【模板】最小费用最大流
题面:[模板]最小费用最大流 代码: #include<cstdio> #include<cstring> #include<iostream> #include& ...
- 【Luogu】P3381最小费用最大流模板(SPFA找增广路)
题目链接 哈 学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...
- P3381 【模板】最小费用最大流
P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...
随机推荐
- L3-023 计算图
建立结构体保存每个结点的前驱,操作符,来回两遍拓扑排序~ #include<bits/stdc++.h> using namespace std; ; struct node { vect ...
- webpack搭建vue项目
链接:https://blog.csdn.net/qq_42181069/article/details/81137180 __dirname : 文件的绝对路径
- Codeforces Round #618 (Div. 2)C. Anu Has a Function
Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For ...
- 温湿度传感器AM2302(DH22)
AM2302 3.3V - 5.5V,建议供电电压为 5V单总线通信模式时,SDA 上拉(开漏)后与微处理器的 I/O 端口相连.单总线通信特殊说明: 0.功耗待机40~50uA;测量1~1.5m ...
- HttpClient和HtmlUnit的比较总结以及使用技巧
本文转自: https://blog.csdn.net/zstu_cc/article/details/39250903 https://blog.csdn.net/zstu_cc/article/d ...
- LinkList(链表)
code: #include <stdio.h> #include <time.h> #include <conio.h> #include <stdlib. ...
- 第七节:Vuejs路由交互及后台系统路由案例
一. 简介 1.路由的概念 路由的本质就是一种对应关系,比如说我们在url地址中输入我们要访问的url地址之后,浏览器要去请求这个url地址对应的资源.那么url地址和真实的资源之间就有一种对应的关系 ...
- Sqlserver 日志文件收缩命令
SELECT NAME,recovery_model_desc FROM sys.databases -- 如果是FULL类型,修改为SIMPLE类型 ALTER DATABASE DBName SE ...
- Java入门笔记 03-面向对象(上)
介绍:Java是面向对象的程序设计语言,类是面向对象的重要内容,可以把类当成是一种自定义类型,可以使用类来定义变量,这种类型的变量统称为引用变量.也就是说,所有类都是引用类型.Java也支持面向对象的 ...
- springmvc_ajax异步上传文件(基于ajaxfileupload.js)
引入js <script th:src="@{/js/ajaxfileupload.js}"></script> html <tr> <t ...