MIN3 (MIN3.CPP)
Cho K loại tiền, trong đó loại tiền thứ i có trị giá v[i], trọng lượng w[i] và độ dày l[i]. Hãy cho biết cần ít nhất bao nhiêu đồng tiền lấy từ K loại tiền này để được tổng số tiền là M và tổng trọng lượng P, và độ dày D (khi các đồng tiền này được xếp chồng lên nhau). Biết rằng mỗi loại tiền có thể lấy nhiều lần.
Dữ liệu: Cho trong file văn bản MIN3.INP,
• dòng đầu gồm 3 số nguyên K(1 ≤ K ≤ 25), M (1 ≤ M ≤ 150), P (1 ≤ P ≤150), D (1 ≤ P ≤150).
• K dòng tiếp theo, dòng thứ i gồm ba số nguyên v[i], w[i], l[i] ( 1 ≤ v[i], w[i], l[i] ≤ 100).
Kết quả: Ghi ra file văn bản MIN3.OUT số nguyên duy nhất là số đồng tiền ít nhất.
Ví dụ:
INPUT
3 3 5 2
1 2 1
1 1 1
2 3 1
OUTPUT
2
Assistant
To solve the MIN3 problem, we'll employ a dynamic programming (DP) approach. The goal is to determine the minimum number of coins required to achieve a specific total value, weight, and thickness using the available coin types. Here's a step-by-step explanation followed by a C++ implementation.
Problem Breakdown
Inputs:
K: Number of coin types.
M: Target total value.
P: Target total weight.
D: Target total thickness.
For each coin type, three integers representing:
v[i]: Value of the i-th coin.
w[i]: Weight of the i-th coin.
l[i]: Thickness of the i-th coin.
Objective:
Find the minimum number of coins needed to reach exactly M value, P weight, and D thickness.
Each coin type can be used multiple times.
Constraints:
(1 \leq K \leq 25)
(1 \leq M, P, D \leq 150)
(1 \leq v[i], w[i], l[i] \leq 100)
Dynamic Programming Approach
We can model this problem as a three-dimensional unbounded knapsack problem where we aim to minimize the number of items (coins) to achieve exact sums in three dimensions (value, weight, thickness).
DP State Definition
dp[m][p][d]: Represents the minimum number of coins needed to achieve a total value of m, a total weight of p, and a total thickness of d.
Initialization
Initialize all dp[m][p][d] to a large value (e.g., infinity) to signify that those states are initially unreachable.
Set dp[0][0][0] = 0 since zero coins are needed to achieve zero value, weight, and thickness.
Transition
For each coin type, update the DP table as follows:
This transition means that if we can achieve ((m - v[i], p - w[i], d - l[i])) with a certain number of coins, then by adding one more coin of the current type, we can achieve ((m, p, d)) with one additional coin.
Final Answer
After processing all coin types, the value dp[M][P][D] will hold the minimum number of coins required to achieve the target values. If dp[M][P][D] remains infinity, it means it's impossible to achieve the target with the given coin types.
C++ Implementation
Below is the C++ code that implements the above approach. It reads input from MIN3.INP and writes the output to MIN3.OUT. For demonstration purposes, the code is written to read from standard input and write to standard output. You can adjust the file input/output as needed.
cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int K, M, P, D;
// Read K, M, P, D
cin >> K >> M >> P >> D;
// Read K coins: each with value, weight, and thickness
vector<tuple<int, int, int>> coins(K);
for(auto &coin: coins){
cin >> get<0>(coin) >> get<1>(coin) >> get<2>(coin);
}
// Initialize DP table with INF
// dp[m][p][d] = minimum number of coins to achieve (m, p, d)
// Dimensions are (M+1) x (P+1) x (D+1)
// Initialize all to INF except dp[0][0][0] = 0
vector<vector<vector<int>>> dp(M+1, vector<vector<int>>(P+1, vector<int>(D+1, INF)));
dp[0][0][0] = 0;
// Iterate over each coin type
for(int i = 0; i < K; i++){
int vi, wi, li;
tie(vi, wi, li) = coins[i];
// Update DP table for each applicable state
for(int m = vi; m <= M; m++){
for(int p_w = wi; p_w <= P; p_w++){
for(int d = li; d <= D; d++){
if(dp[m - vi][p_w - wi][d - li] + 1 < dp[m][p_w][d]){
dp[m][p_w][d] = dp[m - vi][p_w - wi][d - li] + 1;
}
}
}
}
}
// Check the result
if(dp[M][P][D] < INF){
cout << dp[M][P][D];
}
else{
// If no solution exists, you can choose to output -1 or any other indicator
cout << -1;
}
return 0;
}
Explanation of the Code
Reading Input:
The program reads the number of coin types K, and the target values M, P, and D.
It then reads the details of each coin type into a vector of tuples.
Initializing the DP Table:
A 3D vector dp is created with dimensions ((M+1) \times (P+1) \times (D+1)), initialized to a large value (INF).
The base case dp[0][0][0] is set to 0 because no coins are needed to achieve zero value, weight, and thickness.
Filling the DP Table:
For each coin type, the code iterates through all possible states that can include this coin.
It updates the dp table by considering the inclusion of the current coin, ensuring that the minimum number of coins is recorded.
Output:
After processing all coins, the program checks dp[M][P][D].
If it's less than INF, it prints the minimum number of coins required.
Otherwise, it prints -1 to indicate that the target cannot be achieved with the given coin types.
Handling File I/O
If you need to read from MIN3.INP and write to MIN3.OUT, you can modify the code as follows:
cpp
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// Redirect standard input and output to files
freopen("MIN3.INP", "r", stdin);
freopen("MIN3.OUT", "w", stdout);
// The rest of the code remains the same...
int K, M, P, D;
cin >> K >> M >> P >> D;
vector<tuple<int, int, int>> coins(K);
for(auto &coin: coins){
cin >> get<0>(coin) >> get<1>(coin) >> get<2>(coin);
}
vector<vector<vector<int>>> dp(M+1, vector<vector<int>>(P+1, vector<int>(D+1, INF)));
dp[0][0][0] = 0;
for(int i = 0; i < K; i++){
int vi, wi, li;
tie(vi, wi, li) = coins[i];
for(int m = vi; m <= M; m++){
for(int p_w = wi; p_w <= P; p_w++){
for(int d = li; d <= D; d++){
if(dp[m - vi][p_w - wi][d - li] + 1 < dp[m][p_w][d]){
dp[m][p_w][d] = dp[m - vi][p_w - wi][d - li] + 1;
}
}
}
}
}
if(dp[M][P][D] < INF){
cout << dp[M][P][D];
}
else{
cout << -1;
}
return 0;
}
Make sure that the MIN3.INP file is in the correct directory when you run the program, and the output will be written to MIN3.OUT.
Example Walkthrough
Let's verify the provided example:
Input
code
3 3 5 2
1 2 1
1 1 1
2 3 1
Expected Output
code
2
Explanation:
Select one coin of type 3 (2, 3, 1) and one coin of type 1 (1, 2, 1).
Total value: 2 + 1 = 3
Total weight: 3 + 2 = 5
Total thickness: 1 + 1 = 2
Minimum number of coins used: 2
The DP approach correctly identifies that the minimum number of coins required is 2.