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

Source: Internet
Author: User
Problem descriptionthere'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 proble M. 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?
 
Inputthe 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.
Outputfor each test case, please output a line which is "case # X: Y", X means the number of the test case and Y means the maximum profit.
Sample Input
42 310 106 6 62 0 12 1 20 1 01 0 00 0 02 310 108 10 61 01 20 1 01 0 00 0 02 310 108 10 61 01 20 1 00 0 00 0 02 310 108 10 61 01 20 0 01 0 00 0 0
 
Sample output
Case #1: 2 case #2: 4 case #3: 4 case #4: 6 question: give you n projects and M questions, each project has corresponding problems to be solved, and each problem has associated problems. Ask your biggest benefit idea: the DP Method, first pre-process the problems to be completed in each project, it is represented in binary, followed by the DP result. Each project has the possibility of doing or not doing. It is a memory-based search. When we are dealing with the U project, if the status of the current problem already exists, we have recorded it and directly return it.
#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;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.