Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Tuesday, November 6, 2012

uva 11450 - Wedding shopping


Problem link
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;
const int maxm = 210;
const int maxc = 25;
//-----------------------------------
int price[maxc][maxc];
bool reachable[maxc][maxm];
int m,c;
//-----------------------------------
void readfile() {
 cin >> m >> c;
 for (int i=1; i<=c; i++)
 {
  cin >> price[i][0];
  for (int j=1; j<=price[i][0]; j++) cin >> price[i][j];
 }
}
//-----------------------------------
void solve() {
 memset(reachable,false,sizeof(reachable));
   
 for (int i=1; i<=price[1][0]; i++)
  if (m-price[1][i]>=0) reachable[1][m-price[1][i]]=true;
  
 for (int i=2; i<=c; i++)
  for (int j=0; j<=m; j++)
   if (reachable[i-1][j])
    for (int k=1; k<=price[i][0]; k++)
     if (j-price[i][k]>=0) reachable[i][j-price[i][k]]=true;
 
 int i;
 for (i=0; i<=m && !reachable[c][i]; i++);
 if (i==m+1) cout << "no solution" << endl;
 else cout << m-i << endl;
}
//-----------------------------------
int main() {
 int nTest;
 cin >> nTest;
 while (nTest--)
 {
  readfile();
  solve();
 }
 return 0;
}
//-----------------------------------

No comments:

Post a Comment