import java.util.*;
class KuhnMunkres
{
int N, M;
int[][] graph;
int[] link;
int[] labelX, labelY;
boolean[] usedX, usedY;
boolean findPath(int i)
{
usedX[i] = true;
for (int j = 0; j < M; j++)
if (!usedY[j] && labelX[i] + labelY[j] == graph[i][j])
{
usedY[j] = true;
if (link[j] == -1 || findPath(link[j]))
{
link[j] = i;
return true;
}
}
return false;
}
int maxWeightMatch()
{
link = new int[M];
labelX = new int[N];
labelY = new int[M];
usedX = new boolean[N];
usedY = new boolean[M];
Arrays.fill(link, -1);
Arrays.fill(labelX, 0);
Arrays.fill(labelY, 0);
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
labelX[i] = Math.max(labelX[i], graph[i][j]);
for (int k = 0; k < N; k++)
while (true)
{
Arrays.fill(usedX, false);
Arrays.fill(usedY, false);
if (findPath(k)) break;
int delta = Integer.MAX_VALUE;
for (int i = 0; i < N; i++)
if (usedX[i])
for (int j = 0; j < M; j++)
if (!usedY[j])
delta = Math.min(delta, labelX[i] + labelY[j] - graph[i][j]);
if (delta == 0) break;
for (int i = 0; i < N; i++)
if (usedX[i])
labelX[i] += delta;
for (int i = 0; i < M; i++)
if (usedY[i])
labelY[i] -= delta;
}
int result = 0;
for (int i = 0; i < N; i++)
result += labelX[i];
for (int i = 0; i < M; i++)
result += labelY[i];
return result;
}
void solve()
{
Scanner input = new Scanner(System.in);
while (input.hasNextInt())
{
N = input.nextInt();
M = input.nextInt();
graph = new int[N][M];
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
graph[i][j] = input.nextInt();
System.out.println(maxWeightMatch());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Amber算法库2
共55个文件
dpr:52个
pas:2个
java:1个
4星 · 超过85%的资源 需积分: 10 37 下载量 84 浏览量
2008-03-11
18:53:59
上传
评论
收藏 50KB RAR 举报
温馨提示
OI界大牛人Amber写的Pascal版本的算法库,基本上除了SBT以外常用的算法都囊括其中。
资源推荐
资源详情
资源评论
收起资源包目录
assl2.rar (55个子文件)
Graph
NetworkFlow
EdmondsKarp.dpr 4KB
SuccessiveShortestPath.dpr 4KB
EdmondsKarp2.dpr 4KB
RelabelToFront.dpr 5KB
ShortestPath
Dijkstra
Dijkstra.dpr 3KB
Dijkstra2.dpr 2B
BellmanFord
BellmanFord.dpr 2B
FloydWarshall
FloydWarshall.dpr 2B
SPFA
SPFA.dpr 2B
MST
Kruskal
Kruskal.dpr 0B
Prim
Prim.dpr 0B
Match
Hungary
Hungary.dpr 313B
HopcroftKarp
HopcroftKarp.dpr 313B
Edmonds
Edmonds.dpr 4KB
Edmonds3.dpr 5KB
Edmonds2.dpr 4KB
KuhnMunkres
KuhnMunkres.dpr 2KB
KuhnMunkres.java 2KB
Connected
ShrinkStronglyConnected.dpr 3KB
String
ExtendedKMP
DFA
AhoCorasick.dpr 3KB
SuffixTree
SuffixArray
Skew.dpr 6KB
Doubling2.dpr 4KB
Doubling.dpr 3KB
KMP
KMP.dpr 1KB
Geometry
GrahamScan
GrahamScan.dpr 2KB
Base
Geometry2DBase.dpr 3KB
Geometry3DBase.dpr 2KB
JudgeInsidePolygon
Math
Equation
GaussElimination
GaussElimination.dpr 1KB
GaussEliminationMod2.dpr 2KB
Numberic
Modular
ModularLinearEquation.dpr 1KB
ModularLinearEquationSystem.dpr 1KB
ExtendedGCD.dpr 732B
Prime
PrimeFilter.dpr 972B
RabinMiller.dpr 2KB
PrimeTest.dpr 651B
DataStructure
LeftistTree
Splay
Splay.dpr 3KB
Splay3.dpr 5KB
Splay2.dpr 4KB
Treap
Treap.dpr 3KB
Treap3.dpr 5KB
Treap2.dpr 4KB
BinarySearchTree
bst.dpr 3KB
Hash
FibonacciHeap
FibonacciHeap.pas 6KB
SkipList
PairHeap
PairHeap.pas 4KB
AATree
AATree.dpr 3KB
DisjointSet
disjoint.dpr 1KB
BinaryHeap
BinaryHeap.dpr 1KB
MappingBinaryHeap2.dpr 2KB
Base
BinarySearch
BinarySearch.dpr 2KB
Sort
ComparingSort.dpr 3KB
LinearSort.dpr 852B
SegmentSimplify
SegmentSimplify.dpr 2KB
HighPrecision
HighPrecision2.dpr 11KB
HighPrecision.dpr 5KB
RMQ
RMQST.dpr 1KB
共 55 条
- 1
资源评论
- sesamehch2012-06-18算法的先不说,至少代码风格是大师级的了,值得学习
CodeLyoko
- 粉丝: 2
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- YOLOV4-TINY权重文件
- 以下是一个使用贪心算法解决多机调度问题的基本步骤0.txt
- 基于大数据的房产估价是近年来随着技术的发展而兴起的一种新型估价方法.txt
- 企业供应链管理系统v3.rar
- 富芮坤FR8016HA蓝牙开发板使用手册+硬件PCB图+封装库+DEMO演示软件源代码.zip
- 基于YOLOv7的芯片表面缺陷检测系统
- 京东物流 数字化供应链综合研究报告2018.rar
- 基于YOLOv7的植物虫害识别&防治系统
- 2000.1-2023.8中国经济政策不确定性指数月度数据.xlsx
- Screenshot_2024-04-21-20-42-15-443_com.tencent.mm.jpg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功