强行使用状压(能过)
查看原帖
强行使用状压(能过)
397809
yangyi2120楼主2022/12/25 10:13
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int n;
int num[21];
int t[21][21];
vector<int> v[21];
int dp[1 << 20][21];
int pre[21];
int ans_s;
int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> num[i];
        v[0].push_back(i);
    }
    int flag;
    for (int i = 1; i < n; i++)
    {
        for (int j = i + 1; j <= n; j++)
        {
            cin >> flag;
            if (flag == 1)
            {
                v[i].push_back(j);
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < (1 << n); i++)
    {
        for (int j = 0; j <= n; j++)
        {
            if (i != 0 && j == 0)
                continue;
            if (j != 0 && ((i >> (j - 1)) % 2 != 1))
                continue;
            for (int k : v[j])
            {
                if ((i >> (k - 1)) % 2 == 1)
                    continue;
                if (dp[i | (1 << (k - 1))][k] < dp[i][j] + num[k])
                {
                    dp[i | (1 << (k - 1))][k] = dp[i][j] + num[k];
                    pre[k] = j;
                    if (dp[i][j] + num[k] > ans)
                    {
                        ans = dp[i][j] + num[k];
                        ans_s = i | (1 << (k - 1));
                    }
                }
            }
        }
    }
    for (int i = 1; i <= n; i++)
        if ((ans_s >> (i - 1)) % 2 == 1)
            cout << i << " ";
    cout << '\n';
    cout << ans << endl;
    return 0;
}
2022/12/25 10:13
加载中...