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; 
}


你可能感兴趣的:(Juice)