zoukankan      html  css  js  c++  java
  • ZOJ 4257 Most Powerful (状态压缩DP)

    Most Powerful

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    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 Ajgone. 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


    Author: GAO, Yuan
    Contest: ZOJ Monthly, February 2011

    简单的状态压缩DP,

    状态用二进制表示,0表示没有消除,1表示已经消除了。

    dp[i]表示状态为i时获得的最大值。然后不断枚举在的两个进行碰撞。

    (一开始想了个错误的思路,555555555)

    //============================================================================
    // Name        : ZOJ.cpp
    // Author      : 
    // Version     :
    // Copyright   : Your copyright notice
    // Description : Hello World in C++, Ansi-style
    //============================================================================
    
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    int dp[1<<12];
    int g[12][12];
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int n;
        while(scanf("%d",&n)==1 && n)
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                    scanf("%d",&g[i][j]);
            memset(dp,-1,sizeof(dp));
            dp[0]=0;
            int tot=(1<<n);
            int ans=0;
            for(int i=0;i<tot;i++)
            {
                if(dp[i]==-1)continue;
                ans=max(ans,dp[i]);
                for(int j=0;j<n;j++)
                    for(int k=0;k<n;k++)
                        if(j!=k && (i&(1<<j))==0 && (i&(1<<k))==0)
                        {
                            dp[i|(1<<j)]=max(dp[i|(1<<j)],dp[i]+g[k][j]);
                            //dp[i|(1<<k)]=max(dp[i|(1<<k)],dp[i]+g[j][k]);
                        }
            }
            printf("%d\n",ans);
        }
        return 0;
    }

     

    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    linux-溢出程序
    linux下edb调试器
    IOS 学习
    xcode+OC基础学习
    mailcarrier25 EMAIL程序 典型覆盖返回地址XPSP3
    easyftpsvr-1.7.0.2 POC
    VS2010 MSDN Help Library 出现问题处理方法
    虚拟机XP 连接 虚拟机 linux
    修复文章···
    枚举所有进程所有模块,删除制定进程
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3045973.html
Copyright © 2011-2022 走看看