zoukankan      html  css  js  c++  java
  • Marriage Ceremonies LightOJ

    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 jth woman. 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

    题解:dp[ i ][ j ]表示第 i 行,状态为 j 时所取得的最大值

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 int n;
     8 int map[20][20],dp[17][1<<17];
     9 
    10 int solve(){
    11     for(int i=1;i<=n;i++){
    12         for(int j=1;j<(1<<n);j++){
    13             int cnt=0;                                 //  ①               
    14             for(int k=0;k<n;k++) if(j&(1<<k)) cnt++;   //  ②这两行可写成 int cnt=__builtin_popcount(j),快了200ms
    15             if(cnt!=i) continue;                       //  保证状态中有 i 个1
    16             for(int k=0;k<n;k++)
    17                 if(j&(1<<k)) dp[i][j]=max(dp[i][j],dp[i-1][j^(1<<k)]+map[i][k+1]);
    18         }
    19     }
    20     return dp[n][(1<<n)-1];
    21 }
    22 
    23 int main()
    24 {   int kase;
    25     cin>>kase;
    26     for(int t=1;t<=kase;t++){
    27         cin>>n;
    28         for(int i=1;i<=n;i++)
    29             for(int j=1;j<=n;j++)
    30                 scanf("%d",&map[i][j]);
    31         memset(dp,0,sizeof(dp));
    32         printf("Case %d: %d
    ",t,solve());
    33     }
    34     return 0;
    35 }
  • 相关阅读:
    [转]SubVersion 和 CVSNT在Windows下的安装及初步管理
    [Java20071101]JDK配置
    [English20071023]疯狂英语永恒不变的18条黄金法则
    [文摘20071020]富人和穷人的经典差异
    [English20071024]疯狂突破高中句型300句
    [文摘20071017]回家真好 (工作是为了生活)
    [文摘20071020]老婆和老妈掉水里终于有答案啦
    [转]flash与后台数据交换方法整理
    Repeater使用:绑定时 结合 前台JS及后台共享方法
    [文摘20071019]九九重阳节的来历 重阳节传说故事 重阳节的活动习俗 重阳节诗篇
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7502373.html
Copyright © 2011-2022 走看看