G - Most Powerful(两种dp写法)

题目

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

第一种转移方程 dp[i|(1<<k)] = max(dp[i|(1<<k)], dp[i]+dis[j][k]);

0代表存在,1代表已被击毁, 对于i情况下我们找到其中存在的j,即( ! ( i & (1<<j) ) 然后找到i情况下尚未已被击毁的k,也就是存在的k(k 对应的位置是0) ,即 ! ( i & (1<<k) ) && j != k。在i情况下击毁k的新情况为
i |(1<<k), 所以转移方程为dp[i|(1<<k)] = max(dp[i|(1<<k)], dp[i]+dis[j][k]); 最后结果是情况中只有一个位置存在的, 就是该数二进制只有一个0的情况的dp值 ,所以有 ans = max(ans, dp[(1<<n)-1-(1<<i)]);

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
int const inf = 0x3f3f3f3f;
using namespace std;
int dis[15][15];
int dp[1<<11];

int main(){
    int n, i, j, k;
    while(~scanf("%d", &n)&&n){
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &dis[i][j]);
        memset(dp, 0, sizeof(dp));
        for(i = 0; i <= (1<<n)-1; i++){
            for(j = 0; j < n; j++){
                if(!(i&(1<<j))){
                    for(k = 0; k < n; k++)
                        if(!(i&(1<<k))&& j != k){
                            dp[i|(1<<k)] = max(dp[i|(1<<k)], dp[i]+dis[j][k]);
                        }
                }
            }
        }
        int ans = 0;
        for(i = 0; i < n; i++)
            ans = max(ans, dp[(1<<n)-1-(1<<i)]);
        printf("%d\n", ans);
    }

    return 0;
}

第二种转移方程 dp[i] = max(dp[i], dp[i^(1<<k)]+dis[j][k]);

0代表存在,1代表已被击毁, 对于i情况下我们找到其中存在的j,即( ! ( i & (1<<j) ) 然后找到i情况下已被击毁的k(k 对应的位置是1) ,即 ( i & (1<<k) ) && j != k。i情况的上一情况是k存在,未被击毁的的情况,为 i^(1<<k),将k位置恢复为0 所以转移方程为dp[i] = max(dp[i], dp[i^(1<<k)]+dis[j][k]);最后结果是情况中只有一个位置存在的, 就是该数二进制只有一个0的情况的dp值 ,所以有 ans = max(ans, dp[(1<<n)-1-(1<<i)]);

#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
int const inf = 0x3f3f3f3f;
using namespace std;
int dis[15][15];
int dp[1<<11];

int main(){
    int n, i, j, k;
    while(~scanf("%d", &n)&&n){
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                scanf("%d", &dis[i][j]);
        memset(dp, 0, sizeof(dp));
        for(i = 1; i <= (1<<n)-1; i++){
            for(j = 0; j < n; j++){
                if(!(i&(1<<j))){
                    for(k = 0; k < n; k++)
                        if((i&(1<<k))&& j != k){
                            dp[i] = max(dp[i], dp[i^(1<<k)]+dis[j][k]);
                        }
                }
            }
        }
        int ans = 0;
        for(i = 0; i < n; i++)
            ans = max(ans, dp[(1<<n)-1-(1<<i)]);
        printf("%d\n", ans);
    }

    return 0;
}

体会与小结

两种转移方程都能得到正确结果, 一种是把当前i作为基础去更新接下来的情况, 一种是把i作为结果去寻找上一种情况,来更新i的情况。但等式左边的情况值总是要大于右边的情况值,dp[0] = 0是初值所以子情况在执行这一步前已知,所以可以推得左边。所以第一种情况得从0开始,而第二种从1开始不会错,0也没关系就是多走一轮。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值