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;
}
相关文章
|
弹性计算 数据可视化 Java
ECS使用体验
.系统选择与环境搭建 对数据库的操作
|
运维 架构师 小程序
我从 HX 辞职了
这篇文章早就想写了,结果一直拖到 2020 最后一天,借着年终总结的感觉,记一下流水账,算是给这段经历画上一个句号。
325 0
我从 HX 辞职了
|
20天前
|
人工智能 自然语言处理 Shell
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
32387 120
🦞 如何在 OpenClaw (Clawdbot/Moltbot) 配置阿里云百炼 API
|
15天前
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
6875 20
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
|
14天前
|
人工智能 机器人 Linux
OpenClaw(Clawdbot、Moltbot)汉化版部署教程指南(零门槛)
OpenClaw作为2026年GitHub上增长最快的开源项目之一,一周内Stars从7800飙升至12万+,其核心优势在于打破传统聊天机器人的局限,能真正执行读写文件、运行脚本、浏览器自动化等实操任务。但原版全英文界面对中文用户存在上手门槛,汉化版通过覆盖命令行(CLI)与网页控制台(Dashboard)核心模块,解决了语言障碍,同时保持与官方版本的实时同步,确保新功能最快1小时内可用。本文将详细拆解汉化版OpenClaw的搭建流程,涵盖本地安装、Docker部署、服务器远程访问等场景,同时提供环境适配、问题排查与国内应用集成方案,助力中文用户高效搭建专属AI助手。
4852 12
|
17天前
|
人工智能 机器人 Linux
保姆级 OpenClaw (原 Clawdbot)飞书对接教程 手把手教你搭建 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI智能体,支持飞书等多平台对接。本教程手把手教你Linux下部署,实现数据私有、系统控制、网页浏览与代码编写,全程保姆级操作,240字内搞定专属AI助手搭建!
5703 21
保姆级 OpenClaw (原 Clawdbot)飞书对接教程 手把手教你搭建 AI 助手

热门文章

最新文章