1. 程式人生 > >【HDU 5887】Herbs Gathering(dfs+時間剪枝)

【HDU 5887】Herbs Gathering(dfs+時間剪枝)

題目大意:
n個物體 每個物品有體積和價值,取V體積問最大價值。

揹包,不過資料很大……
然後……先寫個暴力的dfs,TLE
然後按價效比排個序,TLE
然後加各種剪枝,TLE

賽後(clock()-st)/CLOCKS_PER_SEC <= 0.02 st為dfs前取的clock()
跑的比誰都快……不想說話。。

程式碼如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <climits>
#include <ctime> #include <cstring> #include <queue> #include <stack> #include <list> #include <algorithm> #include <map> #include <set> #define LL long long #define Pr pair<int,int> #define fread(ch) freopen(ch,"r",stdin) #define fwrite(ch) freopen(ch,"w",stdout)
using namespace std; const int INF = 0x3f3f3f3f; const int msz = 10000; const int mod = 1e9+7; const double eps = 1e-8; struct Obj { LL v,w; double xjb; bool operator < (const struct Obj a)const { return xjb > a.xjb; } } obj[111]; int n; LL v,mn; LL ans; double c; void
dfs(int pos,LL sum,LL v) { if((clock()-c)/CLOCKS_PER_SEC > 0.02) return; ans = max(ans,sum); if(sum+obj[pos].xjb*v < ans) return; if(v < mn) return; if(pos == n) return; if(obj[pos].v <= v) dfs(pos+1,sum+obj[pos].w,v-obj[pos].v); dfs(pos+1,sum,v); } int main() { //fread(""); //fwrite(""); while(~scanf("%d%lld",&n,&v)) { mn = 1e9+7; for(int i = 0; i < n; ++i) { scanf("%lld%lld",&obj[i].v,&obj[i].w); obj[i].xjb = obj[i].w*1.0/obj[i].v; mn = min(mn,obj[i].v); } sort(obj,obj+n); ans = 0; c = clock(); dfs(0,0,v); printf("%lld\n",ans); } return 0; }