ZOJ 4257 MostPowerful(状压DP,简单)

F - Most Powerful
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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


【题目大意】不超过10种气体,两两之间相互碰撞可以产生一定的能量,如a碰b,那么b气体就消失,自身不能碰自身,问最后所能得到的最大能量。

原代码链接:http://blog.csdn.net/accry/article/details/6607703


题解:感觉这个题是我做状态压缩的几个题中最简单的了,这存图用a数组就可以了,也不用处理a数组(有的求路径的题要用弗洛伊德处理原数组),因为碰撞是不可逆的。

接着就用s开始从0枚举状态到1<<N-1,还要注意去掉不可能的情况!!

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int MAXN=10;
const int MAX_S=1<<10;

int a[MAXN+1][MAXN+1];
int dp[MAX_S];
int N;

int main()
{
    while(cin>>N,N)
    {
        for (int i=0; i<N; i++)
            for (int j=0; j<N; j++)
                scanf("%d",&a[i][j]);
        memset(dp,0,sizeof(dp));
        int full=1<<N;
        for (int s=0; s<full; s++)/**< 这块是状态的枚举,必须小于1<<N,
            且从0开始,并且,一般都用S做循环变量*/
        {
            for (int i=0; i<N; i++)
            {
                if ((s&(1<<i))) continue;//去掉i自己的情况
                for (int j=0; j<N; j++)
                {
                    if (i==j) continue;//去掉相同的情况
                    if ((s&(1<<j))) continue;//去掉j自己的情况
                    int newS=s+(1<<j);
                    dp[newS]=max(dp[newS],dp[s]+a[i][j]);
                }
            }
        }
        int ans = 0;
        for(int s = 0; s < full ; ++s)
            ans = max(ans,dp[s]);
        printf("%d\n",ans);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值