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

     

    人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
  • 相关阅读:
    1029. Two City Scheduling
    JS判断Android、iOS或浏览器的多种方法(四种方法)【转】
    layui select onchange事件【转】
    PHP 判断数据类型【转】
    php 对数组进行排序【转】
    php中怎么删除数组的第一个元素和最后一个元素【转】
    php数组操作之获取数组元素索引(键)值【转】
    HTML表单中 textarea标签的value属性赋值【转】
    为什么js的"关联数组"不能转成json字符串而对象可以?【转】
    CSS white-space属性是用来设置如何处理元素中的空白【转】
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3045973.html
Copyright © 2011-2022 走看看