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 |
简单DP79ms1A#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int E, F; int dp[10086]; struct coin{ int p, w; }; coin coins[520]; bool compare(const coin &c1, const coin &c2){ return c1.w < c2.w; } int main() { int t; scanf("%d", &t); for(int ii = 0; ii < t; ii++){ scanf("%d%d", &E, &F); F -= E; int cn; scanf("%d", &cn); for(int i = 0; i < cn; i++){ scanf("%d%d", &coins[i].p, &coins[i].w); } sort(coins, coins+cn, compare); dp[0] = 0; for(int i = 1; i <= F; i++) dp[i] = 2147483647; for(int i = 1; i <= F; i++){ int minCost = 2147483647; for(int j = 0; j < cn; j++){ if(coins[j].w>i) break; if(dp[i-coins[j].w]==2147483647) continue; int tempCost = coins[j].p + dp[i-coins[j].w]; if(tempCost < minCost) minCost = tempCost; } dp[i] = minCost; } if(dp[F] == 2147483647) printf("This is impossible.\n"); else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[F]); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator