uva 10817
2014-05-12 21:31
295 查看
Problem D: Headmaster's Headache |
Time limit: 2 seconds |
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAX_N = 105; const int INF = 7000000; int S, M, N; int ns1 = 0 , ns2 = 0; int s[MAX_N], prize[MAX_N]; int dp[1 << 9][1 << 9]; void solve() { dp[ns1][ns2] = prize[0]; dp[ns2][ns1] = prize[0]; for(int i = 1; i <= N; ++i) { for(int s1 = (1 << S) - 1; s1 >= 0; --s1) { for(int s2 = (1 << S) - 1; s2 >= 0; --s2) { if(dp[s1][s2] != INF) { dp[s1 | s[i]][s1 & s[i] | s2] = min(dp[s1 | s[i]][s1 & s[i] | s2] , dp[s1][s2] + prize[i]); dp[s2 & s[i] | s1][s2 | s[i]] = min(dp[s2 & s[i] | s1][s2 | s[i]] , dp[s1][s2] + prize[i]); } } } } } int main() { //freopen("sw.in","r",stdin); while(~scanf("%d%d%d", &S, &M, &N) && (S + M + N)) { ns1 = 0; ns2 = 0; for(int i = 0; i < (1 << S); ++i) for(int j = 0; j < (1 << S); ++j) dp[i][j] = INF; memset(prize, 0, sizeof(prize)); memset(s, 0, sizeof(s)); for(int i = 1; i <= M; ++i) { int ch, v; char c; scanf("%d%d%c",&v, &ch, &c); prize[0] += v; ns2 |= ns1 & (1 << (ch - 1)); ns1 |= 1 << (ch - 1); while(c != '\n') { scanf("%d%c", &ch, &c); ns2 |= ns1 & (1 << (ch - 1)); ns1 |= 1 << (ch - 1); } } for(int i = 1; i <= N; ++i) { int ch; char c; scanf("%d%d%c", &prize[i], &ch, &c); s[i] |= 1 << (ch - 1); while(c != '\n') { scanf("%d%c", &ch, &c); s[i] |= 1 << (ch - 1); } } solve(); printf("%d\n", dp[(1 << S) - 1][(1 << S) - 1]); } return 0; }
View Code
相关文章推荐
- DM642的优化
- 其实苹果已配备手柄 若Apple TV成为主机
- uva11292 Dragon of Loowater 水题
- Referenced file contains errors (http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd)
- poj2661 Factstone Benchmark 数学
- WEBSOKET服务器搭建
- Next SIEM
- 备忘录模式
- java面试题之 哈希是什么?为什么哈希存取比较快?
- 向量几何在游戏编程中的使用2
- SQL基础-->层次化查询(START BY ... CONNECT BY PRIOR)
- POJ 1190 生日蛋糕(DFS:优化剪枝)
- 游戏开发中的一些基本方法(转载)
- poj3628 Bookshelf 2 DP 01背包
- Javascript 面向对象编程(一):封装
- Java多线程(3)
- ANE原生代码的调试(安卓)
- alloca 函数
- Oracle SQL性能优化
- poj3624 Charm Bracelet DP 01背包问题