zoukankan      html  css  js  c++  java
  • (状态压缩dp)Codeforces 543 C-Remembering Strings

    You have multiset of n strings of the same length, consisting of lowercase English letters. We will say that those strings are easy to remember if for each string there is some position i and some letter c of the English alphabet, such that this string is the only string in the multiset that has letter c in position i.

    For example, a multiset of strings {"abc", "aba", "adc", "ada"} are not easy to remember. And multiset {"abc", "ada", "ssa"} is easy to remember because:

    • the first string is the only string that has character c in position 3;
    • the second string is the only string that has character d in position 2;
    • the third string is the only string that has character s in position 2.

    You want to change your multiset a little so that it is easy to remember. For aijcoins, you can change character in the j-th position of the i-th string into any other lowercase letter of the English alphabet. Find what is the minimum sum you should pay in order to make the multiset of strings easy to remember.

    Input

    The first line contains two integers nm (1 ≤ n, m ≤ 20) — the number of strings in the multiset and the length of the strings respectively. Next n lines contain the strings of the multiset, consisting only of lowercase English letters, each string's length is m.

    Next n lines contain m integers each, the i-th of them contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 106).

    Output

    Print a single number — the answer to the problem.

    Example

    Input
    4 5
    abcde
    abcde
    abcde
    abcde
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    1 1 1 1 1
    Output
    3
    Input
    4 3
    abc
    aba
    adc
    ada
    10 10 10
    10 1 10
    10 10 10
    10 1 10
    Output
    2
    Input
    3 3
    abc
    ada
    ssa
    1 1 1
    1 1 1
    1 1 1
    Output
    0

    第一次写状压dp。状压dp一般是n比较小的情况,这样可以用n位二进制数表示一个状态,依次来进行动态规划。

    对于本题,用col[i][j]表示为了使第i行符合,将第j列中所有与第i行相同的(除了最大的,且可能包括第i行)改成不同的数(n<=20 有26个字母,一定可以)所花费的价格。st[i][j]表示按上述操作后必定符合题意要求的行用二进制表示的状态。

    用dp[i]表示状态i下最小的花费(i范围为[0,(1<<n)-1])

    由为使第i行可行,可能通过只修改其第j列,也可能按上述方式修改第j列的其他行,来进行递推即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <vector>
    12 #include <stack>
    13 #define mp make_pair
    14 //#define P make_pair
    15 #define MIN(a,b) (a>b?b:a)
    16 //#define MAX(a,b) (a>b?a:b)
    17 typedef long long ll;
    18 typedef unsigned long long ull;
    19 const int MAX=1<<21;
    20 const int MAX_V=1e3+5;
    21 const int INF=1e9+5;
    22 const ll INF2=4e18+5;
    23 const double M=4e18;
    24 using namespace std;
    25 const int MOD=1e9+7;
    26 typedef pair<ll,int> pii;
    27 const double eps=0.000000001;
    28 #define rank rankk
    29 int n,m;
    30 int dp[MAX],cost[25][25],Max;
    31 int col[25][25],st[25][25];
    32 char a[25][25];
    33 int main()
    34 {
    35     scanf("%d%d",&n,&m);
    36     for(int i=0;i<n;i++)
    37         scanf("%s",a[i]);
    38     for(int i=0;i<n;i++)
    39         for(int j=0;j<m;j++)
    40             scanf("%d",&cost[i][j]);
    41     for(int i=0;i<n;i++)
    42     {
    43         for(int j=0;j<m;j++)
    44         {
    45             Max=0;
    46             for(int s=0;s<n;s++)
    47             {
    48                 if(a[i][j]==a[s][j])
    49                 {
    50                     Max=max(Max,cost[s][j]);
    51                     col[i][j]+=cost[s][j];
    52                     st[i][j]|=(1<<s);
    53                 }
    54             }
    55             col[i][j]-=Max;
    56         }
    57     }
    58     Max=(1<<n)-1;
    59     fill(dp,dp+(1<<n),INF);
    60     dp[0]=0;
    61     for(int i=0;i<Max;i++)
    62     {
    63         for(int j=0;j<n;++j)
    64         {
    65             if(!((i>>j)&1))
    66             {
    67                 int now=1<<j;
    68                 for(int s=0;s<m;s++)
    69                 {
    70                     dp[i|now]=min(dp[i|now],dp[i]+cost[j][s]);
    71                     dp[i|st[j][s]]=min(dp[i|st[j][s]],dp[i]+col[j][s]);
    72                 }
    73             }
    74         }
    75     }
    76     printf("%d
    ",dp[Max]);
    77     return 0 ;
    78 }
  • 相关阅读:
    xapian的使用
    Andriod 环境配置以及第一个Android Application Project
    2013Esri全球用户大会之ArcGIS for Server&Portal for ArcGIS
    window server 2012 更改密钥 更改系统序列号
    持续集成之路——数据访问层的单元测试(续)
    多项式相乘与相加演示
    hdu 1847 博弈基础题 SG函数 或者规律2种方法
    solaris之cpu
    Android音效SoundPool问题:soundpool 1 not retry
    poj1845-Sumdiv
  • 原文地址:https://www.cnblogs.com/quintessence/p/6936259.html
Copyright © 2011-2022 走看看