Juice

Mr. King has made healthy juice for his colleagues. This has been made of 100% natural vitamin C especially for overcoming fatigue. With powder from various natural fruits in it, it has diverse colors, prices and weights.
Mr. Kim has poured various kinds of powder in a cup to make a special colored cup of juice. If you know the price and weight of each bag of powder and the weight of the filled cup of juice, create a calculation program to find out the lowest cost used for making the cup of juice.

Time limits : 1 second (java: 2 seconds)

Input Format


Input may include many test cases. The number of test cases, T, is given on the first line of input and then the amount of T of test cases is given in a line. (T ≤ 20)
The weight of the empty cup, E, and the weight of the filled cup of juice, F, are given separately as blanks for the first line of each test case. (1 ≤ E ≤ F ≤ 10,000)
The number of kinds of juice that is used, N, is given for the next line. (1 ≤ N ≤ 500)
From the next line through to the amount of N lines, the unit price and weight of each juice is given separately as blanks. The unit price of juice is positive number ≤ 50,000; and the weight is positive number ≤ 10,000.

Output Format

Output the minimum cost of cases for the first line of each test case. If you can not make it, output "impossible".

Input Example

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 5
2
10 3
20 4

Output Example

60
100

20


#include<stdio.h>  
#include<string.h>  
#define min(a,b) a<b?a:b  
const int INF=999999999; 
int c[505],w[505],f[10000]; 
int main() 
{ 
    int i,j,T,E,F,V,N; 
    scanf("%d",&T); 
    while(T--){ 
        scanf("%d%d",&E,&F); 
        V=F-E; 
        scanf("%d",&N); 
        for(i=0;i<N;i++) scanf("%d%d",&w[i],&c[i]); 
        for(f[0]=0,i=1;i<=V;i++) f[i]=INF; 
        for(i=0;i<N;i++){  
            for(j=c[i];j<=V;j++){ 
                f[j]=min(f[j],f[j-c[i]]+w[i]); 
            } 
        } 
        if(f[V]!=INF) 
            printf("%d\n",f[V]); 
        else printf("impossible\n"); 
    } 
    return 0; 
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值