Codeforces 417D Cunning Gena(状态压缩dp)
2014-04-19 13:27
316 查看
题目链接:Codeforces 417D Cunning Gena
题目大意:n个小伙伴,m道题目,每个监视器b花费,给出n个小伙伴的佣金,所需要的监视器数,以及可以完成的题目序号。注意,这里只要你拥有的监视器数量大于小伙伴需要的监视器数量即可。求最少花费多少金额可以解决所有问题。
解题思路:dp[i],i为一个二进制数,表示完成这些题目的最小代价,但是这里要注意,因为有个监视器的数量,一般情况下要开一个二维的状态,但是2^20次方有一百万,再多一维的数组会超内存,所以我的做法是将每个小伙伴按照监视器的数量从小到达排序,慢慢向上加。
#include <cstdio>
#include <cstring>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = (1<<20)+5;
const int M = 105;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct state {
int s;
ll k, val;
}p[M];
int n, m;
ll b, dp
;
bool cmp (const state& a, const state& b) {
return a.k < b.k;
}
void init () {
memset(dp, -1, sizeof(dp));
scanf("%d%d", &n, &m);
cin >> b;
int t, a;
for (int i = 0; i < n; i++) {
cin >> p[i].val >> p[i].k >> t;
p[i].s = 0;
for (int j = 0; j < t; j++) {
scanf("%d", &a);
p[i].s |= (1<<(a-1));
}
}
sort(p, p + n, cmp);
}
ll solve () {
dp[0] = 0;
int t = (1<<m)-1;
ll ans = INF;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= t; j++) {
if (dp[j] == -1) continue;
int u = p[i].s | j;
if (dp[u] == -1)
dp[u] = p[i].val + dp[j];
else
dp[u] = min(dp[u], p[i].val + dp[j]);
}
if (dp[t] != -1)
ans = min(ans, dp[t] + p[i].k * b);
}
return ans == INF ? -1 : ans;
}
int main () {
init ();
cout << solve() << endl;
return 0;
}
题目大意:n个小伙伴,m道题目,每个监视器b花费,给出n个小伙伴的佣金,所需要的监视器数,以及可以完成的题目序号。注意,这里只要你拥有的监视器数量大于小伙伴需要的监视器数量即可。求最少花费多少金额可以解决所有问题。
解题思路:dp[i],i为一个二进制数,表示完成这些题目的最小代价,但是这里要注意,因为有个监视器的数量,一般情况下要开一个二维的状态,但是2^20次方有一百万,再多一维的数组会超内存,所以我的做法是将每个小伙伴按照监视器的数量从小到达排序,慢慢向上加。
#include <cstdio>
#include <cstring>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int N = (1<<20)+5;
const int M = 105;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct state {
int s;
ll k, val;
}p[M];
int n, m;
ll b, dp
;
bool cmp (const state& a, const state& b) {
return a.k < b.k;
}
void init () {
memset(dp, -1, sizeof(dp));
scanf("%d%d", &n, &m);
cin >> b;
int t, a;
for (int i = 0; i < n; i++) {
cin >> p[i].val >> p[i].k >> t;
p[i].s = 0;
for (int j = 0; j < t; j++) {
scanf("%d", &a);
p[i].s |= (1<<(a-1));
}
}
sort(p, p + n, cmp);
}
ll solve () {
dp[0] = 0;
int t = (1<<m)-1;
ll ans = INF;
for (int i = 0; i < n; i++) {
for (int j = 0; j <= t; j++) {
if (dp[j] == -1) continue;
int u = p[i].s | j;
if (dp[u] == -1)
dp[u] = p[i].val + dp[j];
else
dp[u] = min(dp[u], p[i].val + dp[j]);
}
if (dp[t] != -1)
ans = min(ans, dp[t] + p[i].k * b);
}
return ans == INF ? -1 : ans;
}
int main () {
init ();
cout << solve() << endl;
return 0;
}
相关文章推荐
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
- CodeForces - 165E(状态压缩dp)
- (状态压缩dp)Codeforces 543 C-Remembering Strings
- Codeforces 11D A Simple Task (状态压缩DP)
- codeforces 543C C. Remembering Strings(状态压缩dp)
- codeforces 580D Kefa and Dishes【状态压缩+dp】
- Codeforces,Kefa and Dishes,状态压缩DP
- codeforces 321# D. Kefa and Dishes (状态压缩DP)
- Codeforces 417D Cunning Gena(状态压缩dp)
- codeforces 424E Colored Jenga (状态压缩,概率dp用hash记忆优化搜索)
- CodeForces 21D - Traveling Graph 欧拉回路的性质+状态压缩DP
- Codeforces 8C Looking for Order(状态压缩DP)
- codeforces 453B Little Pony and Harmony Chest (离散化+dp状态压缩)
- CodeForces 903F Clear The Matrix (状态压缩dp)
- codeforces 152E Garden(状态压缩dp)
- CodeForces 11D - A Simple Task 状态压缩DP
- CodeForces 8C - Looking for Order 状态压缩DP..DP降低复杂度
- codeforces 285 D Permutation Sum (状态压缩DP)
- codeforces 16E Fish(状态压缩概率DP)
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers