50215. Set Cover

難度:3/5

Second Try: 3/5 Used Time: 13:13

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <ctype.h>
#include <limits.h>

#define min(a,b) ((a)<(b)?(a):(b))

typedef struct{
uint32_t num;
int cost;
} s_set;

int n;
uint32_t all;
s_set set[25];

void get_info(s_set* st, char* s){
st->num = 0;
st->cost = 0;
for(int i = 0; s[i] != '\0'; i++){
st->num |= ((uint32_t)1 << (s[i] - 'a'));
st->cost += (s[i] - 'a' + 1);
}
}

void dfs(uint32_t now_set, int now_idx, int now_cost, int *min_cost){
if(now_cost >= (*min_cost)) return;
if(now_set == all){
(*min_cost) = min((*min_cost), now_cost);
return;
}
else if(now_idx == n) return;
else{
// no choose
dfs(now_set, now_idx + 1, now_cost, min_cost);
// choose
dfs(now_set | set[now_idx].num, now_idx + 1, now_cost + set[now_idx].cost, min_cost);
}
}

signed main(){
for(int i = 0; i < 26; i++) all |= ((uint32_t)1 << (i));

char dummy[55];
while(scanf("%d", &n) != EOF){
for(int i = 0; i < n; i++){
scanf("%s", dummy);
get_info(&set[i], dummy);
}
int min_cost = INT_MAX;
dfs((uint32_t)0, 0, 0, &min_cost);
printf("%d\n", min_cost);
}
}


50215. Set Cover
https://aaronlin1229.github.io/judgegirl_50215/
Author
Akizumi
Posted on
July 17, 2023
Licensed under