AT1919 department classification

AT1919 department classification

Different world Portal:
Luo Gu AT1919
ATcoder056C
official solution to a problem
of this solution to a problem from

Face questions

Companies that are of Takahashi-kun \ (N \) consists of people of employees. Employees \ (i \) and employee \ (j \) between the reliability \ (w_ {i, j}
\) is definite. Because thanks to like the company has grown steadily, \ (N \) had to divide the people in several departments. Here, the score of the department division, (the number of divisions) * \ (K \) - defined as (total sum of trust between two people belonging to different departments). Please write a program to find the maximum value of the score.
\ ((n \ leq 17) \)

A topic Italy

\ (n \) individual groups, will have the cost among different groups of people, so that \ (the number of groups * K - the total cost of \) is maximized.

Zhetizhazuo

Given this low number of questions, we can be like pressure.
Status \ (f [S] \) represents the selected \ (S \) state when the maximum value set, transition is made from a subset thereof, the \ [f [S] = max (f [S], K + f [S '] + (cost of the newly added people who generated subset)) \] However, if the time of each transfer \ (n ^ 2 \) cost calculation and newly added subset of people who produced, complexity is not allowed.
But we can calculate the cost of each of the internal collection for everyone, but the price he and another set of people that is generated by two sets and set the price minus the cost of two internal collections.

\ ['S set A, B, C = A \ B Cup \] \ [F [A, between B] = f [C] - f [A] - f [B] \]

In this way we can \ (O (n-2 ^) \) O (. 1) when transferring pre-calculated for each subset of internal cost. Since the enumeration of subsets \ (O (n-^. 3) \) , so the overall complexity \ (O (n-^. 3) \) , \ (^. 3. 17} = {129, 140, 163 \) , this can question.

Put the code

#include <bits/stdc++.h>
namespace fdata
{
inline char nextchar()
{
    static const int BS = 1 << 21;
    static char buf[BS], *st, *ed;
    if (st == ed)
        ed = buf + fread(st = buf, 1, BS, stdin);
    return st == ed ? -1 : *st++;
}
#ifdef lky233
#define nextchar getchar
#endif
template <typename T>
inline T poread()
{
    T ret = 0;
    char ch;
    while (!isdigit(ch = nextchar()))
        ;

    do
        ret = ret * 10 + ch - '0';
    while (isdigit(ch = nextchar()));
    return ret;
}
template <typename Y>
inline void poread(Y &ret)
{
    ret = 0;
    char ch;
    while (!isdigit(ch = nextchar()))
        ;

    do
        ret = ret * 10 + ch - '0';
    while (isdigit(ch = nextchar()));
}
#undef nextcar
} // namespace fdata
using fdata::poread;
using namespace std;
const int MAXN = 18;
const long long INF = 1ll << 55;
long long w[MAXN][MAXN];
long long memo[1 << MAXN];
long long f[1 << MAXN];
int n;
long long k;
int main()
{
#ifdef lky233
    freopen("testdata.in", "r", stdin);
    freopen("testdata.out", "w", stdout);
#endif
    poread(n);
    poread(k);
    for (register int i = 0; i < n; ++i)
        for (register int j = 0; j < n; ++j)
            poread(w[i][j]);
    for (register int s = 0; s < (1 << n); ++s)
    {
        for (register int i = 0; i < n; ++i)
        {
            for (register int j = i + 1; j < n; ++j)
            {
                register int si = ((s >> i) & 1);
                register int sj = ((s >> j) & 1);
                if (si & sj)
                    memo[s] += w[i][j];
            }
        }
    }
    for (register int i = 0; i <= (1 << n); ++i)
    {
        for (register int j = i; j; j = (j - 1) & i)
        {
            f[i] = max(f[i], k + f[i ^ j] - memo[i] + memo[j] + memo[i ^ j]);
        }
    }
    cout << f[(1 << n) - 1] << endl;
}

There is also a small episode

A subset of the original solution to a problem enumeration is so written

for (register int i = 0; i <= (1 << n); ++i)
    {
        register int j = i;
        do
        {
        } while (j != i);
    }

As \ (J \) \ (! = \) \ (I \) Why can successfully ends the loop, when $ i = -1 $, itsThe second elementBinary representation is \ (11111111 ... \) , \ (J \) & \ (i \) exactly \ (J \) , and therefore the successful end of the cycle.

This hen 题解 arrival 此结 bunch, thank you.

Guess you like

Origin www.cnblogs.com/Shiina-Rikka/p/11574974.html