Hi, I was trying to solve the party schedule problem on SPOJ. Unfortunately my code is always signaled as "Wrong answer", the link of the problem:
SPOJ.com - Problem PARTY[
^]
and Here is my C++ code, I used matrix for max fun, and minPrice for minimium cost:
#include <stdio.h>
#include <string.h>
using namespace std;
struct Party
{
int fee;
int fun;
Party() {fee = 0; fun = 0;}
};
Party parties[101];
int matrix[101][501];
int minPrice[101][501];
int budget, num;
bool input();
void maxFun();
int main()
{
for (int i = 0; i < 101; i++)
{
memset(matrix[i], 0, 501);
memset(minPrice[i], 0, 501);
}
while(input())
{
maxFun();
int fun = matrix[num][budget];
int price = minPrice[num][budget];
printf("%u %u\n", price, fun);
}
}
bool input()
{
scanf("%u %u", &budget, &num);
if (!budget && ! num) return false;
for (int i = 1; i <= num; i++) scanf("%u %u", &parties[i].fee, &parties[i].fun);
return true;
}
void maxFun()
{
int currentfun, updatedfun, updatedPrice, currentPrice;
for (int i = 1; i <= num; i++)
{
for (int j = 1; j <= budget; j++)
{
currentfun = matrix[i-1][j];
updatedfun = parties[i].fun + matrix[i-1][j-parties[i].fee];
currentPrice = minPrice[i-1][j];
updatedPrice = parties[i].fee + minPrice[i-1][j-parties[i].fee];
if (parties[i].fee <= j && updatedfun > currentfun)
{
matrix[i][j] = updatedfun;
minPrice[i][j] = updatedPrice;
}
else if (parties[i].fee <= j && updatedfun == currentfun && updatedPrice < currentPrice)
{
matrix[i][j] = updatedfun;
minPrice[i][j] = currentPrice;
}
else
{
matrix[i][j] = matrix[i-1][j];
minPrice[i][j] = minPrice[i-1][j];
}
}
}
}
So where is the mistake ? is it the algorithm ?
Thank you All,
Sam.
What I have tried:
I have tried to arrange the parties ascending, But of course it didn't work..