最常见的01背包,就不多言了。另外内联函数的确高效,能从0.01sec下降到0sec。
Run Time: 0sec
Run Memory: 304KB
Code Length: 602Bytes
Submit Time: 2012-02-21 19:03:45
// Problem#: 1342
// Submission#: 1213198
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <cstdio>
using namespace std;
inline int max( int a, int b ) { return a > b ? a: b; }
int main()
{
int N, m;
int v[ 25 ], w[ 25 ];
int dp[ 30000 ];
int i, j;
while ( scanf( "%d%d", &N, &m ) != EOF ) {
for ( i = 1; i <= m; i++ )
scanf( "%d%d", &v[ i ], &w[ i ] );
for ( i = 0; i <= N; i++ )
dp[ i ] = 0;
for ( i = 1; i <= m; i++ ) {
for ( j = N; j >= v[ i ]; j-- )
dp[ j ] = max( dp[ j ], dp[ j - v[ i ] ] + v[ i ] * w[ i ] );
}
printf( "%d\n", dp[ N ] );
}
return 0;
}