ZOJ 3471 Most Powerful 状压DP

本文探讨了一种利用一维动态规划方法解决原子组合获得最大能量问题的算法。通过实例分析,展示了如何使用动态规划算法进行优化计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

水题,一维的DP,表示还剩哪些atom的时候能获得的最大能量

 

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <cstdlib>
#include <climits>
#include <cmath>
#include <ctime>
#include <algorithm>
#include <stack>
#include <sstream>
#include <numeric>
#include <fstream>
#include <functional>

using namespace std;

#define MP make_pair
#define PB push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef pair<int,int> pii;
const int INF = INT_MAX / 3;
const double eps = 1e-8;
const LL LINF = 1e17;
const double DINF = 1e60;
const int maxn = 15;
int dist[maxn][maxn],n,f[1 << 11];

int dfs(int state) {
    int ret = 0;
    if(~f[state]) return f[state];
    for(int i = 0;i < n;i++) if(state & (1 << i)) {
        for(int j = 0;j < n;j++) if(state & (1 << j)) {
            if(i == j) continue;
            ret = max(ret,dfs(state & (~(1 << j))) + dist[i][j]);
        }
    }
    return f[state] = ret;
}

int main() {
    while(scanf("%d",&n),n) {
        for(int i = 0;i < n;i++) {
            for(int j = 0;j < n;j++) {
                scanf("%d",&dist[i][j]);
            }
        }
        memset(f,-1,sizeof(f));
        int ans = dfs((1 << n) - 1);
        printf("%d\n",ans);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/rolight/p/3907486.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值