Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
555...为什么总是超时。大虾们帮忙看看呀~~//Dynamic Programming //time limit exceeds while using bottom up //trying top down //#define TOP #include <stdio.h> #include <iostream> using namespace std; ////////////////////////////////////////////////////////////////////////////////// int main(void) { #ifdef TOP int optimize( int, int &, int *, int *, int * ); #else void optimize( int &, int &, int *, int * ); #endif int number_of_test_cases = 0, counter_of_cases = 0; int weight_empty = 0, weight_filled = 0, varieties = 0, net = 0; int *value, *weight; scanf( "%d", &number_of_test_cases); for( counter_of_cases = 1; counter_of_cases <= number_of_test_cases; ++counter_of_cases) { scanf( "%d %d", &weight_empty, &weight_filled ); scanf( "%d", &varieties ); value = new int[ varieties + 3 ]; weight = new int[ varieties + 3 ]; net = weight_filled - weight_empty; for( int counter = 1; counter <= varieties; ++counter ) { scanf( "%d %d", &value[ counter ], &weight[ counter ] ); } // for testing #ifdef TOP int *opt; int temp; opt = new int[ net + 1 ]; for( int counter2 = 0; counter2 <= net + 1; counter2++ ) opt[ counter2 ] = -1; opt[ 0 ] = 0; temp = optimize( net, varieties, value, weight, opt ); if( temp == -1 ) printf( "This is impossible.\n" ); else printf( "The minimum amount of money in the piggy-bank is %d.\n", temp ); #else optimize( net, varieties, value, weight ); #endif // delete value; delete weight; #ifdef TOP delete opt; #endif } return 0; } ///////////////////////////////////////////////////////////////////////////////// // bottom up #ifndef TOP void optimize( int &net, int &varieties, int *value, int *weight ) { int *opt; opt = new int[ net + 3 ]; for( int counter = 0; counter <= net + 2; counter++ ) opt[ counter ] = -1; opt[ 0 ] = 0; int temp; //Dynamic Programming for( int counter = 1; counter <= varieties; counter++) { for( int counter2 = net; counter2 >= weight[ counter ]; counter2-- ) { for( int counter3 = 1; counter3 * weight[ counter ] <= counter2 ; counter3++ ) { temp = opt[ counter2 - weight[ counter ] * counter3 ]; /*if( opt[ counter2 ] == -1 && temp != -1 ) opt[ counter2 ] = temp + value[ counter ] * counter3; if( opt[ counter2 ] != -1 && temp != -1 ) opt[ counter2 ] = min( opt[ counter2 ], opt[ counter2 - weight[ counter ] * counter3 ] + value[ counter ] * counter3 ); */ if( temp == -1) continue; else if( opt[ counter2 ] == -1 ) opt[ counter2 ] = temp + value[ counter ] * counter3; else if( temp + value[ counter ] * counter3 < opt[ counter2 ] ) opt[ counter2 ] = temp + value[ counter ] * counter3; } } } if( opt[ net ] == -1 ) printf( "This is impossible.\n" ); else printf( "The minimum amount of money in the piggy-bank is %d.\n", opt[ net ]); } ///////////////////////////////////////////////////////////////////////////////// #else // top down int optimize( int net, int &varieties, int *value, int *weight, int *opt ) { if( opt[ net ] == -1) for( int counter1 = 1; counter1 <= varieties; counter1++) { for( int counter2 = 1; counter2 * weight[ counter1 ] <= net; counter2++ ) { int temp = optimize( net - weight[ counter1 ] * counter2, varieties, value, weight, opt ); if( temp == -1 ) continue; else if( opt[ net ] == -1 ) opt[ net ] = temp + value[ counter1 ] * counter2; else if( temp + value[ counter1 ] * counter2 < opt[ net ] ) opt[ net ] = temp + value[ counter1 ] * counter2; } } return opt[ net ]; } #endif Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator