C. Coin Troubles 有依赖的背包 + 完全背包变形
http://codeforces.com/problemset/problem/283/C
一开始的时候,看着样例不懂,为什么5 * a1 + a3不行呢?也是17啊
原来是,题目要求硬币数目a3 > a4 > a2,那么,不选的话,是不合法的。就是0、0、0这样是不合法的,因为a3 = a4了。
那么就可以知道, a3起码都要选两个了。
那么怎么维护这个关系呢?,思路是有依赖的背包,需要a3的数目比a4的多,就可以把a4那件物品变成a3 + a4
那么每一次选择的时候,就隐含地选了一件a3了。当然,前提是起码已经选了2件a3、1件a4、0件a2
然后把需要求的val减去一定要选的东西后,做一个完全背包就好了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
int fa[maxn], a[maxn];
bool out[maxn];
int n, q, val;
struct node {
int u, v, tonext;
}e[maxn * ];
int first[maxn];
int num;
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
bool vis[maxn];
void dfs(int cur) {
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) {
cout << << endl;
exit();
}
vis[v] = true;
dfs(v);
vis[v] = false;
}
}
void calc(int cur, int ti) {
if (cur == ) return;
val -= a[cur] * ti;
if (val < ) { //不能等于
cout << << endl;
exit();
}
calc(fa[cur], ti + );
a[cur] += a[fa[cur]];
}
int dp[ + ];
const int MOD = 1e9 + ;
void work() {
cin >> n >> q >> val;
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
for (int i = ; i <= q; ++i) {
int u, v;
cin >> u >> v;
fa[v] = u;
out[u] = true;
add(u, v);
}
for (int i = ; i <= n; ++i) {
// if (vis[i]) continue;
vis[i] = true;
dfs(i);
vis[i] = false;
}
for (int i = ; i <= n; ++i) {
if (out[i]) continue;
calc(i, );
}
dp[] = ;
for (int i = ; i <= n; ++i) {
for (int j = a[i]; j <= val; ++j) {
dp[j] += dp[j - a[i]];
if (dp[j] >= MOD) dp[j] %= MOD;
}
}
cout << dp[val] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
IOS;
work();
return ;
}
C. Coin Troubles 有依赖的背包 + 完全背包变形的更多相关文章
- 【题解】284E. Coin Troubles(dp+图论建模)
[题解]284E. Coin Troubles(dp+图论建模) 题意就是要你跑一个完全背包,但是要求背包的方案中有个数相对大小的限制 考虑一个\(c_i<c_j\)的限制,就是一个\(c_i\ ...
- POJ 3260 多重背包+完全背包
前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...
- 背包!背包!HDU 2602 Bone Collector + HDU 1114 Piggy-Bank + HDU 2191 512
http://acm.hdu.edu.cn/showproblem.php?pid=2602 第一题 01背包问题 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- 【poj3260-最少找零】多重背包+完全背包
多重背包+完全背包. 买家:多重背包:售货员:完全背包: 开两个数组,分别计算出买家,售货员每个面额的最少张数. 最重要的是上界的处理:上界为maxw*maxw+m(maxw最大面额的纸币). (网上 ...
- HDU 3591 The trouble of Xiaoqian(多重背包+全然背包)
HDU 3591 The trouble of Xiaoqian(多重背包+全然背包) pid=3591">http://acm.hdu.edu.cn/showproblem.php? ...
- POJ 3260 The Fewest Coins(多重背包+全然背包)
POJ 3260 The Fewest Coins(多重背包+全然背包) http://poj.org/problem?id=3260 题意: John要去买价值为m的商品. 如今的货币系统有n种货币 ...
- B 维背包+完全背包 Hdu2159
<span style="color:#3333ff;">/* ---------------------------------------------------- ...
- dp--01背包,完全背包,多重背包
背包问题 以下代码 n是物品个数,m是背包容积 物品价值和重量int v[maxn],w[maxn]; 01背包 模板 for(int i = 0; i < n; i++) { for(int ...
- codeforces 284 E. Coin Troubles(背包+思维)
题目链接:http://codeforces.com/contest/284/problem/E 题意:n种类型的硬币,硬币的面值可能相同,现在要在满足一些限制条件下求出,用这些硬币构成t面值的方案数 ...
随机推荐
- csu - 1536: Bit String Reordering (模拟)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...
- nyoj_10_skiing_201405181748
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- Delphi:校验手机号及身份证号
//校验手机号 function IsMobileNumber( num:string ):boolean; begin Result:=False; if length( tr ...
- CSS类选择器
CSS 选择器参考手册 还是 .class #id element 用的最多! 在 CSS 中,选择器是一种模式,用于选择需要添加样式的元素. "CSS" 列指 ...
- Ubuntu 16.04安装Markdown编辑器MarkMyWords
继上一篇文章http://www.cnblogs.com/EasonJim/p/7119345.html中使用Sublime Text 3进行Markdown的编辑,总觉得操作上比较繁琐,现在推荐使用 ...
- MongoDB C#驱动
烟波钓徒 MongoDB C#驱动 http://www.mongodb.org/display/DOCS/CSharp+Driver+Tutorial 笔记 首先下载驱动.驱动有两个文件 Mongo ...
- TensorFlow-GPU环境配置之三——安装bazel
TensorFlow的源码需要使用bazel进行编译,所以需要安装bazel构建工具 1.安装JDK 8 sudo add-apt-repository ppa:webupd8team/java su ...
- 【nginx】【转】正向代理与反向代理的区别[
转自: http://blog.csdn.net/m13666368773/article/details/8060481 正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板,简单 ...
- 使用Maven对JAVA程序打包-带主类、带依赖
使用Maven对JAVA程序打包-带主类.带依赖 http://blog.csdn.net/strongyoung88/article/details/54097830
- kvm 安装
一. 虚拟化 是指通过虚拟化技术将一台计算机虚拟为多台逻辑计算机.在一台计算机上同时运行多个逻辑计算机,每个逻辑计算机可运行不同的操作系统,并且应用程序都可以在相互独立的空间内运行而互相不影响,从而 ...