洛谷P3381 最小费用最大流模板
https://www.luogu.org/problem/P3381
题目描述
如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用。
输入格式
第一行包含四个正整数N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。
接下来M行每行包含四个正整数ui、vi、wi、fi,表示第i条有向边从ui出发,到达vi,边权为wi(即该边最大流量为wi),单位流量的费用为fi。
输出格式
一行,包含两个整数,依次为最大流量和在最大流量情况下的最小费用。
4 5 4 3
4 2 30 2
4 3 20 3
2 3 20 1
2 1 30 9
1 3 40 5
50 280
#include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int MAXN = 1e5 + ;
const int inf = 0x3f3f3f3f; int n, m, st, ed;//点数 边数 源点 汇点
int vis[MAXN], dis[MAXN], flow[MAXN];//源点到i点的花费和流量
int pre[MAXN];//每个点的前驱
int last[MAXN]; //每个点连的前一条边
int mincost, maxflow;
queue<int> Q; struct Edge
{
int to, next, flow, dis;//费用作为 dis 来跑最短路
}edge[MAXN];
int head[MAXN], cnt; void add(int a, int b, int c, int d) //要用到 ^ 操作 所以边从 0 开始
{
edge[++ cnt].to = b;
edge[cnt].next = head[a];
edge[cnt].flow = c;
edge[cnt].dis = d;
head[a] = cnt;
} bool spfa(int st, int ed)
{
mem(dis, inf), mem(flow, inf), mem(vis, );//找到最大流以及最短路(最少费用)
Q.push(st);
vis[st] = ;
dis[st] = ;
pre[ed] = -;
while(!Q.empty())
{
int now = Q.front();
Q.pop();
vis[now] = ;
for(int i = head[now]; i != -; i = edge[i].next)
{
if(edge[i].flow > && dis[edge[i].to] > dis[now] + edge[i].dis)
{
dis[edge[i].to] = dis[now] + edge[i].dis;
pre[edge[i].to] = now;//记录更新后的新前驱
last[edge[i].to] = i;//记录更新后的新的前一条边 为了回溯来更新边的流量
flow[edge[i].to] = min(flow[now], edge[i].flow);//找出最大流
if(!vis[edge[i].to])
{
vis[edge[i].to] = ;
Q.push(edge[i].to);
}
}
}
}
return pre[ed] != -;
} void min_cost_max_flow()
{
while(spfa(st, ed))
{
int now = ed;
maxflow += flow[ed];
mincost += flow[ed] * dis[ed];
while(now != st)//从汇点回溯更新边剩下的流量
{
edge[last[now]].flow -= flow[ed];
edge[last[now] ^ ].flow += flow[ed];
now = pre[now];
}
}
} int main()
{
mem(head, -), cnt = -;
scanf("%d%d%d%d", &n, &m, &st, &ed);
for(int i = ; i <= m; i ++)
{
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d); //有向边起点 终点 容量 单位费用
add(a, b, c, d);
add(b, a, , -d);//反向边流量为 0 ,花费为 负的
}
min_cost_max_flow();
printf("%d %d\n", maxflow, mincost);
return ;
}
最小费用最大流模板
洛谷P3381 最小费用最大流模板的更多相关文章
- 洛谷P3381 最小费用最大流
费用流板子 还是一道板子题..先练练手 #include <bits/stdc++.h> #define INF 0x3f3f3f3f #define full(a, b) memset( ...
- 洛谷 [P3381] 最小费用最大流模版
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...
- 【Luogu】P3381最小费用最大流模板(SPFA找增广路)
题目链接 哈 学会最小费用最大流啦 思路是这样. 首先我们有一个贪心策略.如果我们每次找到单位费用和最短的一条增广路,那么显然我们可以把这条路添加到已有的流量里去——不管这条路的流量是多大,反正它能 ...
- 图论算法-最小费用最大流模板【EK;Dinic】
图论算法-最小费用最大流模板[EK;Dinic] EK模板 const int inf=1000000000; int n,m,s,t; struct node{int v,w,c;}; vector ...
- HDU3376 最小费用最大流 模板2
Matrix Again Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)To ...
- 最大流 && 最小费用最大流模板
模板从 这里 搬运,链接博客还有很多网络流题集题解参考. 最大流模板 ( 可处理重边 ) ; const int INF = 0x3f3f3f3f; struct Edge { int from ...
- Doctor NiGONiGO’s multi-core CPU(最小费用最大流模板)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=693 题意:有一个 k 核的处理器和 n 个工作,全部的工作都须要在一个核上处理一个单位的 ...
- 【网络流#2】hdu 1533 - 最小费用最大流模板题
最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...
- poj 2195 最小费用最大流模板
/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accept ...
随机推荐
- rect dict tect 词根助记
rect: r (跑)e(E 槽子)ct(不停的跑) 就是直的 dict: d(椅子)i(人)C(开口说)t(T 桌子) : 椅子前站人 开口说前面是桌子 tect: tt(TT像盖子)EC(E ...
- 017_STM32程序移植之_AS608指纹模块
STM32程序移植之AS608指纹模块 BUG说明: 硬件接线图如图所示 STM32引脚 指纹模块引脚 功能 3.3V 3.3V PA3 Tx PA2 Rx GND GND PA1 WAK 3.3V ...
- mysql远程服务密码修改
GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root"; FLUSH PRIVILEGE ...
- SQL Server Report Server
1.SQL Server Report Server是利用mircosoft的share point产品 在menu 打开Reporting Services Configuration进行配置,会自 ...
- 浏览器console中加入jquery,测试选择元素
一.chrome浏览器F12打开调试界面,在console中输入(firefox同样可以): var jquery = document.createElement('script'); jquery ...
- [HNOI2009]最小圈 分数规划 spfa判负环
[HNOI2009]最小圈 分数规划 spfa判负环 题面 思路难,代码简单. 题目求圈上最小平均值,问题可看为一个0/1规划问题,每个边有\(a[i],b[i]\)两个属性,\(a[i]=w(u,v ...
- 数据结构实验之栈与队列四:括号匹配(SDUT 2134)
#include <bits/stdc++.h> using namespace std; typedef long long ll; char s[100]; char a[100]; ...
- centos6中安装VMware Tools
使用的是centos6.8,其他6版本方法大致相同. 1 .工具/原料1)安装过虚拟机软件的计算机2)linux操作系统 3)虚拟机配置VMware tools文件, 点击工具栏上的[虚拟机],然后选 ...
- Java高并发下的 “单例模式”
前言:单例模式大家应该很熟悉了,我在这里就自己总结一下自己这段时间学到的单例相关的知识. 单例模式的目的:保证一个类只有单一的实例,也就是说你无法通过new来创建这个类的一个新实例. 单例模式的意义: ...
- java课后实验性问题7
1.异常处理 import javax.swing.*; class AboutException { public static void main(String[] a) { int i = 1, ...