From 368087eb833577853d32c7a960b4ade49590ff22 Mon Sep 17 00:00:00 2001 From: Kirito <1362050620@qq.com> Date: Sun, 22 May 2016 16:40:42 +0800 Subject: [PATCH] Create 1276.cpp --- POJ/1276.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 POJ/1276.cpp diff --git a/POJ/1276.cpp b/POJ/1276.cpp new file mode 100644 index 0000000..69a2e45 --- /dev/null +++ b/POJ/1276.cpp @@ -0,0 +1,78 @@ +#include +#include +#include +#include +#include +using namespace std; + +///Internet 背包 +namespace PackageNet +{ +using namespace std ; +#define MAX 100001 +struct NODE +{ + int n ; + int d ; +}; +int dp[MAX] ; +NODE node[11] ; +int cash, n ; +void ZeroOnePack( int cost ) +{ + int i ; + for ( i = cash ; i >= cost ; i-- ) + { + if ( dp[i] < dp[i-cost]+cost ) + dp[i] = dp[i-cost]+cost ; + } +} +void CompletePack( int cost ) +{ + int i ; + for ( i = cost ; i <= cash ; i++ ) + { + if ( dp[i] < dp[i-cost]+cost ) + dp[i] = dp[i-cost]+cost ; + } +} +void MultiplePack( int cost, int count ) +{ + if ( cost*count >= cash ) + { + CompletePack( cost ) ; + } + else + { + int k = 1 ; + while ( k < count ) + { + ZeroOnePack( cost*k ) ; + count -= k ; + k *= 2 ; + } + ZeroOnePack( cost*count ) ; + } +} +int main() +{ +// freopen( "e:\\in.txt" , "r" , stdin ) ; + while ( cin >> cash >> n ) + { + int i ; + for ( i = 1 ; i <= n ; i++ ) + cin >> node[i].n >> node[i].d ; + memset( dp, 0, sizeof( dp ) ) ; + for ( i = 1 ; i <= n ; i++ ) + { + MultiplePack( node[i].d, node[i].n ) ; + } + cout << dp[cash] << endl ; + } + return 0 ; +} +} +int main() +{ + return PackageNet::main(); +}