题干:
Berland shop sells nn kinds of juices. Each juice has its price cici. 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 nn (1≤n≤1000)(1≤n≤1000) — the number of juices.
Each of the next nn lines contains an integer cici (1≤ci≤100000)(1≤ci≤100000) and a string sisi — the price of the ii-th juice and the vitamins it contains. String sisi contains from 11to 33 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 sisi. The order of letters in strings sisi 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.
Examples
Input
4 5 C 6 B 16 BAC 4 A
Output
15
Input
2 10 AB 15 BA
Output
-1
Input
5 10 A 9 BC 11 CA 4 A 5 B
Output
13
Input
6 100 A 355 BCA 150 BC 160 AC 180 B 190 CA
Output
250
Input
2 5 BA 11 CB
Output
16
Note
In the first example Petya buys the first, the second and the fourth juice. He spends 5+6+4=155+6+4=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 1616, which isn't optimal.
In the second example Petya can't obtain all three vitamins, as no juice contains vitamin "C".
解题报告:
别忘了考虑 ‘AB’和‘BC’这类的三种情况,‘AB’和‘ABC’这种就不需要了,因为还不如直接'ABC'呢。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n,m;
map<string,int> mp;
int main()
{
int n;
string s;
int tmp;
cin>>n;
for(int i = 1; i<=n; i++) {
cin>>tmp>>s;
sort(s.begin(),s.end());
if(mp.find(s) == mp.end()) mp[s] = tmp;
else mp[s] = min(mp[s],tmp);
}
int minn = 0x3f3f3f3f;
if(mp["A"]>0 && mp["B"]>0 && mp["C"]>0) minn = min(minn,mp["A"] + mp["B"] + mp["C"]);
if(mp["AB"]>0 && mp["C"]>0) minn = min(minn,mp["AB"] + mp["C"]);
if(mp["A"]>0 && mp["BC"]>0) minn = min(minn,mp["A"] + mp["BC"]);
if(mp["AC"]>0 && mp["B"]>0) minn = min(minn,mp["AC"] + mp["B"]);
if(mp["ABC"]>0) minn = min(minn,mp["ABC"]);
if(mp["AB"] && mp["AC"]) minn = min(minn,mp["AB"] + mp["AC"]);
if(mp["AB"] && mp["BC"]) minn = min(minn,mp["AB"] + mp["BC"]);
if(mp["AC"] && mp["BC"]) minn = min(minn,mp["AC"] + mp["BC"]);
if(minn == 0x3f3f3f3f) puts("-1");
else printf("%d\n",minn);
return 0;
}
其他的一堆AC代码:(有用dp的,有无限更新的)
#include <bits/stdc++.h>
/*
unsigned seed1 = std::chrono::system_clock::now().time_since_epoch().count();
mt19937 g1.seed(seed1);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
*/
using namespace std;
int dp[8];
int n;
char buf[10];
int main() {
scanf("%d", &n);
for(int i = 1; i < 8; i++) {
dp[i] = 1e9;
}
while(n--) {
int s;
scanf("%d %s", &s, buf);
int m = 0;
for(int i = 0; i < strlen(buf); i++) {
m |= 1 << (buf[i] - 'A');
}
for(int i = 0; i < 8; i++) {
dp[i|m] = min(dp[i|m], dp[i] + s);
}
}
if(dp[7] == 1e9) dp[7] = -1;
printf("%d\n", dp[7]);
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int N;
int m[8];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
for (int i = 0; i < 8; i++)
m[i] = 1e6;
cin >> N;
for (int i = 0; i < N; i++) {
int c;
string s;
cin >> c >> s;
int cc = 0;
for (int j = 0; j < s.length(); j++)
cc += (1 << (s[j] - 'A'));
m[cc] = min (m[cc], c);
}
int mans = 1e9;
for (int i = 0; i < 256; i++) {
int b = 0, cc = 0;
for (int j = 0; j < 8; j++) {
if (i & (1 << j)) {
b |= j;
cc += m[j];
}
}
if (b == 7)
mans = min (mans, cc);
}
if (mans >= 1e6)
cout << "-1\n";
else
cout << mans << "\n";
}
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf=1e9;
int n, c, bst[8];
char buf[8];
int main() {
for(int i=1; i<8; i++) bst[i]=inf;
scanf("%d", &n);
while(n--) {
scanf("%d%s", &c, buf);
int msk=0;
for(int i=0; buf[i]; i++)
msk|=(1<<(buf[i]-'A'));
bst[msk]=min(bst[msk], c);
}
for(int rnd=0; rnd<514; rnd++)
for(int i=0; i<8; i++)
for(int j=0; j<8; j++)
bst[i|j]=min(bst[i|j], bst[i]+bst[j]);
if(bst[7] == inf) bst[7]=-1;
cout<<bst[7]<<endl;
}
#include <bits/stdc++.h>
using namespace std;
const int INF = 123456789;
int p[8];
int main () {
int n;
cin >> n;
for (int i=0; i<8; ++i) {
p[i] = INF;
}
while (n--) {
int x;
string s;
cin >> x >> s;
int mask = 0;
for (char i:s) {
mask |= (1<<(i-'A'));
}
p[mask] = min(p[mask],x);
}
int cost = INF;
for (int i=0; i<8; ++i) {
if (i == 7) {
cost = min(cost,p[i]);
}
}
for (int i=0; i<8; ++i) {
for (int j=0; j<8; ++j) {
if ((i|j) == 7) {
cost = min(cost,p[i]+p[j]);
}
}
}
for (int i=0; i<8; ++i) {
for (int j=0; j<8; ++j) {
for (int k=0; k<8; ++k) {
if ((i|j|k) == 7) {
cost = min(cost,p[i]+p[j]+p[k]);
}
}
}
}
if (cost == INF) cout << -1 << endl;
else cout << cost << endl;
}