ZOJ 3777 Problem Arrangement(狀壓DP)

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   sp   

題目連結 : 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 }
View Code

 

ZOJ 3777 Problem Arrangement(狀壓DP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.