C. Remembering Strings

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/543/problem/C

Description

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.

Sample 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

Sample Output

3

HINT

题意

给你一堆字符串,如果这个字符的第i个字符在所有字符串的第i个都是不同的,那么我们就说这个字符串很好记

然后你可以花费 a[i][j]使得这个字符变成任意字符

然后问你最小花费,使得所有字符串都很好记

题解:

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=<<;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** int a[][];
string s[];
int d[<<];
int p[]; int main()
{
int n=read(),m=read();
for(int i=;i<n;i++)
cin>>s[i];
for(int i=;i<n;i++)
for(int j=;j<m;j++)
cin>>a[i][j];
for(int j=;j<<<n;j++)
d[j]=inf;
d[]=;
for(int i=;i<m;i++)
{
for(int j=;j<;j++)
p[j]=;
for(int j=;j<n;j++)
p[s[j][i]-'a']|=<<j;
for(int j=;j<;j++)
{
int t=,mx=;
for(int k=;k<n;k++)
{
if(p[j]>>k&)
{
for(int l=;l<<<n;l++)
d[l|(<<k)]=min(d[l|(<<k)],d[l]+a[k][i]);
t+=a[k][i];
mx=max(mx,a[k][i]);
}
}
for(int l=;l<<<n;l++)
d[l|p[j]]=min(d[l|p[j]],d[l]+t-mx);
}
}
cout<<d[(<<n)-]<<endl;
return ;
}

最新文章

  1. window对象的inner/outer/page/screen详解
  2. Web Pages - Efficient Paging Without The WebGrid
  3. tomcat 访问软连接
  4. ubuntu usb权限问题解决
  5. crm高速开发之QueryExpression
  6. iOS纯代码制作欢迎界面——UIScrollView, UIPageControl, UIImageView,UIButton, NSTimer
  7. node之路由介绍
  8. CRL快速开发框架4.4版发布,支持主从读写分离
  9. 深度学习之卷积神经网络(CNN)详解与代码实现(一)
  10. SQLAlchemy中解决数据库访问时出现的Incorrect string value: xxx at row 484
  11. bzoj2730(割点+分类讨论)
  12. Java大数相乘-hdu1063
  13. C# 开发圆角控件的具体实现
  14. 使用mysql乐观锁解决并发问题思路
  15. win7下使用Taste实现协同过滤算法
  16. QtWebKit
  17. 【LeetCode 8_字符串_实现】String to Integer (atoi)
  18. 长安大学第四届ACM-ICPC“迎新杯”程序设计竞赛-重现赛 F - 打铁的箱子
  19. 用sql语句查出和sql相关的性能计数器
  20. 【BZOJ】1621: [Usaco2008 Open]Roads Around The Farm分岔路口(dfs)

热门文章

  1. JDK1.8新特性
  2. 【swupdate文档 三】SWUpdate: 嵌入式系统的软件升级
  3. 细说show slave status参数详解(最全)【转】
  4. 【题解】BZOJ 3065: 带插入区间K小值——替罪羊树套线段树
  5. 集合框架之Set学习
  6. C++循环链表解决约瑟夫环问题
  7. SPOJ D-query(莫队算法模板)
  8. ConcurrentMap
  9. 关于js函数 形参和局部变量名相同 的问题
  10. jquery.query.js 插件(示例及简单应用) —— html之间传值