poj 2288 Islands and Bridges 状压dp

题目

题目链接:http://poj.org/problem?id=2288

题目来源:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=105944#problem/H

简要题意:求三个东西和最大的哈密顿路径。

题解

这题比较恶心啊,必须保存前俩的状态,然后需要保存最大值和个数。

通过前俩去递推后边的,把三部分增量根据前面两个求再去更新状态。

要注意两个和一个时候的边界条件。

主要是看实现,思路上没有什么障碍,要各种仔细,代码略长。

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
// head
const int N = 13;
const int M = 1<<13;

bool g[N][N];
int dp[N][N][M];
int cnt[N][N][M];
int a[N];

struct State {
    int pp, p, v;
    State(int pp, int p, int v) : pp(pp), p(p), v(v) {}
    State() {}
};

int getDp(State &s) {
    return dp[s.pp][s.p][s.v];
}
int getCnt(State &s) {
    return cnt[s.pp][s.p][s.v];
}
void setState(State &s, int v, int c) {
    dp[s.pp][s.p][s.v] = v;
    cnt[s.pp][s.p][s.v] = c;
}

int main() {
    int n, m, t, u, v;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        memset(dp, -1, sizeof dp);
        memset(cnt, 0, sizeof cnt);
        memset(g, 0, sizeof g);

        for (int i = 0; i < n; i++) {
            scanf("%d", a+i);
        }
        for (int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            u--, v--;
            if (u == v) continue;
            g[u][v] = g[v][u] = true;
        }

        if (n == 1) {
            printf("%d %d\n", a[0], 1);
            continue;
        }
        queue<State> q;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!g[i][j]) continue;
                int st = (1<<i) | (1<<j);
                State nxt = State(i, j, st);
                setState(nxt, a[i] + a[j] + a[i]*a[j], 1);
        q.push(nxt);
            }
        }

        while (!q.empty()) {
            State cur = q.front();
            q.pop();
            for (int i = 0; i < n; i++) {
                int p = i, pp = cur.p, ppp = cur.pp, v = getDp(cur);
                if (((1<<i) & cur.v) || !g[pp][p]) continue;

                State nxt = State(pp, p, cur.v | (1<<i));
                if (getDp(nxt) == -1) q.push(nxt);

                v += a[p] + a[p]*a[pp];
                if (g[ppp][p]) v += a[ppp]*a[pp]*a[p];
                if (getDp(nxt) == v) {
                    setState(nxt, v, getCnt(nxt) + getCnt(cur));
                } else if (getDp(nxt) < v) {
                    setState(nxt, v, getCnt(cur));
                }
            }
        }

        int mx = (1<<n)-1, ans = 0;
        LL ansc = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                State s = State(i, j, mx);
                LL v = getDp(s);
                if (v > ans) {
                    ans = v;
                    ansc = getCnt(s);
                } else if (v == ans) {
                    ansc += getCnt(s);
                }
            }
        }
        printf("%d %I64d\n", ans, ansc / 2);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值