Untitled
unknown
plain_text
a year ago
2.8 kB
2
Indexable
Never
package nang_cap_may_tinh; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution { static int n, total, vouchers, slg, sl; static int[] gia, mua, comb; static int[][] voucher; public static boolean daMua(){ for(int i = 0; i <= n; i++){ if(mua[i] == 1) return false; } return true; } public static int tinhgia(){ int sum = 0; int[] m = mua.clone(); for(int i = 0; i < vouchers; i++){ if(comb[i] == 1){ sum += voucher[i][0]; for(int j = 1; j <= n; j++){ if(voucher[i][j] != 0 && m[voucher[i][j]] == 1){ m[voucher[i][j]] = 0; } } } } for(int i = 0; i <= n; i++){ if(m[i] == 1){ sum += gia[i]; } } // for(int i = 0; i < vouchers; i++){ // System.out.print(comb[i] + " "); // } // System.out.println("\n" + sum); return sum; } public static void backTrack(int k, int price){ if(price >= total){ return; } if(k == vouchers){ total = Math.min(total, tinhgia()); return; } for(int i = 0; i < 2; i++){ if(i == 1){ comb[k] = 1; backTrack(k+1, price + voucher[k][0]); comb[k] = 0; }else{ backTrack(k+1, price); } } } public static void main(String[] args) { try { System.setIn(new FileInputStream("input.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Scanner sc = new Scanner(System.in); int test = sc.nextInt(); for (int t = 1; t <= test; t++) { n = sc.nextInt(); gia = new int[n+1]; total = Integer.MAX_VALUE; for(int i = 1; i <= n; i++){//gia linh kien gia[i] = sc.nextInt(); } vouchers = sc.nextInt();//so goi voucher voucher = new int[vouchers][n+1]; for(int i = 0; i < vouchers; i++){ voucher[i][0] = sc.nextInt(); int sl = sc.nextInt(); for(int j = 1; j <= sl; j++){ voucher[i][j] = sc.nextInt(); } } slg = sc.nextInt(); mua = new int[n+1]; for(int i = 0; i < slg; i++){ int x = sc.nextInt(); mua[x] = 1; } comb = new int[vouchers]; // System.out.println("gia cho troi"); // for(int i = 0; i <= n; i++){ // System.out.print(gia[i] + " "); // } // System.out.println("\nvoucher"); // for(int i = 0; i < vouchers; i++){ // for(int j = 0; j <= n; j++){ // System.out.print(voucher[i][j] + " "); // } // System.out.println(); // } // System.out.println("can mua"); // for(int i = 0; i <= n; i++){ // System.out.print(mua[i] + " "); // } // System.out.println(); backTrack(0, 0); System.out.println("#" + t + " " + total); } sc.close(); } }