当前位置: 首页 > news >正文

P6577 【模板】二分图最大权完美匹配

P6577 【模板】二分图最大权完美匹配

大意

二分图最大权匹配。

思路

首先,我们先不考虑最大权的问题,我们先考虑如何用网络流解决二分图最大匹配问题,首先,我们考虑这样的事,我们如果想让最小割等于最大匹配,实际上最小割很形象的解决了这个问题,最小割的过程,就是把一张图,割为 \(S\)\(T\) 两部分,那么我们只需要在边 \(u\)\(v\) 间连一条长度为 \(1\) 的边,那么割掉的花费就是 \(1\)

由于每个点都可以做匹配,只需要把超级原点 \(S\) 连向二分图左边的点即可,二分图右边的点连向 \(T\),容量都为 \(1\),这样求出来的最小割就是使得图二分开的最大匹配方案。

那么如果带权呢?那么我们只需要把花费也建出来即可,这样就完成了此题。

但是由于题目只支持 KM 做法,因此此做法仅作为拓展。

代码

//费用流

include

include <string.h>

include

using namespace std;

const int MAX_N = 1000;
const int MAX_M = 10000;
const int inf = 0x3f3f3f3f;
struct edge {
int v, c, w, next;
} e[MAX_M];
int p[MAX_N], s, t, eid;
void init() {
memset(p, -1, sizeof(p));
eid = 0;
}
void insert(int u, int v, int c, int w) {
e[eid].v = v;
e[eid].c = c;
e[eid].w = w;
e[eid].next = p[u];
p[u] = eid++;
}
void addedge(int u, int v, int c, int w) {
insert(u, v, c, w);
insert(v, u, 0, -w);
}
bool vis[MAX_N];
int d[MAX_N];
int pre[MAX_N];
bool spfa() {
memset(vis, 0, sizeof(vis));
memset(d, 0x3f, sizeof(d));
memset(pre, -1, sizeof(pre));
d[s] = 0;
vis[s] = true;
queue q;
q.push(s);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int i = p[u]; i != -1; i = e[i].next) {
if (e[i].c) {
int v = e[i].v;
if (d[u] + e[i].w < d[v]) {
d[v] = d[u] + e[i].w;
pre[v] = i;
if (!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
}
}
return pre[t] != -1;
}
int costflow() {
int ret = 0;
while(spfa()) {
int flow = inf;
for(int i = t; i != s; i = e[pre[i]^1].v) {
flow = min(e[pre[i]].c, flow);
}
for(int i = t; i != s; i = e[pre[i]^1].v) {
e[pre[i]].c -= flow;
e[pre[i]^1].c += flow;
ret += e[pre[i]].w * flow;
}
}
return ret;
}
int main() {
int n, m;
init();
cin >> n >> m;
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
addedge(u, v, 1, -w);
}
s = 0; t = 2 * n + 1;
for(int i = 1;i <= n;i ++){
addedge(s, i, 1, 0);
addedge(n + i, t, 1, 0);
}
cout << -costflow() << endl;
return 0;
}
//KM

include

include

include <string.h>

using namespace std;

typedef long long ll;
const int N = 505;
const ll INF = 1e18;

int n, m;
ll w[N][N], lx[N], ly[N], slack[N];
int rmatch[N], pre[N];
bool rvis[N];

void bfs(int u) {
int x, y = 0, nexty;
ll dt;
memset(pre, 0, sizeof(pre));
for (int i = 1; i <= n; i++) slack[i] = INF;
rmatch[0] = u;

do {rvis[y] = true;x = rmatch[y];dt = INF;for (int i = 1; i <= n; i++) {if (!rvis[i]) {if (slack[i] > lx[x] + ly[i] - w[x][i]) {slack[i] = lx[x] + ly[i] - w[x][i];pre[i] = y;}if (slack[i] < dt) {dt = slack[i];nexty = i;}}}for (int i = 0; i <= n; i++) {if (rvis[i]) {lx[rmatch[i]] -= dt;ly[i] += dt;} else {slack[i] -= dt;}}y = nexty;
} while (rmatch[y] != 0);while (y) {rmatch[y] = rmatch[pre[y]];y = pre[y];
}

}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);

cin >> n >> m;
for (int i = 1; i <= n; i++) {for (int j = 1; j <= n; j++) {w[i][j] = -INF;}
}for (int i = 0; i < m; i++) {int u, v; ll c;cin >> u >> v >> c;w[u][v] = max(w[u][v], c);
}for (int i = 1; i <= n; i++) {memset(rvis, 0, sizeof(rvis));bfs(i);
}ll res = 0;
for (int i = 1; i <= n; i++) res += lx[i] + ly[i];cout << res << "\n";
for (int i = 1; i <= n; i++) {cout << rmatch[i] << (i == n ? "" : " ");
}return 0;

}


http://www.jsqmd.com/news/379490/

相关文章:

  • 详细介绍:Maven 编译的settings配置和pom、idea配置关系
  • 【毕业设计】基于SpringBoot生活版青年学习平台(源码+文档+远程调试,全bao定制等)
  • 3D感知技术与实践(2020年)-04:深度图和点云数据底层处理算法
  • 【毕业设计】基于web的高校一卡通管理系统的设计与实现(源码+文档+远程调试,全bao定制等)
  • 基于Python的Qt研发之Pyside6 QtSerialPort库的运用
  • 【计算机毕业设计案例】springboot基于WIFI协议的课堂点名系统的设计与实现(程序+文档+讲解+定制)
  • 提示工程架构师如何用提示设计打造极致用户体验?
  • 实用指南:Python测试开发工具库:日志脱敏工具(敏感信息自动屏蔽)
  • 2026原创:演唱会门票在线订票系统界面(可定制)
  • ODT
  • 大模型缓存命中
  • 永无乡
  • 2026广东最新紫晶洞厂家top5推荐!广州等地优天然水晶源头供应商权威榜单,品类全货源稳,助力客商高效采购 - 品牌推荐2026
  • 信息系统仿真:信息系统基础理论_(10).仿真结果的验证与校验
  • 假期作业
  • 1950-2024年中国与各大国之间的关系数据
  • P5521 梅深不见冬
  • 2010.1-2026.1中国城市二手房房价历史数据
  • 2026广东最新结婚五金/黄金厂商首选推荐水贝黄金广州总店:广州优选,这家品牌授权店以高性价比与专业服务脱颖而出 - 品牌推荐2026
  • MySQL慢查询优化:定位、分析与优化实战
  • P9446 [ICPC 2021 WF] Prehistoric Programs
  • 别再注册Gmail了!谷歌邮箱这个隐藏功能,让你一个账号当1000个小号用(附保姆级小白教程)
  • 细胞群体动力学仿真软件:CompuCell3D_(6).模拟参数配置与优化
  • Markdown 转 Word 和 PDF:Python 简单实现指南
  • 细胞群体动力学仿真软件:CompuCell3D_(7).细胞间相互作用模型
  • P3381 【模板】最小费用最大流
  • 细胞群体动力学仿真软件:CompuCell3D_(2).细胞建模基础理论
  • P14254 分割(divide)
  • P9358 [ICPC 2022 Xian R] Bridge
  • 2026年可查实盘配资平台推荐:合规透明安全 - 资讯焦点