Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
有向图的DFS,没剪枝,0ms#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator