cf 1042B(状压dp)

简介: cf 1042B(状压dp)

Berland shop sells n kinds of juices. Each juice has its price ci. Each juice includes some set of vitamins in it. There are three types of vitamins: vitamin “A”, vitamin “B” and vitamin “C”. Each juice can contain one, two or all three types of vitamins in it.


Petya knows that he needs all three types of vitamins to stay healthy. What is the minimum total price of juices that Petya has to buy to obtain all three vitamins? Petya obtains some vitamin if he buys at least one juice containing it and drinks it.


Input

The first line contains a single integer n (1≤n≤1000) — the number of juices.


Each of the next n lines contains an integer ci (1≤ci≤100000) and a string si — the price of the i-th juice and the vitamins it contains. String si contains from 1 to 3 characters, and the only possible characters are “A”, “B” and “C”. It is guaranteed that each letter appears no more than once in each string si. The order of letters in strings si is arbitrary.


Output

Print -1 if there is no way to obtain all three vitamins. Otherwise print the minimum total price of juices that Petya has to buy to obtain all three vitamins.


题意:数据有n组数,每组数有一个价值ci和一个字符串S,字符串S中包含3个字母A,B,C,问集齐ABC三个字母的最小价值(一个字母可以有多个)


题解:状压。将维生素A当做001,维生素B当做010,维生素C当做100,目标:求出状态为111的最小代价。

//注意这里的|,两个维生素同时使用,结果是它们的并集,所以是|

#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int dp[100];
char str[100];
int main() {
  int n, val, len, c;
  cin >> n;
  for (int i = 0; i <= 10; i++) dp[i] = inf;
  for (int i = 1; i <= n; i++) {
    cin >> c >> str;
    len = strlen(str);
    val = 0;
    for (int j = 0; j < len; j++) {
      if (str[j] == 'A') val |= 1;
      if (str[j] == 'B') val |= 2;
      if (str[j] == 'C') val |= 4;
    }
    dp[val] = min(dp[val], c);
  }
  for (int i = 0; i <= 7; i++) {
    for (int j = 0; j <= 7; j++) {
      dp[i | j] = min(dp[i | j], dp[i] + dp[j]);
    }
  }
  if (dp[7] >= inf) {
    cout << -1 << endl;
  } else {
    cout << dp[7] << endl;
  }
  return 0;
}
相关文章
|
5月前
CF 1538 G. Gift Set (贪心+思维)
【6月更文挑战第14天】
36 0
|
机器学习/深度学习 人工智能
CF1496A Split it!(数学分析)
CF1496A Split it!(数学分析)
44 0
|
Go vr&ar
CF中的线段树题目
CF中的线段树题目
77 0
|
人工智能 BI
cf 489B(贪心)
cf 489B(贪心)
91 0
cf 327A (前缀和优化dp)
cf 327A (前缀和优化dp)
65 0
CF711D-Directed Roads(组合数学 dfs找环)
CF711D-Directed Roads(组合数学 dfs找环)
84 0
CF711D-Directed Roads(组合数学 dfs找环)
CF1181C Flag (dp 思维)
CF1181C Flag (dp 思维)
75 0
CF1181C Flag (dp 思维)
AC牛客 BM89 合并区间
AC牛客 BM89 合并区间
91 0
|
Shell Windows
CF1567E. Non-Decreasing Dilemma(线段树)
CF1567E. Non-Decreasing Dilemma(线段树)
84 0
|
人工智能 BI
CF1398C. Good Subarrays(思维 前缀和)
CF1398C. Good Subarrays(思维 前缀和)
105 0