原题链接:http://codeforces.com/contest/1042/problem/B
Vitamins
Berland shop sells n n n kinds of juices. Each juice has its price c i c_i 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 ) n (1≤n≤1000) n(1≤n≤1000) — the number of juices.
Each of the next
n
n
n lines contains an integer
c
i
(
1
≤
c
i
≤
100000
)
c_i (1≤c_i≤100000)
ci(1≤ci≤100000) and a string
s
i
s_i
si — the price of the
i
i
i-th juice and the vitamins it contains. String
s
i
s_i
si contains from
1
1
1 to
3
3
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
s
i
s_i
si. The order of letters in strings
s
i
s_i
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.
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 = 15 5+6+4=15 5+6+4=15 and obtains all three vitamins. He can also buy just the third juice and obtain three vitamins, but its cost is 16 16 16, which isn’t optimal.
In the second example Petya can’t obtain all three vitamins, as no juice contains vitamin “C”.
题解
为什么 B B B题就考状压 d p dp dp哟,有毒??
代码
#include<bits/stdc++.h>
using namespace std;
const int M=1005,A=1,B=2,C=4;
int val[M],mask[M],dp[M][10],n,ans=1e9;
char ch[10];
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
scanf("%d%s",&val[i],ch+1);
for(int j=1;j<=strlen(ch+1);++j)if(ch[j]=='A')mask[i]|=A;else if(ch[j]=='B')mask[i]|=B;else if(ch[j]=='C')mask[i]|=C;
}
memset(dp,127,sizeof(dp));dp[0][0]=1;
for(int i=1;i<=n;++i)for(int j=0;j<i;++j)for(int k=0;k<=7;++k)if(dp[j][k]<=1e9)dp[i][k|mask[i]]=min(dp[i][k|mask[i]],dp[j][k]+val[i]);
if(dp[n][7]>1e9)puts("-1"),exit(0);
for(int i=1;i<=n;++i)ans=min(ans,dp[i][7]);
printf("%d",ans-1);
}