-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUva1047.java
More file actions
109 lines (101 loc) · 3.21 KB
/
Uva1047.java
File metadata and controls
109 lines (101 loc) · 3.21 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.util.*;
import java.io.*;
public class Uva1047
{
static boolean [] used ;
static int [] a ;
static int [][] intersections ;
static int r ;
static int c ;
static int m ;
static int ans ;
static boolean [] ans_com ;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr = new PrintWriter(System.out);
StringBuilder sb = new StringBuilder("");
int num = 1 ;
while( true ){
StringTokenizer st = new StringTokenizer(br.readLine());
r = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
if( r == 0 ){
break ;
}
ans = 0 ;
ans_com = new boolean[r];
used = new boolean[r] ;
a = new int[r];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < r; i++) {
a[i] = Integer.parseInt(st.nextToken());
}
m = Integer.parseInt(br.readLine());
intersections = new int[m][] ;
for (int i = 0; i < m; i++) {
st = new StringTokenizer(br.readLine());
int t = Integer.parseInt(st.nextToken());
int [] b = new int[t+1];
for (int j = 0; j < t; j++) {
b[j] = Integer.parseInt(st.nextToken()) - 1 ;
}
b[t] = Integer.parseInt(st.nextToken());
intersections[i] = b ;
}
// start here
generate_combination(0,0,0);
sb.append("Case Number "+ num++ + '\n');
sb.append("Number of Customers: " + ans + '\n');
sb.append("Locations recommended: " );
boolean first = true ;
for (int i = 0; i < r; i++) {
if( ans_com[i] ){
sb.append( first ? i+1 : " " + (i+1) );
first = false ;
}
}
sb.append("\n\n");
}
pr.print(sb.toString());
br.close();
pr.close();
}
static int get_duplicate()
{
int exclusion = 0 ;
for (int i = 0; i < m; i++) {
int [] b = intersections[i] ;
int cnt = 0 ;
for (int j = 0; j < b.length-1 ; j++) {
int x = b[j];
if( used[x] ){
cnt++ ;
}
}
if( cnt > 1 ){
exclusion += ( cnt - 1 ) * b[b.length-1];
}
}
return exclusion ;
}
static void generate_combination(int idx , int built , int inclusion)
{
if( built == c){
int exclusion = get_duplicate();
int y = inclusion - exclusion ;
if( y > ans ){
ans = y ;
ans_com = Arrays.copyOf(used , r);
}
return ;
}
if( idx == r){
return ;
}
used[idx] = true ;
generate_combination(idx+1 , built+1 , inclusion + a[idx] );
used[idx] = false ;
generate_combination(idx+1 , built , inclusion );
}
}