boj 220(상태 압축 DP)

Tourism Planning
Accept:39  
  Submit:170
Time Limit:1000MS  
  Memory Limit:65536KB
Description
Several friends are planning to take tourism during the next holiday.
They have selected some places to visit. They have decided which place to start their tourism and in which order to visit these places. However, anyone can leave halfway during the tourism and will never back to the tourism again if he or she is not interested in the following places. And anyone can choose not to attend the tourism if he or she is not interested in any of the places. 
Each place they visited will cost every person certain amount of money. And each person has a positive value for each place, representing his or her interest in this place. To make things more complicated, if two friends visited a place together, they will get a non negative bonus because they enjoyed each other’s companion. If more than two friends visited a place together, the total bonus will be the sum of each pair of friends’ bonuses.
Your task is to decide which people should take the tourism and when each of them should leave so that the sum of the interest plus the sum of the bonuses minus the total costs is the largest. If you can’t find a plan that have a result larger than 0, just tell them to STAY HOME.
Input
There are several cases. Each case starts with a line containing two numbers N and M ( 1<=N<=10, 1<=M<=10). N is the number of friends and M is the number of places. The next line will contain M integers Pi (1<=i<=M) , 1<=Pi<=1000, representing how much it costs for one person to visit the ith place. Then N line follows, and each line contains M integers Vij (1<=i<=N, 1<=j<=M), 1<=Vij<=1000, representing how much the ith person is interested in the jth place. Then N line follows, and each line contains N integers Bij (1<=i<=N, 1<=j<=N), 0<=Bij<=1000, Bij=0 if i=j, Bij=Bji.
A case starting with 0 0 indicates the end of input and you needn’t give an output.
Output
For each case, if you can arrange a plan lead to a positive result, output the result in one line, otherwise, output STAY HOME in one line.
Sample Input
2 1

십오

0 5
5 0
3 2
30 50
24 48
40 70
35 20
0 4 1
4 0 5
1 5 0
2 2
100 100
50 50
50 50
0 20
20 0
0 0
Sample Output

41
STAY HOME
제목:http://boj.me/onlinejudge/newoj/showProblem/show_problem.php?problem_id=220
분석: 사람이 갔든 안 갔든 압축해서 DP로 하면 된다...
코드:
#include<cstdio>
#include<cstring>
#define max(a,b) (a>b?a:b)
using namespace std;
const int mm = 4444;
int r[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096};
int f[22][mm], p[22], v[22][22], b[22][22], w[mm];
int i, j, k, n, m, ans;
void pre(int k) {
    int i, j, s, sum;
    memset(w, 0, sizeof (w));
    for (s = 0; s < r[n]; ++s) {
        for (i = sum = 0; i < n; ++i) if (s & r[i])w[s] += v[i][k], ++sum;
        w[s] -= p[k] * sum;
        for (i = 0; i < n; ++i) for (j = i + 1; j < n; ++j) if ((s & r[i]) && (s & r[j]))w[s] += b[i][j];
    }
}
int main() {
    while (scanf("%d%d", &n, &m), n + m) {
        for (i = 1; i <= m; ++i)scanf("%d", &p[i]);
        for (i = 0; i < n; ++i) for (j = 1; j <= m; ++j)scanf("%d", &v[i][j]);
        for (i = 0; i < n; ++i) for (j = 0; j < n; ++j)scanf("%d", &b[i][j]);
        for (i = 0; i <= m; ++i) for (j = 0; j < r[n]; ++j)f[i][j] = -1000000000;
        f[0][r[n] - 1] = 0;
        for (k = 1; k <= m; ++k) {
            pre(k);
            for (i = 0; i < r[n]; ++i) for (j = 0; j <= i; ++j) if ((i & j) == j) f[k][j] = max(f[k][j], f[k - 1][i] + w[j]);
        }
        ans = 0;
        for (i = 0; i <= m; ++i) for (j = 0; j < r[n]; ++j)ans = max(ans, f[i][j]);
        if (ans)printf("%d
", ans); else puts("STAY HOME"); } return 0; }

좋은 웹페이지 즐겨찾기