ZOJ 3777 Problem Arrangement(状压DP)

题目链接 : http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5264

//今年省赛的题目,比赛的时候知道是状压却一直没搞出,直到最后。虽然赛后知道做法,也一直没做的,最近想不开就来做了 - -, 顺便用了下快速枚举k-子集。

恩, 做法么就是开dp[i][j] i已经选过了的题目的一个集合,j表示的是获得了j分,然后就可以直接做了。。(但是好像说会T或者卡空间,我的做法是快速枚举k-子集,这个东西可以看下watashi翻译的《挑战编程竞赛》的p157。。 汗

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 //#include <cmath>
 6
 7 using namespace std;
 8
 9 int P[15][15];
10 int dp[1<<12][505];
11 int n, m;
12
13 int main() {
14     int T;
15     scanf("%d", &T);
16     for (int cas = 1; cas <= T; cas++) {
17         scanf("%d%d", &n, &m);
18         for (int i = 1; i <= n; i++) {
19             for (int j = 1; j <= n; j++) scanf("%d", &P[i][j]);
20         }
21         memset(dp, 0, sizeof(dp)); dp[0][0] = 1;
22
23         for (int k = 1; k <= n; k++) {
24             int comb = (1 << k) - 1;
25             while (comb < (1 << n)) {
26                 for (int i = 0; i < n; i++) {
27                     if (comb >> i & 1) {
28                         int add = P[i + 1][k], low = max(0, m-add);
29                         for (int j = m; j >= low; j--) dp[comb][m] += dp[comb-(1<<i)][j];
30                         for (int j = low - 1; j >= 0; j--) dp[comb][j+add] += dp[comb-(1<<i)][j];
31                     }
32                 }
33                 int x = comb & -comb, y = comb + x;
34                 comb = ((comb & ~y) / x >> 1) | y;
35             }
36         }
37         int ret = dp[(1<<n)-1][m];
38         if (!ret) puts("No solution");
39         else {
40             int nex = 1;
41             for (int i = 1; i <= n; i++) nex *= i;
42             int d = __gcd(ret, nex);
43             printf("%d/%d\n", nex/d, ret/d);
44         }
45     }
46     return 0;
47 }

时间: 2024-10-12 18:26:42

ZOJ 3777 Problem Arrangement(状压DP)的相关文章

ZOJ 3471 Most Powerful 状压DP

水题,一维的DP,表示还剩哪些atom的时候能获得的最大能量 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <

ZOJ 3471 Most Powerful(状压DP)

Description Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way

zoj 3777 Problem Arrangement

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5264 题意:给出n道题目以及每一道题目不同时间做的兴趣值,让你求出所有做题顺序中兴趣值大于等于m的比例.用一个分数表示. 状压dp. 枚举每一个状态,用二进制表示.dp[i][j]表示第i个题目,兴趣值为j的个数. 转移方程 dp[i|(1<<j)][k+a[num][j]]+=dp[i][k];兴趣值大于m的为 dp[1|(1<<j)][m]+=dp[i][k

状压DP [ZOJ 3471] Most Powerful

Most Powerful Time Limit: 2 Seconds      Memory Limit: 65536 KB Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a

zoj zju 2994 Tiling a Grid With Dominoes 状压dp

Tiling a Grid With Dominoes Time Limit: 2 Seconds      Memory Limit: 65536 KB We wish to tile a grid 4 units high and N units long with rectangles (dominoes) 2 units by one unit (in either orientation). For example, the figure shows the five differen

ZOJ 3306 状压dp

转自:http://blog.csdn.net/a497406594/article/details/38442893 Kill the Monsters Time Limit: 7 Seconds Memory Limit: 32768 KB In order to celebrate the 8th anniversary of ZOJ, watashi introduces a strange game to other ZJU ACM team members. The board of

Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100610 Description Robots are becoming more and more popular. They are used nowadays not only in manufacturing plants, but also at home. One programmer wit

ZOJ 3471 Most Powerful(状压DP)

Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two at

zoj 3675 状压dp

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4918 昨天的排位,最初我还以为思维题,然后队友说状压DP,直接放弃,赛后看了队友的代码,在搜下网上的,发现队友的代码居然是最短的,膜拜啊~~~~~~~ 思路是队友 A.L.的 dp[s]=min(dp[s],dp[s']+1) 其中s'可以由s通过一次正着剪指甲或者反着剪指甲达到 至于内层循环,0-m,是因为----从剪指甲刀的最后一位看,最后一位从0移动到m,足够所有情况了