Advertisement
juanjo12x

UVA_11495_Wedding_Solution

Aug 9th, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <string>
  6. #include <cctype>
  7. #include <stack>
  8. #include <queue>
  9. #include <list>
  10. #include <vector>
  11. #include <map>
  12. #include <set>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include <cmath>
  16. #define LL unsigned long long
  17. using namespace std;
  18.  
  19. int main() {
  20.     int g,money,k,TC,M,C;
  21.     int price[25][25];
  22.     bool reachable[25][210];
  23.     scanf("%d",&TC);
  24.     while(TC--){
  25.         scanf("%d %d",&M,&C);
  26.         for(g=0;g<C;g++){
  27.             scanf("%d",&price[g][0]);
  28.             for(money=1;money<=price[g][0];money++){
  29.                 scanf("%d",&price[g][money]);
  30.             }
  31.         }
  32.         memset(reachable,false,sizeof reachable);
  33.         for(g=1;g<=price[0][0];g++){/*casos bases*/
  34.             if(M-price[0][g]>=0){
  35.                 reachable[0][M-price[0][g]]=true;
  36.             }
  37.         }
  38.         for(g=1;g<C;g++)
  39.          for(money=0;money<M;money++) if(reachable[g-1][money])
  40.           for(k=1;k<=price[g][0];k++) if(money-price[g][k]>=0)
  41.            reachable[g][money-price[g][k]]=true;
  42.         for (money=0;money<=M && !reachable[C-1][money];money++);
  43.         if(money==M+1) printf("no solution\n");
  44.         else printf("%d\n",M-money);
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement