$$ \newcommand{\seq}[2]{{#1}_{1},{#1}_{2},\cdots,{#1}_{#2}} \newcommand{\num}[1]{1,2,\cdots,#1} \newcommand{\stra}[2]{\begin{bmatrix}#1 \\ #2\end{bmatrix}} \newcommand{\strb}[2]{\begin{Bmatrix}#1 \\ #2\end{Bmatrix}} \newcommand{\dw}[1]{\underline{#1}} \newcommand{\up}[1]{\overline{#1}} $$

Codeforces 544

544 D

题意

给你一张图,求能删除的最多可能边数,使从节点 \(s_i\)\(t_i\) 的距离恰为 \(l_i\) ( \(i=0,1\) )
每条边的边权为 \(1\)
( \(1\le n\le 3000,n-1\le m\le \min \{3000,\frac{n(n-1)}{2}\}\) )
不可能输出 \(-1\)

Examples

Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2
Output
0
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
2 4 2
Output
1
Input
5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 1
Output
-1

\(n\) 遍dijkstra或spfa处理出两两点对间的最短路。
设答案的初始值为 \(s_1\)\(t_1\) 的距离+ \(s_2\)\(t_2\) 的距离。
然后考虑 \(s_1\)\(t_1\) 的最短路和 \(s_2\)\(t_2\) 的最短路有交集的情况。
1.
\(s1\;\;\;\;\;\;\;\;t1\)
\(|\;\;\;\;\;\;\;\;\;\;|\)
\(i---j\)
\(|\;\;\;\;\;\;\;\;\;\;|\)
\(s2\;\;\;\;\;\;\;\;t2\)

\(t1\;\;\;\;\;\;\;\;s1\)
\(|\;\;\;\;\;\;\;\;\;\;|\)
\(i---j\)
\(|\;\;\;\;\;\;\;\;\;\;|\)
\(s2\;\;\;\;\;\;\;\;t2\)

暴力枚举 \(i,j\) 即可。

544 E

题意

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 ≤ a_{ij} ≤ 10^6\) ).

Output

Print a single number — the answer to the problem.

Examples

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

状压\(\text{dp}\)
由于最多只有 \(20\) 行,而有 \(26\) 个字母,所以不存在字母不够的问题。
把一个位置合法化有两种方案:①把当前位置字母改成一个新字母②把本列其余所有与该位置字母相同的位置改掉
预处理出把位置 \((i,j)\) 合法化且采用第二种方案的代价 \(c[][]\) ,和每一列 \(i\) 中所有与位置 \(j\) 相同字母的位置 \(eq[][]\)
转移方程:
\(dp[j|(1<<k)]=min(dp[j|(1<<k)],dp[j]+a[k][i]);\)(①)
\(dp[j|eq[k][i]]=min(dp[j|eq[k][i]],dp[j]+c[k][i]);\)(②)

posted @ 2019-02-12 19:10  chc_1234567890  阅读(167)  评论(0编辑  收藏  举报