tags: dp
Description
llk and wy often go to the yh restaurant to have rice with toppings. One day after they finished eating, llk paid the two people's money together, but wy did not want to owe llk the money. Now wy has some loose money in his hands, and llk also has some loose money in his hands. wy wants to know if they can just make the two owe them, but wy is stupid, can you help wy?
Input
For multiple groups of test data, enter 3 non-negative integers, C, n, m in the first line of each group. C represents the money wy owes llk, n represents the type of face value of money in wy's hands, and m represents the type of face value of money in llk's hands. In the next n rows, each row contains two numbers v and c, which respectively represent c coins with face value v in wy's hand. In the next m rows, there are two numbers v and c in each row, representing c coins with face value v in the hand of llk. (C <= 10000; 1<=n, m<50; 0<=v <=100; 0<=c<=10)
Output
Each group of data outputs one line. If there is a scheme to make wy and llk not owe, output YES, otherwise output NO.
Sample Input
7 1 1
10 1
1 10
Sample Output
YES
HINT
wy gave llk a 10 yuan, and llk gave wy three 1 yuan
Idea: I wanted to use dfs at first, but it was too difficult to write. The discussion after the game was a multiple backpack. The test is dp judgment.
State definition: dp[i][j] represents the legal situation when the repayment amount of the first i category is j.
State transition: dp[i][j]=dp[i-1][j-k*v[i]] (k:0~c[i])
The usual dp is a little different, after the initial assignment of 1, and then dp[i][j] is 1, there is no need to transfer. And pay attention to the transfer conditions are j-k*v[i]>=0 (the amount of money to be paid back is positive) and j-k*v[i]<=C, my input does this to prevent crossing the boundary;
Then look at the code comments specifically, the school's oj is easy to Tle
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=150;
typedef int LL;//Int is required, otherwise T
LL dp[maxn][10010];
LL v[maxn];
LL c[maxn];
int main(void)
{
std::ios::sync_with_stdio(false);
cin.tie(0);
LL C,n,m;
while(cin>>C>>n>>m)
{
memset(dp,0,sizeof(dp));
for(LL i=1;i<=n;i++)
cin>>v[i]>>c[i];
for(LL i=n+1;i<=n+m;i++)
{
cin>>v[i]>>c[i];
v[i]=-v[i];
}
dp[0][0]=1;
for(LL i=1;i<=n+m;i++)
for(LL j=0;j<=C;j++)//If j<=10000, change the loop of j and k, otherwise it will be T
for(LL k=0;k<=c[i];k++)///
{
if(dp[i][j]) continue;//If it is already 1, don't update
if(j-k*v[i]>=0&&j-k*v[i]<=C)//v[i] will be a negative number, <=C, otherwise it may exceed memory
dp[i][j]=dp[i-1][j-k*v[i]];
}
if(dp[n+m][C]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
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 g...
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...
There are n people in this world, conveniently numbered 1 through n. They are using burles to buy goods and services. Occasionally, a person might not have enough currency to buy what he wants or need...
Topic 1 Title: gives the debt relationship of n individuals, and asks to simplify the debt relationship. For example, a owes b10 yuan and b owes c10 yuan, so you can directly ...
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...
101 knapsack problem Have N N N And a capacity of items V V V Backpack. Each item can only be usedonce。 The first i i i Volume of items is v i v_i viThe value is w i w_i wi. Which solving the items ...
Topic: Given n n n Personal debt relationship, u , v , w u,v,w u,v,w Just v v v Owe u u u w w wFor a dollar, we know that the debt relationship can be transferred, so find all the debt relationships w...