zoukankan      html  css  js  c++  java
  • Lightoj 1011

    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

    Output for Sample Input

    2

    2

    1 5

    2 1

    3

    1 2 3

    6 5 4

    8 1 2

    Case 1: 7

    Case 2: 1

    n男  n女 选n对使他们的权值最大。相当于每行取一个数且这些数不能有在同一列的。

    状态:dp[s]=max(dp[s],dp[s|(1<<i)]+a[cnt][i],其中s&(1<<i)=1

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/6/14 19:21:17
    File Name     :1011.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int dp[1<<16];
    int n,m;
    int a[30][30];
    int dfs(int s,int cnt){
        if(s==(1<<n)-1){
            return dp[s]=0;
        }
        if(dp[s]!=-1)return dp[s];
        for(int i=0;i<n;i++){
            if(!(s&(1<<i))){
                dp[s]=max(dp[s],dfs(s|(1<<i),cnt+1)+a[cnt][i]);
            }
        }
        return dp[s];
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int T;
        cin>>T;
        for(int t=1;t<=T;t++){
            cin>>n;
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    scanf("%d",&a[i][j]);
            memset(dp,-1,sizeof dp);
            printf("Case %d: %d
    ",t,dfs(0,0));
    
        }
        return 0;
    }

    另一种姿势:dp[i][s]=max(dp[i][s],dp[i-1][k]+a[i][j]).第i行状态为s时能得到的最大值。k是上一行的状态。暴力转移一下:

    #include <stdio.h>
    #include <string.h> #include <iostream> using namespace std; int arr[20][20]; int dp[20][1 << 18]; int main() { int T; int _case = 1; scanf("%d", &T); while(T--) { memset(dp, 0, sizeof(dp)); int n; scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 0; j < n; j++) scanf("%d", &arr[i][j]); for(int i = 1; i <= n; i++) { for(int j = 0; j < n; j++) { for(int k = 0;k <= (1 << n) - 1;k++) { if( (k & (1 << j)) == 0) dp[i][k | (1 << j)] = max(dp[i][k | (1 << j)], dp[i - 1][k] + arr[i][j]); } } } int ans = 0; for(int i = 0;i <= (1 << n) - 1;i++) ans = max(ans, dp[n][i]); printf("Case %d: %d ", _case++, ans); } return 0; }
  • 相关阅读:
    防窜货下加密锁使用常见问题
    SQL Server 2000/2005/2008 触发器的管理和查看
    列表显示时,部分凭证会分两行显示,且不能删除
    JDBC 连接 带实例名的SQL Server
    登录软件提示:读取数据源出现错误,pkcs7填充无效,无法被移除
    完美卸载SQL Server 2008的方案
    彻底卸载(删除)SQL server2000
    NCV5取消:授权数到达,或者许可证过期提示的秘诀
    SQL Server日志清空方法 .
    第二天 一列布局
  • 原文地址:https://www.cnblogs.com/pk28/p/5586128.html
Copyright © 2011-2022 走看看