HDU 3957 Street Fighter

Problem Description
Street Fighter is a popular Fighting game. In the game, you could choose many roles to fight. 
Each role has some models, in different model the role has different super skill. Such as Ryu, he has two model -- "Metsu Hadoken" and "Metsu Shoryuken". In "Metsu Hadoken" model, Ryu could beat Chun-Li easyly and in "Metsu Shoryuken" he could beat Ken.
HDU 3957 Street Fighter_第1张图片
Giving the information of which role in which model could beat which role in which model. Your task is choosing minimum roles in certain model to beat other roles in any model.(each role could only be chosen once)
 

Input
The first line is a number T(1<=T<=30), represents the number of case. The next T blocks follow each indicates a case.
The first line of each case contains a integers N(2<=N<=25), indication the number of roles. (roles numbered from 0 to N - 1).
Then N blocks follow, each block contain the information of a role.
The first of each block contains a integer M(1<=M<=2), indication the number of model of this role.(models numbered from 0 to M - 1)
Then M lines follow, each line contain a number K(1<=K<=10), then K pairs integers(role id and model id) follow, indicating the current role in this model could beat that role in such model.
 

Output
For each case, output the number of case and the minimum roles have to choose to reach the goal.(as shown in the sample output)
 

Sample Input
   
   
   
   
2 2 2 1 1 0 1 1 1 2 1 0 0 1 0 1 4 2 2 1 0 1 1 1 2 0 2 2 3 0 0 1 1 2 0 2 2 0 0 0 1 1 1 0 2 2 2 0 2 1 1 1 0
 

Sample Output
   
   
   
   
Case 1: 2

Case 2: 2

直接建图跑个dlx重复覆盖,过程中注意一个人物只能选择一次

#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn = 105;
int T, n, m, x, y, t, tot, f[maxn][2], tt = 0;
struct people
{
	int x, y;
	people(){}
	people(int x, int y) :x(x), y(y){}
	bool operator <(const people &a)
	{
		return f[x][y] < f[a.x][a.y];
	}
};
vector<people> p[maxn][2];

inline void read(int &ret)
{
	char c;
	do {
		c = getchar();
	} while (c < '0' || c > '9');
	ret = c - '0';
	while ((c = getchar()) >= '0' && c <= '9')
		ret = ret * 10 + (c - '0');
}

struct DLX
{
#define maxn 500005
#define F(i,A,s) for (int i=A[s];i!=s;i=A[i])
	int L[maxn], R[maxn], U[maxn], D[maxn];
	int row[maxn], col[maxn], ans[maxn], cnt[maxn];
	int n, m, num, sz;

	void add(int now, int l, int r, int u, int d, int x, int y)
	{
		L[now] = l;	R[now] = r;	U[now] = u;
		D[now] = d;   row[now] = x;  col[now] = y;
	}
	void reset(int n, int m)
	{
		num = 0x7FFFFFFF;
		this->n = n;	this->m = m;
		for (int i = 0; i <= m; i++)
		{
			add(i, i - 1, i + 1, i, i, 0, i);
			cnt[i] = 0;
		}
		L[0] = m; 	R[m] = 0; 	sz = m + 1;
	}
	void insert(int x, int y)
	{
		int ft = sz - 1;
		if (row[ft] != x)
		{
			add(sz, sz, sz, U[y], y, x, y);
			U[D[sz]] = sz; D[U[sz]] = sz;
		}
		else
		{
			add(sz, ft, R[ft], U[y], y, x, y);
			R[L[sz]] = sz; L[R[sz]] = sz;
			U[D[sz]] = sz; D[U[sz]] = sz;
		}
		++cnt[y];	++sz;
	}

	//精确覆盖
	void remove(int now)
	{
		R[L[now]] = R[now];
		L[R[now]] = L[now];
		F(i, D, now) F(j, R, i)
		{
			D[U[j]] = D[j];
			U[D[j]] = U[j];
			--cnt[col[j]];
		}
	}
	void resume(int now)
	{
		F(i, U, now)	F(j, L, i)
		{
			D[U[j]] = j;
			U[D[j]] = j;
			++cnt[col[j]];
		}
		R[L[now]] = now;
		L[R[now]] = now;
	}
	bool dfs(int x)
	{
		//if (x + A() >= num) return;
		if (!R[0]) { num = min(num, x); return true; }
		int now = R[0];
		F(i, R, 0) if (cnt[now]>cnt[i]) now = i;
		remove(now);
		F(i, D, now)
		{
			ans[x] = row[i];
			F(j, R, i) remove(col[j]);
			if (dfs(x + 1)) return true;
			F(j, L, i) resume(col[j]);
		}
		resume(now);
		return false;
	}
	//精确覆盖

	//重复覆盖
	void Remove(int now)
	{
		F(i, D, now)
		{
			L[R[i]] = L[i];
			R[L[i]] = R[i];
		}
	}
	void Resume(int now)
	{
		F(i, U, now) L[R[i]] = R[L[i]] = i;
	}
	int vis[maxn];
	int flag[maxn];
	int A()
	{
		int dis = 0;
		F(i, R, 0) vis[i] = 0;
		F(i, R, 0) if (!vis[i])
		{
			dis++;	vis[i] = 1;
			F(j, D, i) F(k, R, j) vis[col[k]] = 1;
		}
		return dis;
	}
	void Dfs(int x)
	{
		if (!R[0]) num = min(num, x);
		else if (x + A()<num)
		{
			int now = R[0];
			F(i, R, 0) if (cnt[now]>cnt[i]) now = i;
			F(i, D, now)
			if (!flag[row[i] ^ 1])
			{
				flag[row[i]] = 1;
				Remove(i); F(j, R, i) Remove(j);
				Dfs(x + 1);
				F(j, L, i) Resume(j); Resume(i);
				flag[row[i]] = 0;
			}
		}
	}
	//重复覆盖
}dlx;

int main()
{
	read(T);
	while (T--)
	{
		scanf("%d", &n);
		memset(f, 0, sizeof(f));
		for (int i = 1; i <= n; i++) 
			for (int j = 0; j < 2; j++) p[i][j].clear();
		tot = 0;
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &m);
			for (int j = 0; j < m; j++)
			{
				f[i][j] = ++tot;
				scanf("%d", &t);
				while (t--)
				{
					scanf("%d%d", &x, &y);
					p[i][j].push_back(people(x + 1, y));
				}
				for (int k = 0; k < m; k++) p[i][j].push_back(people(i, k));
			}
		}
		dlx.reset(n, tot);
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				sort(p[i][j].begin(), p[i][j].end());
				for (int k = 0; k < p[i][j].size(); k++)
				{
					x = p[i][j][k].x;
					y = p[i][j][k].y;
					dlx.insert(i + i + j, f[x][y]);
				}
			}
		}
		memset(dlx.flag, 0, sizeof(dlx.flag));
		dlx.Dfs(0);
		printf("Case %d: %d\n", ++tt, dlx.num);
	}
	return 0;
}


你可能感兴趣的:(HDU)