最小费用最大流 模板
来源:互联网 发布:最小割算法 编辑:程序博客网 时间:2024/06/05 09:40
#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#define maxn 1010#define maxm 1010101#define Inf 2147483647using namespace std;struct Edge{int u, v, cap, flow, cost;int next;}e[maxm];int n, m, s, t;int flow = 0, cost = 0;int fi[maxn], ecnt;int inq[maxn], p[maxn];//p:上一条边 int d[maxn];//spfa int a[maxn];//可改进量 void add_edge(int u, int v, int cap, int cost) {ecnt++;e[ecnt] = (Edge){u, v, cap, 0, cost, fi[u]};fi[u] = ecnt;}bool spfa(int s, int t) {for(int i = 1; i <= n; i++)d[i] = Inf;memset(inq, 0, sizeof(inq));d[s] = 0, inq[s] = 1, p[s] = 0, a[s] = Inf;int q[maxn], head = 0, tail = 1;q[head] = s;while(head < tail) {int u = q[head++];inq[u] = 0;for(int i = fi[u]; i != -1; i = e[i].next)if(e[i].cap > e[i].flow && d[e[i].v] > d[u] + e[i].cost) {d[e[i].v] = d[u] + e[i].cost;p[e[i].v] = i;a[e[i].v] = min(a[u], e[i].cap - e[i].flow);if(!inq[e[i].v]) {q[tail++] = e[i].v, inq[e[i].v] = 1;}}}if(d[t] == Inf) return false;flow += a[t], cost += d[t]*a[t];int u = t;while(u != s) {e[p[u]].flow += a[t];e[p[u]^1].flow -= a[t];u = e[p[u]].u;}return true;}int main() {memset(fi, -1, sizeof(fi));scanf("%d%d", &n, &m);s = 1, t = n;for(int i = 1; i <= m; i++) {int u, v, cap, cost;scanf("%d%d%d%d", &u, &v, &cap, &cost);add_edge(u, v, cap, cost);add_edge(v, u, 0, -cost);}while(spfa(s, t));printf("%d", cost);return 0;}果然还是自己写的模板看起来舒服==
0 0
- 模板[最小费用最大流]
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流,模板
- 最小费用最大流模板
- 最小费用最大流 模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- 最小费用最大流模板
- oracle中number类型 默认长度是多少
- 读取与解析XML数据(SAX、DOM、PULL)
- bat脚本_WINDOWS命令和批处理
- ES6和ES5中的遍历
- PHP 学习笔记(三):高级教程:多维数组,日期,文件打开/读取,创建/写入,上传
- 最小费用最大流 模板
- 远程主机强迫关闭了一个现有的连接
- 机房收费之——MSHflexgrid控件
- C++随笔--输入多行数据
- python网络编程 - 简单的时间获取服务器
- Bootstrap(二)
- 工具链解析
- 音乐扫描
- c语言中结构体存储空间确定