HDU 3722 KM模板题

/****************************************************************************************************
    KM模板题,无非是看不看得出来罢了~
****************************************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
//#include <hash_map>		//Visual C++ lib
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <sstream>
#include <utility>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <limits>
#include <memory>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
using namespace std;

#define LOWBIT(x) ( (x) & ( (x) ^ ( (x) - 1 ) ) )
#define CLR(x, k) memset((x), (k), sizeof(x))
#define CPY(t, s) memcpy((t), (s), sizeof(s))
#define SC(s) static_cast<int>(s)
#define LEN(s) static_cast<int>( strlen((s)) )
#define SZ(s) static_cast<int>( (s).size() )

typedef double LF;
//typedef long long LL;		//GNU C++
//typedef unsigned long long ULL;
typedef __int64 LL;		//Visual C++ 2010
typedef unsigned __int64 ULL;

typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PDD;

//typedef hash_map<int, int>::iterator HMI;
typedef map<int, int>::iterator MI;
typedef vector<int>::iterator VI;
typedef list<int>::iterator LI;
typedef set<int>::iterator SI;

const int INF_INT = 0x3f3f3f3f;
const LL INF_LL = 0x7fffffffffffffffLL;		//15f
const double oo = 10e9;
const double eps = 10e-9;
const double PI = acos(-1.0);

const int MAXV = 204;
const int MAXL = 1004;

int test, n;
char card[MAXV][MAXL];
int lack;
int lx[MAXV], ly[MAXV], match[MAXV], maze[MAXV][MAXV];
bool visx[MAXV], visy[MAXV];

bool dfs(int x)
{
	visx[x] = true;
	for (int y = 1; y <= n; ++y)
	{
		if (visy[y])
		{
			continue ;
		}
		int t = lx[x] + ly[y] - maze[x][y];
		if (0 == t)
		{
			visy[y] = true;
			if (-1 == match[y] || dfs(match[y]))
			{
				match[y] = x;
				return true;
			}
		}
		else if (lack > t)
		{
			lack = t;
		}
	}
	return false;
}
void KM()
{
	CLR(lx, 0);
	CLR(ly, 0);
	CLR(match, -1);
	for (int i = 1; i <= n; ++i)
	{
		for (int j = 1; j <= n; ++j)
		{
			lx[i] = max(lx[i], maze[i][j]);
		}
	}
	for (int x = 1; x <= n; ++x)
	{
		while (true)
		{
			CLR(visx, false);
			CLR(visy, false);
			lack = INF_INT;
			if (dfs(x))
			{
				break ;
			}
			for (int i = 1; i <= n; ++i)
			{
				if (visx[i])
				{
					lx[i] -= lack;
				}
				if (visy[i])
				{
					ly[i] += lack;
				}
			}
		}
	}
	return ;
}
int str_cmp(char *sa, char *sb)
{
	int len[2] = {LEN(sa), LEN(sb)};
	int ptr[2] = {len[0] - 1, 0};
	int res = 0;
	while (ptr[0] >= 0 && ptr[1] < len[1] && sa[ ptr[0] ] == sb[ ptr[1] ])
	{
		--ptr[0];
		++ptr[1];
		++res;
	}
	return res;
}
void ace()
{
	while (cin >> n)
	{
		for (int i = 1; i <= n; ++i)
		{
			cin >> card[i];
		}
		for (int i = 1; i <= n; ++i)
		{
			for (int j = 1; j <= n; ++j)
			{
				maze[i][j] = str_cmp(card[i], card[j]);
			}
			maze[i][i] = 0;
		}
		KM();
		int res = 0;
		for (int i = 1; i <= n; ++i)
		{
			res += maze[ match[i] ][i];
		}
		printf("%d\n", res);
	}
	return ;
}
int main()
{
	ace();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值