Click here to Skip to main content
16,004,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C++
#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..
Posted
Comments
Patrice T 6-Feb-16 17:33pm    
What is your algorithm exactly ?
Samuel Shokry 6-Feb-16 22:09pm    
0/1 knapsack

1 solution

I think it is time for you to stop guessing what your code is doing. It is time to see your code executing and ensuring that it does what you expect.

The debugger is your friend. It will show you what your code is really doing.
Follow the execution step by step, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Your problem is about exploring all combinations of parties. Another way to see it is about exploring a binary tree. It is also related to back-tracking.
Sorting the list of parties can only be about optimization.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900