#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , f[N][N] , c[N] , w[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = 0 ; j <= v ; j++ ) {
f[i][j] = f[ i - 1 ][j] ;
if ( j >= c[i] ) {
f[i][j] = max( f[i][j] , f[ i - 1 ][ j - c[i] ] + w[i] ) ;
}
}
}
cout << f[n][v] << endl ;
return 0 ;
}
01背包(优化):
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , f[N] , c[N] , w[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = v ; j >= c[i] ; j-- ) {
f[j] = max( f[j] , f[ j - c[i] ] + w[i] ) ;
}
}
cout << f[v] << endl ;
return 0 ;
}
二:完全背包
完全背包(朴素):
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , f[N][N] , c[N] , w[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = 0 ; j <= v ; j++ ) {
for ( int k = 0 ; k * c[i] <= j ; k++ ) {
f[i][j] = max( f[i][j] , f[ i - 1 ][ j - c[i] * k ] + w[i] * k ) ;
}
}
}
cout << f[n][v] << endl ;
return 0 ;
}
完全背包(一级优化):
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , f[N][N] , c[N] , w[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = 0 ; j <= v ; j++ ) {
f[i][j] = f[ i - 1 ][j] ;
if ( j >= c[i] ) {
f[i][j] = max( f[i][j] , f[i][ j - c[i] ] + w[i] ) ;
}
}
}
cout << f[n][v] << endl ;
return 0 ;
}
完全背包(二级优化):
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , f[N] , c[N] , w[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = c[i] ; j <= v ; j++ ) {
f[j] = max( f[j] , f[ j - c[i] ] + w[i] ) ;
}
}
cout << f[v] << endl ;
return 0 ;
}
三:多重背包
多重背包(朴素)
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 5000 + 5 ;
int n , v , c[N] , w[N] , s[N] , f[N][N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] >> s[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = 0 ; j <= v ; j++ ) {
for ( int k = 0 ; k <= s[i] && k * c[i] <= j ; k++ ) {
f[i][j] = max( f[i][j] , f[ i - 1 ][ j - k * c[i] ] + k * w[i] ) ;
}
}
}
cout << f[n][v] ;
return 0 ;
}
多重背包(二进制优化)
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 25000 + 5 ;
int n , v , c[N] , w[N] , f[N] ;
int cnt = 0 ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
while ( n-- ) {
int a , b , s ;
cin >> a >> b >> s ;
int k = 1 ;
while ( k <= s ) {
cnt++ ;
c[cnt] = a * s ;
c[cnt] = b * s ;
}
}
n = cnt ;
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = v ; j >= c[i] ; j-- ) {
f[j] = max( f[j] , f[ j - c[i] ] + w[i] ) ;
}
}
cout << f[v] << endl ;
return 0 ;
}
多重背包(单调队列优化)
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 25000 + 5 ;
int n , m , g[N] , q[N] , f[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> m ;
for ( int i = 0 ; i < n ; i++ ) {
int v , w , s ;
cin >> v >> w >> s ;
memcpy( g , f , sizeof f ) ;
for ( int j = 0 ; j < v ; j++ ) {
int hh = 0 , tt = -1 ;
for ( int k = j ; k <= m ; k = k + v ) {
if ( hh <= tt && q[hh] < k - s * v ) {
hh++ ;
}
while ( hh <= tt && g[q[tt]] - ( q[tt] - j ) / v * w <= g[k] - ( k - j ) / v * w ) {
tt-- ;
}
tt++ ;
q[tt] = k ;
f[k] = g[q[hh]] + ( k - q[hh] ) / v * w ;
}
}
}
cout << f[m] << endl;
return 0 ;
}
四:分组背包
分组背包
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 2000 + 5 ;
int n , v ;
int c[N][N] , w[N][N] , s[N] ;
int f[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> m ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> s[i] ;
for ( int j = 1 ; j <= s[i] ; j++ ) {
cin >> c[i][j] >> w[i][j] ;
}
}
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = v ; j >= 0 ; j-- ) {
for ( int k = 1 ; k <= s[i] ; k++ ) {
if ( c[i][k] <= j ) {
f[j] = max( f[j] , f[ j - c[i][k] ] + w[i][k] ) ;
}
}
}
}
cout << f[v] ;
return 0 ;
}
五:混合背包
混合背包
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 50000 + 5 ;
int n , v , tc , tw , s , cnt = 1 ;
int c[N] , w[N] ;
int f[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> tc >> tw >> s ;
if ( s == 0 ) {// 完全背包
s = v / tc ;
}
if ( s == -1 ) {// 01背包
s = 1 ;
}
int k = 1 ;
while ( k <= s ) {
cnt++ ;
c[cnt] = k * tc ;
w[cnt] = k * tw ;
s = s - k ;
k = k * 2 ;
}
if ( s > 0 ) {
cnt++ ;
c[cnt] = s * tc ;
w[cnt] = s * tw ;
}
}
n = cnt ;
for ( int i = 1 ; i <= n ; i++ ) {
for ( intj = v ; j >= c[i] ; j-- ) {
f[j] = max( f[j] , f[ j - c[i] ] + w[i] ) ;
}
}
cout << f[v] << endl ;
return 0 ;
}
六:二维费用背包
二维费用背包
C++
#include<bits/stdc++.h>using namespace std;
const int N = 5000 + 5 ;
int n , v , tv , tm , w ;
int f[N][N] ;
int main(){
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v >> m ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> tv >> tm >> w ;
for ( int j = v ; j >= tv ; j-- ) {
for ( int k = m ; k >= tm ; k-- ) {
f[j][k] = max( f[j][k] , f[ j - tv ][ k - tm ] + w ) ;
}
}
}
cout << f[v][m] << endl ;
return 0 ;
}
七:有依赖背包
有依赖背包
C++
#include<bits/stdc++.h>using namespace std;
const int N = 1000 + 5 ;
int n , v , cnt , w[N] , c[N] , f[N][N] ;
int root ;
int head[N] , dis[N] , vis[N] ;
struct node {
int to , next ;
} e[N] ;
void add( int x , int y ) {
cnt++ ;
e[cnt].to = y ;
e[cnt].next = head[x] ;
head[x] = cnt ;
return ;
}
void dfs ( int k ) {
for ( int i = head[k] ; i ; i = e[i].next ) {
int son = e[i].to ;
dfs( son ) ;
for ( int j = v - c[k] ; j >= 0 ; j-- ) {
for ( int l = 0 ; l <= j ; l++ ) {
f[k][j] = max( f[k][j] , f[k][ j - l ] + f[son][l] ) ;
}
}
}
for ( int i = v ; i >= c[k] ; i-- ) {
f[k][i] = f[k][ i - c[k] ] + w[k] ;
}
for ( int i = 0 ; i < c[k] ; i++ ) {
f[k][i] = 0 ;
}
}
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
int p ;
cin >> c[i] >> w[i] >> p ;
if ( p == -1 ) {
root = i ;
}
}
dfs( root ) ;
cout << f[root][v] << endl ;
return 0 ;
}
八:背包问题求方案数
背包问题求方案数
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 3000 + 5 , inf = 0x3f3f3f3f , mod = 1e9 + 7 ;
int n , v , res , ans ;
int c[N] , w[N] , f[N] , g[N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = 1 ; i <= n ; i++ ) {
f[i] = -inf ;
}
g[0] = 1 ;
f[0] = 0 ;
for ( int i = 1 ; i <= n ; i++ ) {
for ( int j = v ; j >= c[i] ; j-- ) {
int cnt = 0 , maxn ;
maxn = max( f[j] , f[ j - c[i] ] + w[i] ) ;
if ( maxn == f[j] ) {
cnt = cnt + g[j] ;
}
if ( maxn == f[ j - c[i] ] + w[i] ) {
cnt = cnt + g[ j - c[i] ] ;
}
g[j] = cnt % mod ;
f[j] = maxn ;
}
}
for ( int i = 1 ; i <= v ; i++ ) {
res = max( res , f[i] ) ;
}
for ( int i = 1 ; i <= v ; i++ ) {
if ( res == f[i] ) {
ans = ans + g[i] ;
}
}
cout << ans << endl ;
return 0 ;
}
九:背包问题求具体方案
背包问题求具体方案
C++
#include<bits/stdc++.h>
using namespace std ;
const int N = 3000 + 5 ;
int n , v ;
int c[N] , w[N] , f[N][N] ;
int main( ) {
ios::sync_with_stdio( false ) ;
cin.tie( 0 ) , cout.tie( 0 ) ;
cin >> n >> v ;
for ( int i = 1 ; i <= n ; i++ ) {
cin >> c[i] >> w[i] ;
}
for ( int i = n ; i >= 1 ; i-- ) {
for ( int j = 0 ; j <= v ; j++ ) {
f[i][j] = f[ i + 1 ][j] ;
if ( j >= c[i] ) {
f[i][j] = max( f[i][j] , f[ i + 1 ][ j - c[i] ] + w[i] ) ;
}
}
}
int j = v ;
for ( int i = 1 ; i <= n ; i++ ) {
if ( j >= c[i] && f[i][j] == f[ i + 1 ][ j - c[i] ] + w[i] ) {
cout << i << endl ;
j = j - c[i] ;
}
}
return 0 ;
}