Java实现最小费用最大流问题
1 问题描述
在最大流有多组解时,给每条边在附上一个单位费用的量,问在满足最大流时的最小费用是多少?
2 解决方案
下面代码所使用的测试数据如下图:

package com.liuzhen.practice;
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static int MAX = 1000;
public static int n; //图中顶点数目
public static boolean[] used = new boolean[MAX]; //判断顶点是否在队列中
public static int[] pre = new int[MAX]; //记录最短增广路径中相应节点的前节点
public static int[] distance = new int[MAX]; //记录源点到图中其他所有顶点的最短距离
public static int[] capacity = new int[MAX]; //用于记录遍历图每一次得到增广路径的流量
public static ArrayList<edge>[] map; //图的邻接表
//表示图中边信息内部类
static class edge {
public int from; //边的起点
public int to; //边的终点
public int cap; //边的容量
public int cost; //边的费用
public edge(int from, int to, int cap, int cost) {
this.from = from;
this.to = to;
this.cap = cap;
this.cost = cost;
}
}
//输入给定图数据
@SuppressWarnings("unchecked")
public void init() {
Scanner in = new Scanner(System.in);
n = in.nextInt();
int k = in.nextInt(); //给定图的边数目
map = new ArrayList[n];
for(int i = 0;i < n;i++)
map[i] = new ArrayList<edge>();
for(int i = 0;i < k;i++) {
int from = in.nextInt();
int to = in.nextInt();
int cap = in.nextInt();
int cost = in.nextInt();
map[from].add(new edge(from, to, cap, cost)); //正向边
map[to].add(new edge(to, from, 0, -cost)); //反向边
}
}
//寻找顶点start到顶点end的最短路径(PS:即费用最少的一条增广路径)
public boolean spfa(int start, int end) {
int[] count = new int[n];
for(int i = 0;i < n;i++) {
used[i] = false;
pre[i] = -1;
distance[i] = Integer.MAX_VALUE;
capacity[i] = Integer.MAX_VALUE;
}
used[start] = true;
pre[start] = start;
distance[start] = 0;
count[start]++;
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(start);
while(!list.isEmpty()) {
int index = list.get(0);
list.remove(0);
used[index] = false;
for(int i = 0;i < map[index].size();i++) {
edge temp = map[index].get(i);
if(temp.cap > 0 && distance[temp.to] > distance[index] + temp.cost) {
//记录顶点start到图中其它顶点之间的最短费用距离
distance[temp.to] = distance[index] + temp.cost;
pre[temp.to] = index;
//记录增广路径能够流通的最大流量
capacity[temp.to] = Math.min(capacity[index], temp.cap);
if(!used[temp.to]) {
used[temp.to] = true;
list.add(temp.to);
count[temp.to]++;
if(count[temp.to] > n) //用于判断图中是否有负环
return false;
}
}
}
}
if(distance[end] != Integer.MAX_VALUE && pre[end] != -1)
return true;
return false;
}
public int getResult() {
init(); //输入给定图数据
int minCost = 0;
int start = 0; //把源点设置为顶点0
int end = n - 1; //把汇点设置为顶点n - 1
while(true) {
if(spfa(start, end) == false)
break;
System.out.println("增广路径增量:"+capacity[end]+", 费用流:"+distance[end]);
minCost += distance[end] * capacity[end];
int last = end;
int begin = end;
System.out.print("汇点出发");
while(begin != start) {
last = begin;
begin = pre[last];
int i = 0, j = 0;
System.out.print("——>"+last);
for(;i < map[begin].size();i++) {
if(map[begin].get(i).to == last)
break;
}
map[begin].get(i).cap -= capacity[end]; //正向边剩余流量减少
for(;j < map[last].size();j++) {
if(map[last].get(j).to == begin)
break;
}
map[last].get(j).cap += capacity[end]; //反向边剩余流量增加
}
System.out.println("——>"+begin);
}
return minCost;
}
public static void main(String[] args) {
Main test = new Main();
int result = test.getResult();
System.out.println(result);
}
}
运行结果:
7
1 2 1
3 3 2
2 5 5
4 3 4
5 2 10
2 1 3
5 4 7
增广路径增量:2, 费用流:12
汇点出发——>5——>4——>1——>0
增广路径增量:1, 费用流:15
汇点出发——>5——>2——>3——>0
Java实现最小费用最大流问题的更多相关文章
- 算法笔记_140:最小费用最大流问题(Java)
目录 1 问题描述 2 解决方案 1 问题描述 在最大流有多组解时,给每条边在附上一个单位费用的量,问在满足最大流时的最小费用是多少? 2 解决方案 下面代码所使用的测试数据如下图: 具体代码如下 ...
- poj3422 Kaka's Matrix Travels(最小费用最大流问题)
/* poj3422 Kaka's Matrix Travels 不知道 k次 dp做为什么不对??? 看了大牛的代码,才知道还可以这样做! 开始没有理解将a 和 a‘ 之间建立怎样的两条边,导致程序 ...
- hdu 2686最小费用最大流问题
#include<stdio.h> #include<queue> #include<string.h> using namespace std; #define ...
- ACM/ICPC 之 卡卡的矩阵旅行-最小费用最大流(可做模板)(POJ3422)
将每个点拆分成原点A与伪点B,A->B有两条单向路(邻接表实现时需要建立一条反向的空边,并保证环路费用和为0),一条残留容量为1,费用为本身的负值(便于计算最短路),另一条残留容量+∞,费用为0 ...
- Going Home(最小费用最大流)
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16200 Accepted: 8283 Description On a ...
- HIT 2715 - Matrix3 - [最小费用最大流][数组模拟邻接表MCMF模板]
题目链接:http://acm.hit.edu.cn/hoj/problem/view?id=2715 Time limit : 5 sec Memory limit : 64 M Zhouguyue ...
- POJ-2159 最小费用最大流
Going Home 自己写的第一道费用流,图建好一波板子AC.不过还是有几个地方有点迷. 先来 ...
- POJ2516 Minimum Cost —— 最小费用最大流
题目链接:https://vjudge.net/problem/POJ-2516 Minimum Cost Time Limit: 4000MS Memory Limit: 65536K Tota ...
- poj 2135最小费用最大流
最小费用最大流问题是经济学和管理学中的一类典型问题.在一个网络中每段路径都有"容量"和"费用"两个限制的条件下,此类问题的研究试图寻找出:流量从A到B,如何选择 ...
随机推荐
- 【蓝桥杯C组】备赛基础篇之高精度算法
一.高精度加法 思路: 运用vector数组(c选手可用len来记录数组长度,数组去保存数字)将存入字符串里面的数字符倒叙保存,按照小学的加法列式,相加保存进位即可.具体参考代码. 详细代码解析: # ...
- sql 取值时将字符串类型数字转化成整形
select cast(a.Vchcode as int) as avchcode,a.ptypeid,a.assqty,unit,b.pfullname,b.standard,b.type from ...
- 06.drf(django)的权限
默认配置已经启用权限控制 settings 'django.contrib.auth', 默认 migrate 会给每个模型赋予4个权限,如果 ORM 类不托管给django管理,而是直接在数据库中建 ...
- django 中CBV和FBV 路由写法的区别
使用视图函数时, FBV: django完成URL解析之后,会直接把request对象以及URL解析器捕获的参数(比如re_path中正则表达捕获的位置参数或关键字参数)丢给视图函数 CBV: 这些参 ...
- 王艳 201771010127《面向对象程序设计(java)》第十四周学习总结
实验十四 Swing图形界面组件 一理论部分 1.Layout Manager(布局管理器):布局管理器是一组类,实现 java.awt.LayoutManager 接口,决定容器中组件的位置和大小 ...
- Linux 下批量杀死进程
ps aux|grep python|grep -v grep|cut -c 9-15|xargs kill -15 管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入.下面 ...
- 初探Redis-基础类型List
Redis存在五种基础类型:字符串(String).列表(List).哈希(Hash).集合(Set).有序集合(Sorted Set).本次列举出List的常用操作. Redis官网:https:/ ...
- pytest常用命令参数
pytest 参数 1.参数:-s 运行过程中执行print打印函数:pytest -s,以下两个输出 上边带参数,下边不带 2.参数: --collect-only 收集将要执行的用例,但不会执行用 ...
- OpenStack的Trove组件详解
一:简介 一.背景 1. 对于公有云计算平台来说,只有计算.网络与存储这三大服务往往是不太够的,在目前互联网应用百花齐放的背景下,几乎所有应用都使用到数据库,而数据库承载的往往是应用最核心的数 ...
- Chisel3 - util - Lookup
https://mp.weixin.qq.com/s/g85Si6n37D9PYfR5hEoRQQ 实现一个查找逻辑. 参考链接: https://github.com/freechips ...