设为首页 加入收藏

TOP

ZOJ 3812 We Need Medicine 01背包+位优化
2015-07-20 17:43:14 来源: 作者: 【 】 浏览:1
Tags:ZOJ 3812 Need Medicine 背包 +位 优化

题目链接:点击打开链接

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       #include 
       typedef unsigned long long ll; using namespace std; const int N = 400 + 1; const int M = 200000 + 1; int t[N], w[N], g[51][M], ans[N], dep; ll d[M]; map
       
         id; char ch; void get(int& z) { while ((ch = getchar()) > '9' || ch < '0'); z = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') z = z * 10 + ch - '0'; } int main() { for (ll i = 1; i <= 50; ++i) id[1ll << i] = i; int cas, n, m, x, y; ll v, mx = (1ll << 51) - 1; get(cas); while (cas -- > 0) { memset(d, 0, sizeof d); d[0] = 1; memset(g, -1, sizeof g); get(n); get(m); for (int i = 0; i < n; ++i) { get(w[i]); get(t[i]); for (int j = M - t[i] - 1; j >= 0; --j) if (d[j] > 0) { v = d[j + t[i]]; d[j + t[i]] |= (d[j] << w[i]) & mx; v = v ^ d[j + t[i]]; while (v > 0) { if (v & 1) { v = v & (v - 1); continue; } x = id[v & (v - 1) ^ v]; g[x][j + t[i]] = i; v = v & (v - 1); } } //g[w[i]][t[i]] = i; } while (m -- > 0) { get(x); get(y); if (g[x][y] >= 0) { dep = 0; while (x > 0 && y > 0) { ans[dep++] = g[x][y]; x -= w[ans[dep - 1]]; y -= t[ans[dep - 1]]; } for (int i = dep - 1; i >= 0; --i) { if (i == 0) printf("%d\n", ans[i] + 1); else printf("%d ", ans[i] + 1); } } else puts("No solution!"); } } return 0; }
       
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 3233 Matrix Power Series 下一篇HDOJ 4915 Multiplication table

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)