EK  + dijkstra (2246ms) 开氧气(586ms)

dijkstra的势 可以处理负权 https://www.luogu.org/blog/28007/solution-p3381

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9; int n, m, s, t, cnt; struct node {
int to, nex, val, cost;
}E[100005];
int head[5005]; void addedge(int x, int y, int va, int cos) {
E[++cnt].to = y; E[cnt].nex = head[x]; head[x] = cnt; E[cnt].val = va; E[cnt].cost = cos;
} struct no1 {
int to, edge;
}pre[5005]; struct no2 {
int d, id;
friend bool operator < (no2 A, no2 B) {
return A.d > B.d;
}
}; int dis[5005];
int h[5005];
bool dij() { for(int i = 1; i <= n; i++) dis[i] = INF;
priority_queue<no2> que; dis[s] = 0;
que.push((no2){0, s}); while(!que.empty()) {
no2 u = que.top();
que.pop();
if(dis[u.id] < u.d) continue; for(int i = head[u.id]; i; i = E[i].nex) {
int v = E[i].to;
if(E[i].val > 0 && dis[v] + h[v] > dis[u.id] + E[i].cost + h[u.id]) {
dis[v] = dis[u.id] + E[i].cost + h[u.id] - h[v];
pre[v].to = u.id;
pre[v].edge = i;
que.push((no2){dis[v], v});
}
}
}
if(dis[t] != INF) return true;
else return false;
} int ans, cos1;
void EK() {
ans = cos1 = 0; while(dij()) {
int mi = INF;
for(int i = t; i != s; i = pre[i].to) {
mi = min(mi, E[pre[i].edge].val);
}
for(int i = t; i != s; i = pre[i].to) {
E[pre[i].edge].val -= mi;
E[pre[i].edge ^ 1].val += mi;
}
for(int i = 1; i <= n; i++) h[i] += dis[i];
ans += mi;
cos1 += mi * h[t];
}
} int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
cnt = 1; for(int i = 1; i <= m; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
addedge(a, b, c, d);
addedge(b, a, 0, -d);
}
EK();
printf("%d %d\n", ans, cos1);
return 0;
}

EK + spfa (1775ms) 开氧气(1398ms)

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9; int n, m, s, t, cnt; struct node {
int to, nex, val, cost;
}E[100005];
int head[5005]; void addedge(int x, int y, int va, int cos) {
E[++cnt].to = y; E[cnt].nex = head[x]; head[x] = cnt; E[cnt].val = va; E[cnt].cost = cos;
} struct no1 {
int to, edge;
}pre[5005]; int dis[5005];
int vis[5005]; bool spfa() {
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis)); queue<int> que;
que.push(s);
vis[s] = 1;
dis[s] = 0;
while(!que.empty()) {
int u = que.front();
vis[u] = 0;
que.pop(); for(int i = head[u]; i; i = E[i].nex) {
int v = E[i].to;
if(E[i].val > 0 && dis[v] > dis[u] + E[i].cost) {
dis[v] = dis[u] + E[i].cost;
pre[v].to = u;
pre[v].edge = i;
if(!vis[v]) {
vis[v] = 1;
que.push(v);
}
}
}
}
if(dis[t] != 0x3f3f3f3f) return true;
else return false;
} int ans, cos1;
void EK() {
ans = cos1 = 0; while(spfa()) {
int mi = INF;
for(int i = t; i != s; i = pre[i].to) {
mi = min(mi, E[pre[i].edge].val);
}
for(int i = t; i != s; i = pre[i].to) {
E[pre[i].edge].val -= mi;
E[pre[i].edge ^ 1].val += mi;
}
ans += mi;
cos1 += mi * dis[t];
}
} int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
cnt = 1; for(int i = 1; i <= m; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
addedge(a, b, c, d);
addedge(b, a, 0, -d);
}
EK();
printf("%d %d\n", ans, cos1);
return 0;
}

Dinic多路增广 + 弧优化 + spfa (1744ms)

#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9; int n, m, s, t, cnt;
int maxflow, mincost; struct node {
int to, nex, val, cost;
}E[100005];
int head[5005];
int cur[5005]; void addedge(int x, int y, int va, int cos) {
E[++cnt].to = y; E[cnt].nex = head[x]; head[x] = cnt; E[cnt].val = va; E[cnt].cost = cos;
} struct no1 {
int to, edge;
}pre[5005]; int dis[5005];
int inque[5005];
int vis[5005]; bool spfa() {
for(int i = 1; i <= n; i++) inque[i] = 0, dis[i] = 0x3f3f3f3f, cur[i] = head[i], vis[i] = 0; queue<int> que;
que.push(s);
inque[s] = 1;
dis[s] = 0;
while(!que.empty()) {
int u = que.front();
inque[u] = 0;
que.pop(); for(int i = head[u]; i; i = E[i].nex) {
int v = E[i].to;
if(E[i].val > 0 && dis[v] > dis[u] + E[i].cost) {
dis[v] = dis[u] + E[i].cost;
pre[v].to = u;
pre[v].edge = i;
if(!inque[v]) {
inque[v] = 1;
que.push(v);
}
}
}
}
if(dis[t] != 0x3f3f3f3f) return true;
else return false;
} int dfs(int x, int flow) {
if(x == t) {
vis[x] = 1;
maxflow += flow;
return flow;
} vis[x] = 1;
int used = 0;
int rflow = 0;
for(int i = cur[x]; i; i = E[i].nex) {
cur[x] = i;
int v = E[i].to;
if(E[i].val > 0 && dis[v] == dis[x] + E[i].cost && (!vis[v] || v == t)) {
if(rflow = dfs(v, min(flow - used, E[i].val))) {
used += rflow;
E[i].val -= rflow;
E[i ^ 1].val += rflow;
mincost += E[i].cost * rflow;
if(used == flow) break;
}
}
}
return used;
} void dinic() {
maxflow = mincost = 0;
while(spfa()) {
vis[t] = 1;
while(vis[t]) {
memset(vis, 0, sizeof(vis));
dfs(s, INF);
}
}
} int main() {
scanf("%d%d%d%d", &n, &m, &s, &t);
cnt = 1; for(int i = 1; i <= m; i++) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
addedge(a, b, c, d);
addedge(b, a, 0, -d);
}
dinic();
printf("%d %d\n", maxflow, mincost);
return 0;
}

P3381 [模板] 最小费用最大流的更多相关文章

  1. 【洛谷 p3381】模板-最小费用最大流(图论)

    题目:给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 解法:在Dinic的基础下做spfa算法. 1 #include<cst ...

  2. 洛谷P3381 (最小费用最大流模板)

    记得把数组开大一点,不然就RE了... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define int long long 4 ...

  3. 洛谷.3381.[模板]最小费用最大流(zkw)

    题目链接 Update:我好像刚知道多路增广就是zkw费用流.. //1314ms 2.66MB 本题优化明显 #include <queue> #include <cstdio&g ...

  4. P3381 【模板】最小费用最大流

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入输出格式 输入格式: 第一行 ...

  5. 洛谷P3381 最小费用最大流模板

    https://www.luogu.org/problem/P3381 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用 ...

  6. 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)

    题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...

  7. P3381 【模板】最小费用最大流(MCMF)

    P3381 [模板]最小费用最大流 题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入格式 第一行包含四个正整数N ...

  8. 洛谷P3381 - 【模板】最小费用最大流

    原题链接 题意简述 模板题啦~ 题解 每次都以费用作为边权求一下最短路,然后沿着最短路增广. Code //[模板]最小费用最大流 #include <cstdio> #include & ...

  9. Luogu P3381 (模板题) 最小费用最大流

    <题目链接> 题目大意: 给定一张图,给定条边的容量和单位流量费用,并且给定源点和汇点.问你从源点到汇点的最带流和在流量最大的情况下的最小费用. 解题分析: 最小费用最大流果题. 下面的是 ...

随机推荐

  1. go module 基本使用

    前言 go的版本以至1.13,一直以来令人诟病的依赖管理也有了官方的方向,但是看了一下目前很多blog文章还是比较老的. 所以这里对 go mod 做一个大致的说明 正文 前提 go版本为1.13及以 ...

  2. 【C++】《C++ Primer 》第八章

    第八章 IO库 一.IO类 1. 标准库定义的IO类型 头文件 作用 类型 iostream 从标准流中读写数据 istream, wistream 从流读取数据 ostream, wostream ...

  3. IDEA一步步创建Maven管理的Spring入门程序

    目前,做Java开发的很多人都在使用IDEA了,而有些人也选择用Eclipse,我这里介绍一下IDEA一步步创建Maven项目的步骤,并创建一个Spring的入门程序(Java项目,非Web项目),讲 ...

  4. ip访问本机vs调试项目

    环境:win10 vs2019 webapi F5启动调试. 问题:localhost可以访问,127.0.0.1和本机ip访问不了.比如想让别人浏览一下看效果,或者测试人员测试功能,每次修改都有重新 ...

  5. yum -y install gnuplot

    [root@test~]# yum -y install gnuplotLoaded plugins: fastestmirrorLoading mirror speeds from cached h ...

  6. Celery--短信与邮件

    1 Celery 实现短信--邮件 1.1 容联云-短信 from ronglian_sms_sdk import SmsSDK accountSid = '8a216da8757784cd01759 ...

  7. MySQL全面瓦解17:触发器相关

    关于触发器 现实开发中我们经常会遇到这种情况,比如添加.删除和修改信息的时候需要记录日志,我们就要在完成常规的数据库逻辑操作之后再去写入日志表,这样变成了两步操作,更复杂了. 又比如删除一个人员信息的 ...

  8. kubernets之卷

    一 卷的由来以及种类和常用的卷的类型 前面介绍了大部分都是pod的管理以及在集群内部和集群外部如何访问pod,但是我们也了解到,pod是有生命周期的,当pod所在节点下线,或者等其他原因原因导致pod ...

  9. LeetCode383. 赎金信

    题目 给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成.如果可以构成,返回 tru ...

  10. 私有镜像仓库Harbor基础介绍与部署

    企业级私有镜像仓库Harbor 一:介绍 Harbor,是一个英文单词,意思是港湾,港湾是干什么的呢,就是停放货物的,而货物呢,是装在集装箱中的,说到集装箱,就不得不提到Docker容器,因为dock ...