tags: Dynamic programming
Topic link
Food Problem
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1731 Accepted Submission(s): 544
Problem Description
Few days before a game of orienteering, Bell came to a mathematician to solve a big problem. Bell is preparing the dessert for the game. There are several different types of desserts such as small cookies, little grasshoppers and tiny mantises. Every type of dessert may provide different amounts of energy, and they all take up different size of space.
Other than obtaining the desserts, Bell also needs to consider moving them to the game arena. Different trucks may carry different amounts of desserts in size and of course they have different costs. However, you may split a single dessert into several parts and put them on different trucks, then assemble the parts at the game arena. Note that a dessert does not provide any energy if some part of it is missing.
Bell wants to know how much would it cost at least to provide desserts of a total energy of p (most of the desserts are not bought with money, so we assume obtaining the desserts costs no money, only the cost of transportation should be considered). Unfortunately the mathematician is having trouble with her stomach, so this problem is left to you.
Input
The first line of input contains a integer T(T≤10) representing the number of test cases.
For each test case there are three integers n,m,p on the first line (1≤n≤200,1≤m≤200,0≤p≤50000), representing the number of different desserts, the number of different trucks and the least energy required respectively.
The i−th of the n following lines contains three integers ti,ui,vi(1≤ti≤100,1≤ui≤100,1≤vi≤100) indicating that the i−th dessert can provide ti energy, takes up space of size ui and that Bell can prepare at most vi of them.
On each of the next m lines, there are also three integers xj,yj,zj(1≤xj≤100,1≤yj≤100,1≤zj≤100) indicating that the j−th truck can carry at most size of xj , hiring each one costs yj and that Bell can hire at most zj of them.
Output
For every test case output the minimum cost to provide the dessert of enough energy in the game arena if it is possible and its cost is no more than 50000. Otherwise, output TAT on the line instead.
Sample Input
4
1 1 7
14 2 1
1 2 2
1 1 10
10 10 1
5 7 2
5 3 34
1 4 1
9 4 2
5 3 3
1 3 3
5 3 2
3 4 5
6 7 5
5 3 8
1 1 1
1 2 1
1 1 1
Sample Output
4
14
12
TAT
Source
2015 ACM/ICPC Asia Regional Changchun Online
The meaning of the title: n kinds of snacks, each kind of snack has a certain energy t, volume u and quantity v. Now m kinds of trucks are used to carry these snacks, and each type of driving has a certain capacity x , Cost y and quantity z, snacks can be divided, but you have to choose all of them if you choose one piece, now ask the minimum cost of transporting snacks whose total energy is not less than p
Idea: After reviewing multiple backpacks, I don’t know how to QAQ.
There are many restrictions here and it’s messy. Let’s take a step by step and look at the dim sum first. The dim sum capacity we need first is the minimum volume of p, and then the minimum cost of the truck under the minimum volume. So it becomes a two-step problem. First ask for multiple backpacks for dim sum, and then ask for backpacks for trucks. The slove here is actually a binary enumeration optimization operation (to prevent overtime, multiple backpacks can be converted into 01 backpacks at the same time).
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=205;
const int N=5e4+100;
const int inf=0x3f3f3f3f;
int dp[N],t[maxn],u[maxn],v[maxn],x[maxn],y[maxn],z[maxn],a[N],b[N],cnt=0;
void slove(int n,int s1[],int s2[],int s3[])
{
cnt=0;
for(int i=1;i<=n;++i)
{
int k=1;
while(k<s3[i])
{
a[cnt]=k*s1[i],b[cnt++]=k*s2[i];
s3[i]-=k;
k*=2;
}
if(s3[i]) a[cnt]=s3[i]*s1[i],b[cnt++]=s3[i]*s2[i];
}
}
int main()
{
int T,n,m,p,sum1,sum2,ans,minn;
scanf("%d",&T);
while(T--)
{
sum1=sum2=0;ans=minn=inf;
scanf("%d %d %d",&n,&m,&p);
for(int i=1;i<=n;++i) scanf("%d %d %d",&t[i],&u[i],&v[i]),sum1+=t[i]*v[i];
for(int i=1;i<=m;++i) scanf("%d %d %d",&x[i],&y[i],&z[i]),sum2+=x[i]*z[i];
for(int i=0;i<N;++i) dp[i]=inf;
dp[0]=0;
slove(n,t,u,v);
for(int i=0;i<cnt;++i)//dp[j] represents the smallest volume with energy j
for(int j=p+100;j>=a[i];--j)//Optimized with rolling array, so reverse order
{
dp[j]=min(dp[j],dp[j-a[i]]+b[i]);
if(j>=p) minn=min(minn,dp[j]);
}
if(sum1<p||sum2<minn){
printf("TAT\n");continue;
}
slove(m,x,y,z);
for(int i=0;i<N;++i) dp[i]=0;
for(int i=0;i<cnt;++i)//dp[j] represents the maximum volume cost j
for(int j=50000;j>=b[i];--j)
{
dp[j]=max(dp[j],dp[j-b[i]]+a[i]);
if(dp[j]>=minn) ans=min(ans,j);
}
if(ans>50000) printf("TAT\n");
else printf("%d\n",ans);
}
}
Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Problem Description There are n coins with different denominations, and the denomination of each coin is stored in the array T[1:n]. Now I ...
Article Directory Multipack I Title description Plain version code (time complexity: O(NVS)) Update while reading Use rolling array to optimize one dimension Multiple knapsack problem II (binary optim...
Multiple backpack issues Topic description There are N items and a capacity of a backpack. Item I ItemUp to SIEach volume is VI, the value is Wi. Solving which items will be loaded into a backpack, wh...
Multiple backpack Problem Description The problem of multiple backpacks is from the problem of 0-1 backpacks, and we will first understand the 0-1 backpack problem before solving multiple backpack pro...
WeChat public account:Jerry's algorithm and NLP The backpack problem is a classicDynamic programmingmodel.It is simple and easy to understand, and to a certain extent can reveal the essence of dynamic...
Multiple backpacks, the number of items that can be selected is limited, up to c[i] 1. Binary optimization: The idea of binary optimization is still very clever, according to c [i] to get a set of s...
B - Dividing HDU - 1059 The meaning of the question: give 1,2,3,4,5,6, the amount of all kinds of money, ask if you can spell out half of the total amount of money. Idea: Run the multi-pac...
Problem Description: There are n items, their weight is wi, their size is vi, their number is ni, now give you a C-size backpack, how to make a backpack The loaded items have the largest sum of weight...
1086 Backpack Problem V2 Base time limit: 1 second Space limit: 131072 KB Score: 40Difficulty: Level 4 algorithmic questions Collection Follow There are N kinds of items, the quantity of each it...
Before that, we talked about 01 backpack and complete backpack. This time, let’s talk about the funny and weird (I don’t think) multiple backpack problem. Multiple knapsack problem Classic...