您的位置:首页 > 其它

Codeforces Round #302 (Div. 2) 题解

2015-06-03 23:38 197 查看
题目链接:http://codeforces.com/contest/544

A、题意:给你k和一个串,要你在把串分成k部分,且首字母都不相同

解:直接看串有没有k种不同字母

/*
* Problem:
* Author:  SHJWUDP
* Created Time:  2015/6/4 星期四 10:21:49
* File Name: 233.cpp
* State:
* Memo:
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF=0x3f3f3f3f;

const int MaxA=20+7;

int n, m;
char str[MaxA][MaxA];
int arr[MaxA][MaxA];
int f[1<<20];
int lowzero(int x) {
int res=0;
while(x&1) {
x>>=1; res++;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
//freopen("out", "w", stdout);
#endif
while(~scanf("%d%d", &n, &m)) {
for(int i=0; i<n; i++) {
scanf("%s", str[i]);
}
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
scanf("%d", &arr[i][j]);
}
}
memset(f, 0x3f, sizeof(f)); f[0]=0;
int staNum=1<<n;
for(int s=0; s<staNum; s++) {
if(f[s]==INF) continue;
int r=lowzero(s);
if(r>=n) continue;
for(int j=0; j<m; j++) {
f[s|(1<<r)]=min(f[s|(1<<r)], f[s]+arr[r][j]);
int bits=0, sum=0, mx=0;
for(int i=0; i<n; i++) if(str[i][j]==str[r][j]) {
sum+=arr[i][j];
mx=max(mx, arr[i][j]);
bits|=1<<i;
}
f[s|bits]=min(f[s|bits], f[s]+sum-mx);
}
}
printf("%d\n", f[staNum-1]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: