zoukankan      html  css  js  c++  java
  • E

    E - Ac Challenge

    一个比较简单的 状压dp

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long long ll;
    const int N = 21;
    
    ll f[1 << N], a[N], b[N];
    vector<int>G[N];
    int n, m;
    bool judge(int state, int pos) {
    	// 判断state状态加入pos是否河里
    	if (f[state] == -1 or (state & (1 << pos))) return false;
    	for (int v : G[pos]) {
    		if (not (state & (1 << v)))return false;
    	}
    	return true;
    }
    int count(int x) {
    	int ans = 0;
    	while (x)ans++, x -= (-x) & x;
    	return ans;
    }
    int main() {
    	scanf("%d", &n);
    	for (int i = 0; i < n; i++) {
    		scanf("%lld%lld%d", a + i, b + i, &m);
    		while (m--) {
    			int x; scanf("%d", &x);
    			G[i].push_back(x-1);
    		}
    	}
    	ll ans = 0;
    	memset(f, 0xff, sizeof f);
    	f[0] = 0;
    	for (int i = 0; i < 1 << n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (not judge(i, j))continue;
    			f[i | (1 << j)] = max(f[i | (1 << j)], f[i] + (1 + count(i)) * a[j] + b[j]);
    			ans = max(ans, f[i | (1 << j)]);
    		}
    	}
    	printf("%lld
    ", ans);
    }
    
  • 相关阅读:
    sql 大数据量 的分表操作
    NHibernate 2.0 配置
    NHibernate 之调用存储过程
    k-v-o 扫盲
    320学习笔记 2
    GCD之dispatch queue
    使用KVO体会
    timer,runloop,thread,task小总结
    Run Loop
    iPh oto的删除动画
  • 原文地址:https://www.cnblogs.com/sduwh/p/14070954.html
Copyright © 2011-2022 走看看