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;
    }

     

    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    20160419—JS备忘:服务器回发刷新页面提示重试的解决方案。
    JS备忘--子父页面获取元素属性、显示时间,iframe之间互相调用函数
    20160215--获取页面的高度和宽度
    20151013--设计模式六大原则(转载)
    20150915--文件下载
    20150909—Win10系统80端口被System占用
    GridView中实现CheckBox的全选
    Invoke和BeginInvoke的区别(转载)
    AJAX+图像验证码(一般处理程序)
    报表
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3045973.html
Copyright © 2011-2022 走看看