2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp

Posted cjlhy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long
using namespace std;

const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-3;
const double PI = acos(-1);

int T, m, n, up, dp2[1<<20], dp[1<<20], pre[1<<20];
char s[100];
vector<int> ans;

int main() {
//    freopen("tests.in", "r", stdin);
    scanf("%d", &T);
    while(T--) {
        ans.clear();
        scanf("%d%d", &m, &n);
        up = 1 << m;
        for(int i = 0; i < up; i++) dp2[i] = 0, dp[i] = inf;
        for(int i = 0; i < n; i++) {
            scanf("%s", s); int x = 0;
            for(int j = 0; j < m; j++)
                if(s[j] == 1) x += 1 << j;
            dp2[x]++;
        }
        for(int i = 0; i < m; i++)
            for(int s = 0; s < up; s++)
                if(s>>i&1) dp2[s^(1<<i)] += dp2[s];
        dp[0] = 0;
        for(int s = 0; s < up; s++) {
            for(int i = 0; i < m; i++) {
                if(s>>i&1) continue;
                int nexs = s | (1<<i);
                if(dp[s]+dp2[s] < dp[nexs]) {
                    dp[nexs] = dp[s]+dp2[s];
                    pre[nexs] = s;
                }
            }
        }
        int nows = up-1, nexs;
        while(nows) {
            nexs = pre[nows];
            int xo = nexs ^ nows;
            for(int i = 0; i < m; i++) {
                if(1<<i == xo) {
                    ans.push_back(i+1);
                    break;
                }
            }
            nows = nexs;
        }
        reverse(ans.begin(), ans.end());
        printf("%d
", dp[up-1]);
        for(int t : ans) printf("%d ", t);
        puts("");
    }
    return 0;
}
/*
*/

 

以上是关于2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp的主要内容,如果未能解决你的问题,请参考以下文章

(寒假开黑gym)2018 ACM-ICPC, Syrian Collegiate Programming Contest(爽题)

2018 ACM-ICPC, Syrian Collegiate Programming Contest F - Pretests SOS dp

ACM-ICPC 2018 南京赛区网络预赛 Magical Girl Haze 最短路

2017-2018 ACM-ICPC, Asia Daejeon Regional Contest

ACM-ICPC 2018 南京赛区网络预赛 E题

ACM-ICPC 2018 徐州赛区(网络赛)