hdu3535题解
hdu3535:http://acm.hdu.edu.cn/showproblem.php?pid=3535
该题是非常全面的一道分组背包问题。其实理解了最多一个的分组背包问题,解题起来也是很简单的。但是该题卡了我3天,真让人郁闷。就是因为一个case过不了,里面的时间ci可以为0!!!测试数据中竟然有0分钟这种事情!!!
题解:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
int dp[][+];
typedef struct Job
{
int ci;
int gi;
}Job;
int main()
{
int n,t;
while(~scanf("%d%d",&n,&t))
{
int s[n];
vector<Job> vjob[n];
for(int i=;i<n;++i)
{
int m;
scanf("%d%d",&m,&s[i]);
for(int j=;j<m;++j)
{
Job job;
scanf("%d%d",&job.ci,&job.gi);
vjob[i].push_back(job);
}
}
memset(dp,,sizeof(dp));
for(int i=;i<n;++i)
{
memcpy(dp[],dp[],sizeof(dp[]));
//最少1
if(s[i]==)
{
memset(dp[],-,sizeof(dp[]));
for(int j=;j<vjob[i].size();++j)
{
for(int v=t;v>=;--v)
{
int ci = vjob[i][j].ci;
int gi = vjob[i][j].gi;
if(v >= ci)
{
if(dp[][v-ci] != -)
dp[][v] = max(dp[][v],dp[][v-ci]+gi);
if(dp[][v-ci] != -)
dp[][v] = max(dp[][v],dp[][v-ci]+gi);
}
}
}
}
//最多1
else if(s[i]==)
{
for(int v=t;v>=;--v)
{
for(int j=;j<vjob[i].size();++j)
{
int ci = vjob[i][j].ci;
int gi = vjob[i][j].gi;
if(v >= ci)
{
if(dp[][v-ci] != - && ci != )
{
dp[][v] = max(dp[][v],dp[][v-ci]+gi);
}
else if(ci== && dp[][v] != -)
{
//测试数据中竟然有0分钟这种事情!!!
dp[][v] = max(dp[][v],dp[][v]+gi);
}
}
}
}
}
//随意
else if(s[i]==)
{
for(int j=;j<vjob[i].size();++j)
{
for(int v=t;v>=;--v)
{
int ci = vjob[i][j].ci;
int gi = vjob[i][j].gi;
if(v >= ci)
{
if(dp[][v-ci] != -)
dp[][v] = max(dp[][v],dp[][v-ci]+gi);
}
}
}
}
}
printf("%d\n",dp[][t]);
}
return ;
}
hdu3535题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- [luoguP3047] [USACO12FEB]附近的牛Nearby Cows(DP)
传送门 dp[i][j][0] 表示点 i 在以 i 为根的子树中范围为 j 的解 dp[i][j][1] 表示点 i 在除去 以 i 为根的子树中范围为 j 的解 状态转移就很好写了 ——代码 #i ...
- Codeforces 158B (数学)
B. Mushroom Scientists time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- sql自增长和占位符?"相矛盾"的问题
1.对于sql server数据当数据被定义为自增长时,插入,无法将那个位置用字符占位,我们可以使用部分插入的方法来做. insert into users (username,email,grad ...
- NYOJ5 Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- Stockbroker Grapevine POJ 1125 Floyd
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37069 Accepted: ...
- react实现ssr服务器端渲染总结和案例(实例)
1.什么是 SSR SSR 是 server side render 的缩写,从字面上就可以理解 在服务器端渲染,那渲染什么呢,很显然渲染现在框架中的前后端分离所创建的虚拟 DOM 2.为什么要实现服 ...
- Redhat 6.1安装ArcGIS Server10.1
http://blog.csdn.net/linghe301/article/details/7762985 操作环境:Redhat 6.1 Linux 安装ArcGIS Server10.1之前,还 ...
- [Vue-rx] Watch Vue.js v-models as Observable with $watchAsObservable and RxJS
You most likely already have data or properties in your template which are controlled by third-party ...
- 嵌入式C语言经常使用keyword
1.statickeyword 这个keyword前面也有提到.它的作用是强大的. 要对statickeyword深入了解.首先须要掌握标准C程序的组成. 标准C程序一直由下列部分组成: ...
- LNMP环境搭建——PHP篇
一.源代码安装 1.编译安装 ./configure --prefix=/usr/local/php\ --with-config-file-path=/usr/local/php/etc --wit ...