POJ 1170
#include<iostream> #define MAXN 1000 #define M_100 100 #define M_5 6 #define inf 1000000000 using namespace std; struct _code { long num; long price; int place; }; struct _node { int n[5]; bool prop; long price; }; long dp[M_5][M_5][M_5][M_5][M_5]; _node _offer[M_100]; _code _m[MAXN]; int _sub[M_5]; long n[M_5+1]; void init(); int num_off; long _dp(); int main() { //freopen("acm.acm","r",stdin); memset(dp,-1,sizeof(dp)); dp[0][0][0][0][0] = 0; int num_buy; int num_pair; int i; int j; int u; int one_code; int one_num; init(); memset(_sub,-1,sizeof(_sub)); cin>>num_buy; for(i = 0; i < num_buy; ++ i) { cin>>u; cin>>_m[u].num; cin>>_m[u].price; _m[u].place = i; _sub[i] = u; } cin>>num_off; for(i = 0; i < num_off; ++ i) { cin>>num_pair; for(j = 0; j < num_pair; ++ j) { cin>>one_code; cin>>one_num; if(_m[one_code].num == -1) { _offer[i].prop = false; } else { _offer[i].n[_m[one_code].place] = one_num; } } cin>>_offer[i].price; } memset(n,0,sizeof(n)); for(i = 0;; ++ i) { if(_sub[i] != -1) { n[i+1] = _m[_sub[i]].num; } else break; } cout<<_dp(); } long _dp() { int i; if(dp[n[1]][n[2]][n[3]][n[4]][n[5]] != -1) { return dp[n[1]][n[2]][n[3]][n[4]][n[5]]; } else { long sum_1 = 0; long sum_2 = 0; int j; for(i = 0; i < 5; ++ i) { if(_sub[i] == -1) { break; } else { sum_1 += n[i+1]*_m[_sub[i]].price; } } long min = sum_1; for(i = 0; i < num_off; ++ i) { if(_offer[i].prop) { for(j = 0; j < 5; ++ j) { if(n[j+1] < _offer[i].n[j]) break; } if(j != 5) continue; else { for(j = 0; j < 5; ++ j) { n[j+1] -= _offer[i].n[j]; } } sum_2 = _offer[i].price; sum_2 += _dp(); for(j = 0; j < 5; ++ j) { n[j+1] += _offer[i].n[j]; } if(min > sum_2) { min = sum_2; } } } dp[n[1]][n[2]][n[3]][n[4]][n[5]] = min; return min; } } void init() { int i; int j; for(i = 0; i < MAXN; ++ i) { _m[i].num = -1; } for(i = 0; i < M_100; ++ i) { for(j = 0; j < 5; ++ j) { _offer[i].n[j] = 0; } _offer[i].prop = true; } }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com