zoukankan      html  css  js  c++  java
  • ZOJ 3471 Most Powerful 【状态压缩】

    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 atoms perform when collided and the power every two atoms can produce.

    You are to write a program to make it most powerful, which means that the sum of power produced during all the collides is maximal.

    Input

    There are multiple cases. The first line of each case has an integer N (2 <= N <= 10), which means there are N atoms: A1, A2, ... , AN. Then N lines follow. There are N integers in each line. The j-th integer on the i-th line is the power produced when Ai and Aj collide with Aj gone. All integers are positive and not larger than 10000.

    The last case is followed by a 0 in one line.

    There will be no more than 500 cases including no more than 50 large cases that N is 10.

    Output

    Output the maximal power these N atoms can produce in a line for each case.

    Sample Input

    2
    0 4
    1 0
    3
    0 20 1
    12 0 1
    1 10 0
    0

    Sample Output

    4
    22

    题目大意:有一些气球,当a1碰到a2时候,a2消失得到能量,求得到的最大能量;

    相关二进制的基本知识:
         判断j是否属于集合i:i&(1<<j)
         在集合i中去除j:i-(1<<j)或者 i&(!(1<<j)) i^(1<<j)
          在集合i中加入点j:i|(1<<j)

    思路:dp[state]表示状态为state时的能量最大值,state=10时候化为二进制为1010表示2和4号气球已经消失;

      状态转移方程如下:dp[newstate]=max(dp[newstate], dp[state]+a[i][j]); //对于i&&j都不属于state状态;

    代码如下:

    View Code
    #include<stdio.h>
    #include<string.h>
    #include<math.h> 
    #include<iostream>
    using namespace std; 
    #define N 11
    #define M 1<<10 
    int dp[M+2], a[N][N]; 
    int main()
    {
        int n, i, sum, j, state;
        while(scanf("%d", &n)!=EOF)
        {
            if(n==0)
                break; 
            memset(dp, 0, sizeof(dp)); 
            memset(a, 0, sizeof(a)); 
            sum=(1<<n); 
            for(i=0; i<n; i++)
                for(j=0; j<n; j++)
                    scanf("%d", &a[i][j]);
            for(state=0; state<sum; state++)
            {
                for(i=0; i<n; i++)
                {
                    if(state&(1<<i))
                        continue; //在state状态下i已经被消灭
                    for(j=0; j<n; j++)
                    {
                        if((state&(1<<j)))
                            continue;  //在state状态下j已经被消灭
                        if(i==j)
                            continue; 
                        int newstate=state+(1<<j);
                        dp[newstate]=max(dp[newstate], dp[state]+a[i][j]);
                        //dp[state|(1<<j)]=max(dp[state|(1<<j)], dp[state]+a[i][j]);  
                    }
                }
            }
            int maxnum=0; 
            for(i=0; i<sum; i++)
                maxnum=max(maxnum, dp[i]);
            printf("%d\n", maxnum);
        }
    } 
  • 相关阅读:
    Oslec Echo Canceller:开源回声消除方案
    引用:SpeechLinks
    打印论文分类
    语音算法方案公司
    推送 push
    Android完美多语言应用,不重启应用,不改变系统语言
    定时任务quartz
    在android平台解决找不到sun.misc.BASE64Enocder的问题
    IOS背景view隐藏键盘
    为ExpandableListView自定义Item
  • 原文地址:https://www.cnblogs.com/Hilda/p/2674202.html
Copyright © 2011-2022 走看看