poj3698(二分图最大匹配 + 拆点)

思路:把电影拆开,,,对于每部电影的去的天数呢分开,,(1...d1)(d1 + 1.....d1 + d2)....入上面这种拆点。

然后就是每周的周几可以去,就把周扩展成天:比如说1表示第一周的周一,8表示第二周的周一,如此类推,

用上下这种扩展的方式简图,,,只要最后的匹配数目等于总的天数,那么就是可以的。

点击题目链接

/*****************************************
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 <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int T,n;
int d,w;
int day[8];
int g[1010][400];
int vN,uN;
int link[410];
bool vis[410];
bool dfs(int u){
	for (int i = 1;i <= vN;i++){
		if (g[u][i] && !vis[i]){
			vis[i] = true;
			if (link[i] == -1 || dfs(link[i])){
				link[i] = u;
				return true;
			}
		}
	}
	return false;
}
int Hungary(){
	int ret = 0;
	MEM(link, -1);
	for (int i = 1;i <= vN;i++){
		MEM(vis, false);
		if (dfs(i)) ret++;
	}
	return ret;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	cin >> T;
	while(T--){
		cin >> n;
		int st = 1,ed = 0;
		int sum = 0;
		uN = vN = 0;
		MEM(g, 0);
		MEM(day, 0);
		for (int i = 1;i <= n;i++){
			for (int j = 1;j <= 7;j++){
				scanf("%d",&day[j]);
			}
			scanf("%d%d",&d,&w);
			for (int j = st;j < st + d;j++){
				for (int k = 1;k <= 7;k++){
					for (int l = 0;l < w;l++){
						g[j][k + l * 7] = day[k];
					}
				}
			}
			st += d;
			sum += d;
			vN = max(vN,7 * w);
		}
		uN = st - 1;
		int ans = Hungary();
		if (ans == sum) puts("Yes");
		else puts("No");
	}
	return 0;
}


你可能感兴趣的:(图论,二分图匹配)