HDU - 4971 A simple brute force problem. (DP)

Problem Description
There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may cost his budget. There may be dependencies between technical problems, for example, A requires B means you need to solve problem B before solving problem A. If A requires B and B requires A, it means that you should solve them at the same time. You can select which problems to be solved and how to solve them freely before finish your projects. Can you tell me the maximum profit?
 

Input
The first line of the input is a single integer T(<=100) which is the number of test cases.

Each test case contains a line with two integer n(<=20) and m(<=50) which is the number of project to select to complete and the number of technical problem.

Then a line with n integers. The i-th integer(<=1000) means the profit of complete the i-th project.

Then a line with m integers. The i-th integer(<=1000) means the cost of training to solve the i-th technical problem.

Then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need to solve for the i-th project.

After that, there are m lines with each line contains m integers. If the i-th row of the j-th column is 1, it means that you need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.
 

Output
For each test case, please output a line which is "Case #X: Y ", X means the number of the test case and Y means the the maximum profit.
 

Sample Input
  
  
4 2 3 10 10 6 6 6 2 0 1 2 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 1 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 1 0 0 0 0 0 0 0 2 3 10 10 8 10 6 1 0 1 2 0 0 0 1 0 0 0 0 0
 

Sample Output
  
  
Case #1: 2 Case #2: 4 Case #3: 4 Case #4: 6 题意:给你n个工程,m个问题,每个工程都有对应要解决的问题,每个问题还有关联问题,问你最大的收益 思路:DP的方法,首先预处理出每个工程要完成的问题,用二进制表示,然后就是dp结果,每个工程我们有做与不做的可能,记忆化搜索,当我们在面对第u个工程的时候, 如果当前的问题已经处理的状态出现过的话,我们也已经记录过了,直接返回
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <vector>
typedef __int64 ll;
using namespace std;
const int maxn = 100;

int G[maxn][maxn], pj[maxn], pb[maxn];
vector<int> solve[maxn];
int n, m;
ll sum, status, need[maxn];
map<ll, ll> mp[maxn];

void dfs(int u) {
	if (status & (1<<u))
		return;
	status |= (1<<u);

	for (int i = 0; i < m; i++)
		if (G[u][i])
			dfs(i);
}

ll dp(int u, ll st) {
	if (u >= n)
		return 0;
	if (mp[u].find(st) != mp[u].end())
		return mp[u][st];

	ll tmp = dp(u+1, st);

	ll sum = pj[u];
	for (int i = 0; i < m; i++)
		if (!(st & (1<<i)) && (need[u] & (1<<i)))
			sum -= pb[i];

	tmp = max(tmp, sum + dp(u+1, st | need[u]));
	return mp[u][st] = tmp;
}

int main() {
	int t, cas = 1;
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i++)
			scanf("%d", &pj[i]);
		for (int i = 0; i < m; i++)
			scanf("%d", &pb[i]);

		int num, tmp;
		for (int i = 0; i < n; i++) {
			mp[i].clear();
			solve[i].clear();
			scanf("%d", &num);
			for (int j = 0; j < num; j++) {
				scanf("%d", &tmp);
				solve[i].push_back(tmp);
			}
		}
		for (int i = 0; i < m; i++)
			for (int j = 0; j < m; j++)
				scanf("%d", &G[i][j]);

		for (int i = 0; i < n; i++) {
			status = 0;
			int size = solve[i].size();
			for (int j = 0; j < size; j++) 
				dfs(solve[i][j]);
			need[i] = status;
		}

		printf("Case #%d: %I64d\n", cas++, dp(0, 0));
	}
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值