(UVA) Wedding Shopping - Solution
Link to the problem: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2445
The solution below used Dynamic Programming to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public ArrayList<ArrayList<Integer>> garments;
public int[][] matrix;
public int maxMoney;
public int totalMoney;
public int numGarments;
public void rec(int garment, int necessaryMoney) {
if (necessaryMoney > maxMoney) {
return;
}
if (garment == numGarments) {
totalMoney = Math.max(totalMoney, necessaryMoney);
return;
}
if (matrix[garment][necessaryMoney] != 0) {
return;
}
matrix[garment][necessaryMoney] = 1;
for (int i = 0; i < garments.get(garment).size(); i++) {
// go to the next garment considering the ith model of the current garment
rec(garment+1, necessaryMoney+garments.get(garment).get(i));
}
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
maxMoney = sc.nextInt();
numGarments = sc.nextInt();
garments = new ArrayList<>();
for (int j = 0; j < numGarments; j++) {
garments.add(new ArrayList<Integer>());
}
for (int j = 0; j < numGarments; j++) {
int numModels = sc.nextInt();
for (int k = 0; k < numModels; k++) {
garments.get(j).add(sc.nextInt());
}
}
matrix = new int[numGarments][maxMoney+1];
totalMoney = -1;
rec(0, 0);
String answer = (totalMoney == -1) ? "no solution\n" : (totalMoney+"\n");
bw.write(answer);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
The solution below used Dynamic Programming to solve this problem.
import java.io.*;
import java.util.*;
class Main {
public ArrayList<ArrayList<Integer>> garments;
public int[][] matrix;
public int maxMoney;
public int totalMoney;
public int numGarments;
public void rec(int garment, int necessaryMoney) {
if (necessaryMoney > maxMoney) {
return;
}
if (garment == numGarments) {
totalMoney = Math.max(totalMoney, necessaryMoney);
return;
}
if (matrix[garment][necessaryMoney] != 0) {
return;
}
matrix[garment][necessaryMoney] = 1;
for (int i = 0; i < garments.get(garment).size(); i++) {
// go to the next garment considering the ith model of the current garment
rec(garment+1, necessaryMoney+garments.get(garment).get(i));
}
}
public void process() throws NumberFormatException, IOException {
Scanner sc = new Scanner(System.in);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int numTests = sc.nextInt();
for (int i = 0; i < numTests; i++) {
maxMoney = sc.nextInt();
numGarments = sc.nextInt();
garments = new ArrayList<>();
for (int j = 0; j < numGarments; j++) {
garments.add(new ArrayList<Integer>());
}
for (int j = 0; j < numGarments; j++) {
int numModels = sc.nextInt();
for (int k = 0; k < numModels; k++) {
garments.get(j).add(sc.nextInt());
}
}
matrix = new int[numGarments][maxMoney+1];
totalMoney = -1;
rec(0, 0);
String answer = (totalMoney == -1) ? "no solution\n" : (totalMoney+"\n");
bw.write(answer);
}
bw.flush();
bw.close();
return;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Main m = new Main();
m.process();
System.exit(0);
}
}
Comments
Post a Comment