zoukankan      html  css  js  c++  java
  • Marriage Ceremonies(状态压缩dp)

     Marriage Ceremonies
    Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

    Description

    You work in a company which organizes marriages. Marriages are not that easy to be made, so, the job is quite hard for you.

    The job gets more difficult when people come here and give their bio-data with their preference about opposite gender. Some give priorities to family background, some give priorities to education, etc.

    Now your company is in a danger and you want to save your company from this financial crisis by arranging as much marriages as possible. So, you collect N bio-data of men and N bio-data of women. After analyzing quite a lot you calculated the priority index of each pair of men and women.

    Finally you want to arrange N marriage ceremonies, such that the total priority index is maximized. Remember that each man should be paired with a woman and only monogamous families should be formed.

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case contains an integer N (1 ≤ n ≤ 16), denoting the number of men or women. Each of the next N lines will contain N integers each. The jth integer in the ith line denotes the priority index between the ith man and jthwoman. All the integers will be positive and not greater than 10000.

    Output

    For each case, print the case number and the maximum possible priority index after all the marriages have been arranged.

    Sample Input

    2

    2

    1 5

    2 1

    3

    1 2 3

    6 5 4

    8 1 2

    Sample Output

    Case 1: 7

    Case 2: 16

     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 int a[17][17] ;
     6 int dp[17][1 << 17] ;
     7 int n ;
     8 
     9 int main ()
    10 {
    11    // freopen ("a.txt" , "r" , stdin ) ;
    12     int T ;
    13     scanf ("%d" , &T ) ;
    14     int ans = 0 ;
    15     while (T--) {
    16         scanf ("%d" , &n ) ;
    17         for (int i = 0 ; i < n ; i++) {
    18             for (int j = 0 ; j < n ; j++) {
    19                 scanf ("%d" , &a[i][j]) ;
    20             }
    21         }
    22         for (int i = 0 ; i < n ; i++) {
    23             for (int j = 0 ; j < 1 << n ; j++ ) {
    24                 dp[i][j] = -1 ;
    25             }
    26         }
    27         for (int i = 0 ; i < n ; i++) {
    28             dp[0][1 << i] = a[0][i] ;
    29         }
    30         for (int i = 1 ; i < n ; i++) {
    31             for (int j = 0 ; j < 1 << n ; j++) {
    32                 if (dp[i - 1][j] != -1) {
    33                     for (int k = 0 ; k < n ; k++) {
    34                         if ( ! (j & 1 << k) ) {
    35                             dp[i][j | 1 << k] = max (dp[i][j | 1 << k] , dp[i - 1][j] + a[i][k] ) ;
    36                         }
    37                     }
    38                 }
    39             }
    40         }
    41         printf ("Case %d: %d
    " , ++ans , dp[n - 1][(1 << n) - 1 ] ) ;
    42     }
    43     return 0 ;
    44 }
    View Code

    先把“ 数据 ”存入 下标为 1 << 0 , 1 << 1 , 1 << 2 , 1 << n - 1的这几个位置,然后就是一般的dp过程了

    状态压缩dp我觉得有两点蛮神奇的(当然能用数学方法证明的)

    1,! ( j & 1 << k ) 这能让已经加过的滚蛋;orz

    2,(1 << 0) + (1 << 1 ) + ... + (1 << n - 1) = (1 << n ) - 1 orz 不仔细看还真没发觉

  • 相关阅读:
    EXP8
    EXP7
    数据库作业
    windows下如何编译运行龙脉代码
    CVE-2019-6340 Drupal8's REST RCE 漏洞复现
    小黄衫获奖感言
    Exp6 MSF应用基础
    Exp5
    实验一 密码引擎-4-国䀄算法交叉测试(选做)
    2020-2021-2 20175335 丹增罗布 《网络对抗技术》Exp1 PC平台逆向破解
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/4348347.html
Copyright © 2011-2022 走看看