最小费用最大流算法

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define INF 0x3f3f3f3f
 4 #define M(a, b) memset(a, b, sizeof(a))
 5 const int N = 1e3 + 5;
 6 struct Edge {
 7     int from, to, cap, flow, cost;
 8 };
 9
10 struct MCMF {
11     int n, m;
12     vector<Edge> edges;
13     vector<int> G[N];
14     int d[N], inq[N], p[N], a[N];
15
16     void init(int n) {
17         this->n = n;
18         for (int i = 0; i < n; ++i) G[i].clear();
19         edges.clear();
20     }
21
22     void AddEdge(int from, int to, int cap, int cost) {
23         edges.push_back((Edge){from, to, cap, 0, cost});
24         edges.push_back((Edge){to, from, 0, 0, -cost});
25         m = edges.size();
26         G[from].push_back(m-2); G[to].push_back(m-1);
27     }
28
29     bool spfa(int s, int t, int &flow, int &cost) {
30         M(inq, 0); M(d, INF);
31         d[s] = 0; inq[s] = 1;
32         queue<int> q;
33         q.push(s);
34         while (!q.empty()) {
35             int x = q.front(); q.pop();
36             inq[x] = 0;
37             for (int i = 0; i < G[x].size(); ++i) {
38                 Edge &e = edges[G[x][i]];
39                 if (d[e.to] > d[x] + e.cost && e.cap > e.flow) {
40                     d[e.to] = d[x] + e.cost;
41                     p[e.to] = G[x][i];
42                     a[e.to] = min(a[x], e.cap-e.flow);
43                     if (inq[e.to]) continue;
44                     q.push(e.to); inq[e.to] = 1;
45                 }
46             }
47         }
48         if (d[t] == INF) return false;
49         flow += a[t];
50         cost += d[t] * a[t];
51         int u = t;
52         while (u != s) {
53             edges[p[u]].flow += a[t];
54             edges[p[u]^1].flow -= a[t];
55             u = edges[p[u]].from;
56         }
57         return true;
58     }
59
60     int Mincost(int s, int t) {
61         int flow = 0, cost = 0;
62         while (spfa(s, t, flow, cost));
63         return cost;
64     }
65
66 };
时间: 2024-10-24 20:04:56

最小费用最大流算法的相关文章

【进阶——最小费用最大流】hdu 1533 Going Home (费用流)Pacific Northwest 2004

题意: 给一个n*m的矩阵,其中由k个人和k个房子,给每个人匹配一个不同的房子,要求所有人走过的曼哈顿距离之和最短. 输入: 多组输入数据. 每组输入数据第一行是两个整型n, m,表示矩阵的长和宽. 接下来输入矩阵. 输出: 输出最短距离. 题解: 标准的最小费用最大流算法,或者用KM算法.由于这里是要学习费用流,所以使用前者. 最小费用最大流,顾名思义,就是在一个网络中,不止存在流量,每单位流量还存在一个费用.由于一个网络的最大流可能不止一种,所以,求出当前网络在流量最大的情况下的最小花费.

poj 2195 最小费用最大流模板

/*Source Code Problem: 2195 User: HEU_daoguang Memory: 1172K Time: 94MS Language: G++ Result: Accepted Source Code */ #include <iostream> #include <stdio.h> #include <queue> #include <math.h> #include <string.h> using namespa

AHU-835 FJ的旅行 【最小费用最大流】

Description 每当西瓜的朋友来西瓜家看他,西瓜总是喜欢带他们逛自己的豪宅.西瓜的豪宅有N幢楼(1<=N<=1000),用1到N的整数编号.1号楼是西瓜豪宅的大门,N号楼是西瓜的储藏室.西瓜的豪宅里总共有M条道路(1<=M<=10000).每条道路连接两栋不同的楼房,道路的长度不超过35000.为了最好地炫耀他的豪宅,西瓜准备从大门出发,经过一些楼房到达储藏室,再经过一些楼房回到自己的大门.他要求他的路径越短越好,但是不能经过任意一条道路多于一次.请你计算这样的一条最短路径

poj 2516 最小费用最大流

Language: Default Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 14334   Accepted: 4908 Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (m

hdu 1533 Going Home 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need

POJ 2516 Minimum Cost (最小费用最大流)

Minimum Cost Time Limit: 4000MS   Memory Limit: 65536K       Description Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area there are N shopkeepers (marked from 1 to N) which stocks goods from him.Dearboy

hdu 2686 Matrix 最小费用最大流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n Matrix. A positive integer number is put in each area of the Matrix.Every time yifenfei should to do is that choose a detour which frome the top left

UVA 10746 Crime Wave - The Sequel【最小费用最大流】

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1687 题意:给你n个城市到m个海港的距离,求每个城市都有船只去的最短平均航行距离. 源点向城市建边 城市向海港 海港向汇点建边 容量为1,最后城市向海港的费用为距离 代码: #include<stdio.h> #include<iostream> #in

poj 2526 Minimum Cost【最小费用最大流】

题目链接:http://poj.org/problem?id=2516 题意: n个店主 m个供应商 k种货物 给你店主对k种货物的需求及供货商k种货物的囤货量及K种运输费用. 解法:k次费用流,分别求每种货物的费用.源点到供应点建边,店主到汇点建边,费用均为0,容量为1.然后供应点到店主建边,费用为矩阵,容量无穷大即可. 代码: /* POJ 2195 Going Home 邻接矩阵形式最小费用最大流 */ #include<stdio.h> #include<iostream>