【hdu3080】01背包(容量10^7)
【题意】n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v)。n<=50,v<=10^7
【题解】
先用并查集找出各个联通块。
这题主要就是v太大了,跟以往的背包不同。
我们回想01背包,f[j+v[i]]=max(f[j]+w[i]);
在这里面很明显很多状态都没有用。
优化:如果有2个状态,v1<=v2 && w1>=w2 则(v2,w2)这个状态是没有用的。
我们回到滚动数组中:
f[i][j+v[i]]=max(f[i^1][j]+w[i]);
那么我们可以用两个优先队列,q0存以前的f[i-1],q1存当前的f[i]。
我们在求f[i]的时候,就把q0一个个pop出来,更新后放到q1里面去。
做完i,我们要把q1放到q0,然后做i+1。
在把q1放到q0的时候就把没用的状态去掉(对于(v,val),先按val从大到小排序,再按v从小到大排序,对同样的val取最小的v)。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; typedef long long LL;
const LL N=,INF=(LL)1e15;
LL n,m,fa[N],t[N],w[N],c[N][N];
struct node{LL v,val;}; struct cmp
{
bool operator () (node &x,node &y)
{
if(x.val==y.val) return x.v<y.v;
return x.val<y.val;
}
}; priority_queue<node,vector<node>,cmp> q0,q1; LL findfa(LL x)
{
if(fa[x]==x) return x;
return findfa(fa[x]);
} LL minn(LL x,LL y){return x<y ? x:y;} int main()
{
freopen("a.in","r",stdin);
freopen("me.out","w",stdout);
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=n;i++)
{
int num,xx;
scanf("%lld%lld%d",&t[i],&w[i],&num);
for(int j=;j<=num;j++)
{
scanf("%d",&xx);
fa[findfa(i)]=findfa(xx);
}
}
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
{
fa[i]=findfa(i);
c[fa[i]][++c[fa[i]][]]=i;
}
node x,p,f;
LL ans=INF;
for(int k=;k<=n;k++) if(c[k][])
{
// printf("k = %d\n",k);
while(!q0.empty()) q0.pop();
while(!q1.empty()) q1.pop();
x.v=;x.val=;
q0.push(x);
for(int j=;j<=c[k][];j++)
{
int i=c[k][j];
while(!q0.empty())
{
x=q0.top();q0.pop();q1.push(x);
f.v=x.v+t[i];
f.val=x.val+w[i];
if(f.v>=ans) continue;
if(f.val>=m) {ans=minn(ans,f.v);continue;}
q1.push(f);
}
LL mn=INF;
while(!q1.empty())
{
x=q1.top();q1.pop();
if(x.val>=m) ans=minn(ans,x.v);
if(x.v<mn) mn=x.v,q0.push(x);
// printf("v = %lld val = %lld\n",x.v,x.val);
}
}
// f[i^1][v+c[i]]=maxx(f[i^1][v+c[i]],f[i][v]+w[i]]);
}
if(ans<INF) printf("Case %d: %lld\n",++cas,ans);
else printf("Case %d: Poor Magina, you can't save the world all the time!\n",++cas);
}
return ;
}
【hdu3080】01背包(容量10^7)的更多相关文章
- HLOJ1366 Candy Box 动态规划(0-1背包改)
题目描述: 给出N个盒子(N<=100),每个盒子有一定数量的糖果(每个盒子的糖果数<=100),现在有q次查询,每次查询给出两个数k,m,问的是,如果从N个盒子中最多打开k个盒子(意思是 ...
- 2018.08.10 atcoder Median Sum(01背包)
传送门 题意简述:输入一个数组an" role="presentation" style="position: relative;">anan. ...
- HDU 3339 In Action【最短路+01背包模板/主要是建模看谁是容量、价值】
Since 1945, when the first nuclear bomb was exploded by the Manhattan Project team in the US, the n ...
- POJ3211 Washing Clothes[DP 分解 01背包可行性]
Washing Clothes Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 9707 Accepted: 3114 ...
- 【BZOJ-2427】软件安装 Tarjan + 树形01背包
2427: [HAOI2010]软件安装 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 960 Solved: 380[Submit][Status ...
- hdu2546 01背包
http://acm.split.hdu.edu.cn/showproblem.php?pid=2546 01背包问题,首先拿出5元买最贵的东西,那接下来就是背包容量m-5,物品数量n-1 的01背包 ...
- hdu 2955 01背包
http://acm.hdu.edu.cn/showproblem.php?pid=2955 如果认为:1-P是背包的容量,n是物品的个数,sum是所有物品的总价值,条件就是装入背包的物品的体积和不能 ...
- FOJProblem 2214 Knapsack problem(01背包+变性思维)
http://acm.fzu.edu.cn/problem.php?pid=2214 Accept: 4 Submit: 6Time Limit: 3000 mSec Memory Lim ...
- hdu 3466 排序01背包
也是好题,带限制的01背包,先排序,再背包 这题因为涉及到q,所以不能直接就01背包了.因为如果一个物品是5 9,一个物品是5 6,对第一个进行背包的时候只有dp[9],dp[10],…,dp[m], ...
随机推荐
- Java:static的作用分析
static表示“静态”或者“全局”的意思,但在Java中不能在所有类之外定义全局变量,只能通过在一个类中定义公用.静态的变量来实现一个全局变量. 一.静态变量 1. Java中存在两种变量,一种是s ...
- autofac无法解析一例
在asp.net mvc分项目开发中,如果类库位于其他的项目中,则必须在global中对其他项目的类库进行注册,否则会报“ None of the constructors found with 'A ...
- Scala function programming
1. Arbitrary multi parameters funcs sum(1,2,3,4,5) = sum(1 to 5: _*)the equal '=' can be ignored if ...
- 【APUE】Chapter14 Advanced I/O
14.1 Introduction 这一章介绍的内容主要有nonblocking I/O, record locking, I/O multiplexing, asynchronous I/O, th ...
- Android 上能提高学习工作效率的应用
在知乎上有朋友问 Android 上能提高学习.工作效率的应用有哪些?我给他们的推荐获得了最多赞同.以后会不断完善更新此贴. Any.do :规划日程,各平台都有. Evernote:记笔记,各平台都 ...
- YAGNI 声明
1.YAGNI介绍 YAGNI 全名是 You aren't Going to Need It,在你设计草案的初稿中,应该努力使用最简单可以工作的事物,直至程序的某个方面要求你添加额外的特性. 2.思 ...
- 【SpringCloud】第二篇: 服务消费者(rest+ribbon)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- (vue01)vue环境搭建
腾讯,百度,网易....这么大媒体平台咋老推送这么lower的信息? 你们不缺钱啊....我这么善良的孩子都别你们带坏了 强烈鄙视马化腾 强烈鄙视李彦宏 参考地址: https://segmentfa ...
- [leetcode-648-Replace Words]
In English, we have a concept called root, which can be followed by some other words to form another ...
- [转]juery-zTree的基本用法
[简介] zTree 是利用 jQuery 的核心代码,实现一套能完成大部分常用功能的 Tree 插件 兼容 IE.FireFox.Chrome 等浏览器 在一个页面内可同时生成多个 Tree 实例 ...