首页 技术 正文
技术 2022年11月16日
0 收藏 905 点赞 4,679 浏览 1400 个字

求二分最大匹配,但还要尽量接近原匹配。。。

解决方法:对于N个顶点的二分图,每条边同时乘上一个比N稍微大的数N’,然后对于在原匹配的边就都+1。

经过这样处理,求得的答案Ans乘除N’即是原图的最大匹配,而Ans对N’取余则是现匹配中原匹配的边数。

嗯这种思想以前貌似也有接触过,“乘加除余”?

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#include <queue>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 54
#define MAX 0x3fffffff
#define ll long long
using namespace std;
int read()
{
int x=0, f=1; char ch=getchar();
while (!isdigit(ch)) { if (ch=='-') f=-1; ch=getchar(); }
while (isdigit(ch)) { x=x*10+ch-'0'; ch=getchar(); }
return x*f;
}int nx, ny, ans, now, l[N], st[N], lx[N], ly[N], v[N][N];
bool vx[N], vy[N];bool Find(int x)
{
vx[x]=1;
rep(y, 1, ny)
{
if (vy[y]) continue;
int a=lx[x]+ly[y]-v[x][y];
if (!a)
{
vy[y]=1; if (!l[y] || Find(l[y])) { l[y]=x; return 1; }
}
else st[y]=min(st[y], a);
}
return false;
}inline int km()
{
clr(l, 0); clr(ly, 0); rep(i, 1, nx) lx[i]=-MAX;
rep(i, 1, nx) rep(j, 1, ny) if (lx[i]<v[i][j]) lx[i]=v[i][j];
rep(i, 1, nx)
{
rep(j, 1, ny) st[j]=MAX;
while (1)
{
clr(vx, 0); clr(vy, 0);
if (Find(i)) break; int a=MAX;
rep(j, 1, ny) if (!vy[j] && st[j]<a) a=st[j];
if (a==MAX) return 1;
rep(j, 1, nx) if (vx[j]) lx[j]-=a;
rep(j, 1, ny) if (vy[j]) ly[j]+=a; else st[j]-=a;
}
}
int a=0; rep(i, 1, ny) if (l[i]) a+=v[l[i]][i];
return a;
}int main()
{
while (~scanf("%d%d", &nx, &ny))
{
rep(i, 1, nx) rep(j, 1, ny) v[i][j]=read()*N; now=ans=0;
rep(i, 1, nx) { int a=read(); now+=v[i][a]/N; v[i][a]++; }
int ans=km();
printf("%d %d\n", nx-ans%N, ans/N-now);
}
return 0;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905