HDU-3718 Similarity

题目只有26个字母,所以我们新建一个二分图,v[i][j]表示字母i对应字母j时能成功匹配的个数,给这个边矩阵v求个最大匹配就是答案。

#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cctype>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define N 12345
#define MAX 1<<30
#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 readch()
{
	char ch=getchar();
	while (!isupper(ch)) ch=getchar();
	return ch-‘A‘+1;
}

int n, m, l[27], st[27], lx[27], ly[27], v[27][27], k[N];
bool vx[27], vy[27];

bool Find(int x)
{
	vx[x]=1;
	rep(y, 1, 26)
	{
		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(ly, 0); clr(l, 0); rep(i, 1, 26) lx[i]=-MAX;
	rep(i, 1, 26) rep(j, 1, 26) if (lx[i]<v[i][j]) lx[i]=v[i][j];
	rep(i, 1, 26)
	{
		rep(j, 1, 26) st[j]=MAX;
		while (1)
		{
			clr(vx, 0); clr(vy, 0);
			if (Find(i)) break; int a=MAX;
			rep(j, 1, 26) if (!vy[j] && st[j]<a) a=st[j];
			rep(j, 1, 26) if (vx[j]) lx[j]-=a;
			rep(j, 1, 26) if (vy[j]) ly[j]+=a; else st[j]-=a;
		}
	}
	int a=0; rep(i, 1, 26) a+=lx[i]+ly[i];
	return a;
}

int main()
{
	int t=read(); while (t--)
	{
		n=read(); m=read(); m=read();
		rep(i, 1, n) k[i]=readch();
		rep(o, 1, m)
		{
			rep(i, 1, 26) rep(j, 1, 26) v[i][j]=0;
			rep(i, 1, n) v[k[i]][readch()]++;
			printf("%.4lf\n", double(km())/n);
		}
	}
	return 0;
}

  

时间: 2024-08-04 04:30:26

HDU-3718 Similarity的相关文章

HDU 3718 Similarity(KM最大匹配)

HDU 3718 Similarity 题目链接 题意:给定一个标准答案字符串,然后下面每一行给一个串,要求把字符一种对应一种,要求匹配尽量多 思路:显然的KM最大匹配问题,位置对应的字符连边权值+1 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int MAXNODE = 27; typede

hdu 3718 Different Division

Different Division Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 234    Accepted Submission(s): 90 Problem Description Now we will give you a graph, there are many points in the graph. We wil

KM HDU 3718

1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 7 #define MAXN 50 8 #define inf 65553566 9 int love[MAXN][MAXN]; /// 记录每个妹子和每个男生的好感度 10 int ex_girl[MAXN]; /// 每个妹子的期望值 11

【图论】二分图匹配总结

二分图匹配总结 二分图匹配 1.二分图最大匹配.求两个集合内,每一个元素仅仅能用一次.两集合间存在一些匹配关系,求最大匹配多少对,利用匈牙利算法,对于每一个结点不断去找增广路去匹配 有几个重要性质: 1.最小点覆盖 = 最大匹配 2.最大独立集 = 总结点 - 最大匹配 模板: bool dfs(int u) { for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (vis[v]) continue; vis[v] = 1; i

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

hdu图论题目分类

=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many Tables 基础并查集★ 1272 小希的迷宫 基础并查集★ 1325&&poj1308 Is It A Tree? 基础并查集★ 1856 More is better 基础并查集★ 1102 Constructing Roads 基础最小生成树★ 1232 畅通工程 基础并查集★ 123

hdu KM匹配题集

[HDU]2255 奔小康赚大钱 模板题★1533 Going Home 模板题★2426 Interesting Housing Problem KM★3395 Special Fish KM★2282 Chocolate KM★2813 One fihgt one KM★1853 Cyclic Tour 最小费用圈覆盖★★3488 Tour 最小费用圈覆盖★★3435 A new Graph Game 最小费用圈覆盖★★3722 Card Game 最小费用圈覆盖★★3718 Similar

题单二:图论500

http://wenku.baidu.com/link?url=gETLFsWcgddEDRZ334EJOS7qCTab94qw5cor8Es0LINVaGMSgc9nIV-utRIDh--2UwRLvsvJ5tXFjbdpzbjygEdpGehim1i5BfzYgYWxJmu ==========  以下是最小生成树+并查集=========================[HDU]1213         How Many Tables        基础并查集★1272         小

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

图论精炼500题

忘了从哪转的了... =============================以下是最小生成树+并查集====================================== [HDU] 1213               How Many Tables                    基础并查集★ 1272               小希的迷宫                     基础并查集★ 1325&&poj1308    Is It A Tree?