lightoj 1198 - Karate Competition

大意就是两个俱乐部对垒,每个俱乐部出战n个人,都有一个战斗值,1*1的对决,战斗值高的一方总是获胜。

赢的得2分,平局1分,输了不得分。。。

现在问最多你的俱乐部可以得多少分。。。。

显然的最大权匹配,,,先出来出来两两之间对垒的得分情况,在就是裸的km了。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
/**********************Point*****************************/
struct Point{
	double x,y;
	Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector A,Vector B){
	return Vector(A.x + B.x,A.y + B.y);
}
Vector operator - (Vector A,Vector B){//向量减法
	return Vector(A.x - B.x,A.y - B.y);
}
Vector operator * (Vector A,double p){//向量数乘
	return Vector(A.x * p,A.y * p);
}
Vector operator / (Vector A,double p){//向量除实数
	return Vector(A.x / p,A.y / p);
}
int dcmp(double x){//精度正负、0的判断
	if (fabs(x) < eps) return 0;
	return x < 0?-1:1;
}
bool operator < (const Point& A,const Point& B){//小于符号的重载
	return A.x < B.x || (A.x == B.x && A.y < B.y);
}
bool operator == (const Point& A,const Point& B){//点重的判断
	return dcmp(A.x - B.x) == 0&& dcmp(A.y - B.y) == 0;
}
double Dot(Vector A,Vector B){//向量的点乘
	return A.x * B.x + A.y * B.y;
}
double Length(Vector A){//向量的模
	return sqrt(Dot(A,A));
}
double Angle(Vector A,Vector B){//向量的夹角
	return acos(Dot(A,B) / Length(A) / Length(B));
}
double Cross(Vector A,Vector B){//向量的叉积
	return A.x * B.y - A.y * B.x;
}
double Area2(Point A,Point B,Point C){//三角形面积
	return Cross(B - A,C - A);
}
Vector Rotate(Vector A,double rad){//向量的旋转
	return Vector(A.x * cos(rad) - A.y * sin(rad),A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A){//法向量
	int L = Length(A);
	return Vector(-A.y / L,A.x / L);
}
double DistanceToLine(Point p,Point A,Point B){//p到直线AB的距离
	Vector v1 = B - A,v2 = p - A;
	return fabs(Cross(v1,v2)) / Length(v1);
}
double DistanceToSegment(Point p,Point A,Point B){//p到线段AB的距离
	if (A == B) return Length(p - A);
	Vector v1 = B - A, v2 = p - A,v3 = p - B;
	if (dcmp(Dot(v1,v2) < 0)) return Length(v2);
	else if (dcmp(Dot(v1,v3)) > 0) return Length(v3);
	else return DistanceToLine(p,A,B);
}
bool SegmentProperIntersection(Point A1,Point A2,Point B1,Point B2){//线段相交
	double c1 = Cross(A2 - A1,B1 - A1),c2 = Cross(A2 - A1,B2 - A1);
	double c3 = Cross(B2 - B1,A1 - B1),c4 = Cross(B2 - B1,A2 - B1);
	return dcmp(c1)*dcmp(c2) < 0 && dcmp(c3)*dcmp(c4) < 0;
}
const int N = 77;
int A[N], B[N];
int n;

struct KM {
	int n, m;
	int g[N][N];
	int Lx[N], Ly[N], slack[N];
	int right[N], left[N];
	bool S[N], T[N];

	void Initation(int n,int m) {
		this->n = n,this->m = m;
		memset(g, 0,sizeof g);
	}

	void Addedge(int u,int v,int w) {
		g[u][v] += w;
	}

	void updata() {
		int a = INF;
		for (int i = 1;i <= m;++i)
			if (!T[i]) a = min(a, slack[i]);
		for (int i = 1;i <= n;++i)
			if (S[i]) Lx[i] -= a;
		for (int i = 1;i <= m;++i)
			if (T[i]) Ly[i] += a;
	}

	bool Search(int u) {
		S[u] = true;
		for (int i = 1;i <= m;++i) {
			if (T[i]) continue;
			int tmp = Lx[u] + Ly[i] - g[u][i];
			if (!tmp) {
				T[i] = true;
				if (left[i] == -1 || Search(left[i])) {
					left[i] = u;
					right[u] = i;
					return true;
				}
			}else slack[i] = min(tmp, slack[i]);
		}
		return false;
	}

	int km() {
		memset(right, -1,sizeof right);
		memset(left, -1,sizeof left);
		memset(Ly, 0,sizeof Ly);

		for (int i = 1;i <= n;++i) {
			Lx[i] = -INF;
			for (int j = 1;j <= n;++j)
				Lx[i] = max(Lx[i], g[i][j]);
		}

		for (int i = 1;i <= n;++i) {
			memset(slack, INF,sizeof slack);
			while (true) {
				memset(S, false,sizeof S);
				memset(T, false,sizeof T);
				if (Search(i)) break;
				updata();
			}
		}

		int ret = 0;
		for (int i = 1;i <= n;++i)
			ret += g[i][right[i]];
		return ret;
	}
}km;

inline void Input() {
	scanf("%d",&n);
	for (int i = 1;i <= n;++i)
		scanf("%d",&A[i]);
	for (int i = 1;i <= n;++i)
		scanf("%d",&B[i]);
}
inline void Initation() {
	km.Initation(n, n);

	for (int i = 1;i <= n;++i) {
		for (int j = 1;j <= n;++j) {
			if (A[i] > B[j]) km.Addedge(i, j, 2);
			else if (A[i] == B[j]) km.Addedge(i, j, 1);
			else km.Addedge(i, j, 0);
		}
	}
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	scanf("%d",&t);
	while (t--) {
		Input();
		Initation();
		printf("Case %d: %d\n", ++icase, km.km());
	}
	return 0;
}


你可能感兴趣的:(km,lightoj)