codeforces544E

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 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 n, m (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,矩阵上面就直接考虑横向转移和竖向转移。
每次找最低位DP即可。

%:pragma GCC optimize(4)
using namespace std;
#include<bits/stdc++.h>
#define N 30
int dp[1<<25];
int n,m;
int a[N][N];
#define r(x) scanf("%d",&x)
#define F(i,a,b) for(int i=a;i<=b;i++) 
int qi[N][N];
int st[N][N];
char c[N][N];
int lowbit(int x)
{
    return (x&(-x));
}
int main()
{
    r(n);r(m);
    for(int i=1;i<=n;i++) scanf("%s",c[i]+1);
    F(i,1,n)
    F(j,1,m) 
    r(a[i][j]);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            int it=-2e9,sum=0;
            for(int k=1;k<=n;k++)
            {
                if(c[k][j]==c[i][j])
                {
                    st[i][j]|=(1<<(k-1));
                    sum+=a[k][j];
                    it=max(it,a[k][j]);
                } 
            }
            sum-=it;
            qi[i][j]=sum;
                    }
    }
    for(int i=0;i<=(1<<n)-1;i++) dp[i]=2e9;//初值赋太大 
    dp[0]=0;
    for(int i=0;i<=(1<<n)-1;i++)
    {
        int k;
        for(k=0;k<=n;k++)
        if(!((1<<k)&i))
        {
            break;
        }
        for(int j=1;j<=m;j++)
        {
            dp[i|(1<<k)]=min(dp[i|(1<<k)],dp[i]+a[k+1][j]);
            dp[i|st[k+1][j]]=min(dp[i]+qi[k+1][j],dp[i|st[k+1][j]]);
        }
    }
    cout<<dp[(1<<n)-1]<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值