Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

有向图的DFS,没剪枝,0ms

Posted by forgetEver at 2012-09-22 17:42:39 on Problem 1062
#include <stdio.h>
#include <string.h>
static const int nmax = 104;

typedef struct item_s
{
    int price;
    int level;
    int x;
    int subsSet[nmax];
    int discount[nmax];
}Item;

Item items[nmax];
int  value[nmax];
int  m, n;

int choosen[nmax];
int maxlevel, minlevel;
int bestValue;
//int currValue;
// use dfs first
int dfs(int idx)
{
    int bakmax = maxlevel, bakmin = minlevel;
    int res = value[idx];
    for (int i = 0; i < items[idx].x; ++i)
    {
        int randno = items[idx].subsSet[i];
        int level = items[randno].level;
        if (level > maxlevel)  maxlevel = level;
        if (level < minlevel)  minlevel = level;
        if (choosen[randno] == 0 && (maxlevel - minlevel) <= m)
        {
            choosen[randno] = 1;
            int candiate = dfs(randno) + items[idx].discount[i];
            if (candiate < res) {
                res = candiate;
                if (idx == 1)
                    bestValue = res;
            }
            choosen[randno] = 0;
        }
        maxlevel = bakmax;
        minlevel = bakmin;
    }
    return res;
}


int main()
{
    while (scanf("%d%d", &m, &n) == 2)
    {
        for (int i = 1; i <= n; ++i)
        {
            scanf("%d%d%d", &items[i].price, &items[i].level, &items[i].x);
            for (int k = 0; k < items[i].x; ++k)
            {
                scanf("%d%d", &items[i].subsSet[k], &items[i].discount[k]);
            }
            value[i] = items[i].price;
        }
        memset(choosen, 0x00, sizeof(choosen));
        maxlevel = items[1].level;
        minlevel = items[1].level;
        choosen[1] = 1;
        bestValue = value[1];
        dfs(1);
        printf("%d\n", bestValue);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator