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

     

    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    IOS总结_无需自己定义UITabbar也可改变UITabbarController的背景和点击和的颜色
    破解中国电信华为无线猫路由(HG522-C)自己主动拨号+不限电脑数+iTV
    HDUJ 2074 叠筐 模拟
    CSRF——攻击与防御
    Ant命令行操作
    C#软件开发实例.私人订制自己的屏幕截图工具(七)加入放大镜的功能
    qemu-kvm-1.1.0源代码中关于迁移的代码分析
    FileSystemWatcher使用方法具体解释
    configure交叉编译
    海量图片存储策略
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3045973.html
Copyright © 2011-2022 走看看