Remembering Strings - CodeForces 543 C 状压dp

来源:互联网 发布:网络模式切换 编辑:程序博客网 时间:2024/05/21 01:43

Remembering Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aij coins, you can change character in the j-th position of thei-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.

Sample test(s)
input
4 5abcdeabcdeabcdeabcde1 1 1 1 11 1 1 1 11 1 1 1 11 1 1 1 1
output
3
input
4 3abcabaadcada10 10 1010 1 1010 10 1010 1 10
output
2
input
3 3abcadassa1 1 11 1 11 1 1
output
0


题意:给定你一些字符串和每个字符修改成任意字符的花费。要求使得每个字符串都存在一个在其j列上唯一的字符。

思路:如果要使得i行j列的字符唯一,有两种方式,一种是改变这个字符,一种是改变该列上其他所有的字符。用状态压缩存储有哪些行的字符串不存在唯一的字符,然后逐一去进行上述两种操作。

AC代码如下:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;char s[25][25];int n,m,dp[1100000],num[25][25],INF=1e9,MAXS;int POW[25],MS[25][25],cost[25][25];void init(){    int i,j,k,S;    for(i=1;i<=n;i++)       for(j=1;j<=m;j++)       {           MS[i][j]=MAXS;           for(k=1;k<=n;k++)              if(s[i][j]==s[k][j])              {                  MS[i][j]-=POW[k];                  if(k!=i)                    cost[i][j]+=num[k][j];              }       }}int main(){    int i,j,k,ret,S,S2;    bool flag;    scanf("%d%d",&n,&m);    for(i=1;i<=n;i++)       scanf("%s",s[i]+1);    for(i=1;i<=n;i++)       for(j=1;j<=m;j++)          scanf("%d",&num[i][j]);    POW[1]=1;    for(i=2;i<=23;i++)       POW[i]=POW[i-1]*2;    MAXS=POW[n+1]-1;    init();    ret=MAXS;    for(i=1;i<=n;i++)    {        for(j=1;j<=m;j++)        {            flag=true;            for(k=1;k<=n;k++)               if(s[i][j]==s[k][j] && i!=k)                 flag=false;            if(flag)            {                ret-=POW[i];                break;            }        }    }    dp[ret]=0;    for(S=0;S<ret;S++)       dp[S]=INF;    for(S=ret;S>0;S--)       if(dp[S]<INF)       {           for(i=1;i<=n;i++)              if(S&POW[i])              {                  for(j=1;j<=m;j++)                  {                      S2=S-POW[i];                      dp[S2]=min(dp[S2],dp[S]+num[i][j]);                      S2=S&MS[i][j];                      dp[S2]=min(dp[S2],dp[S]+cost[i][j]);                  }              }       }    printf("%d\n",dp[0]);}



0 0
原创粉丝点击