首页 技术 正文
技术 2022年11月23日
0 收藏 719 点赞 2,487 浏览 2269 个字

[HAOI2010]软件安装

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 2029  Solved: 811
[Submit][Status][Discuss]

Description

现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi。我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大)。

但是现在有个问题:软件之间存在依赖关系,即软件i只有在安装了软件j(包括软件j的直接或间接依赖)的情况下才能正确工作(软件i依赖软件j)。幸运的是,一个软件最多依赖另外一个软件。如果一个软件不能正常工作,那么它能够发挥的作用为0。

我们现在知道了软件之间的依赖关系:软件i依赖软件Di。现在请你设计出一种方案,安装价值尽量大的软件。一个软件只能被安装一次,如果一个软件没有依赖则Di=0,这时只要这个软件安装了,它就能正常工作。

Input

第1行:N, M  (0<=N<=100, 0<=M<=500)
      第2行:W1, W2, … Wi, …, Wn (0<=Wi<=M )
      第3行:V1, V2, …, Vi, …, Vn  (0<=Vi<=1000 )
      第4行:D1, D2, …, Di, …, Dn (0<=Di<=N, Di≠i )

Output

一个整数,代表最大价值。

Sample Input

3 10
5 5 6
2 3 4
0 1 1

Sample Output

5

HINT

 树形,dp依赖型的,算完点,是一棵树对吧,然后就从根向下dp数据范围如此之小。f[i][j]表示i这个点,可以用安装连续几个点的方案数。 

 #include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstdio> #define N 107
#define M 507
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
} int n,m,cnt,scc,ind,top;
int v[N],w[N];
int sv[N],sw[N];
int dfn[N],low[N],belong[N];
int q[N],f[N][M],in[M];
struct edge{
int to,next;
}e[M],ed[M];int last[N],last2[N];
bool inq[N]; void insert(int u,int v){e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;}
void insert2(int u,int v)
{
in[v]=;
ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
}
void tarjan(int x)
{
int now=;
low[x]=dfn[x]=++ind;
q[++top]=x;inq[x]=;
for(int i=last[x];i;i=e[i].next)
if(!dfn[e[i].to])
{
tarjan(e[i].to);
low[x]=min(low[x],low[e[i].to]);
}
else if(inq[e[i].to])
low[x]=min(low[x],dfn[e[i].to]);
if(low[x]==dfn[x])
{
scc++;
while(now!=x)
{
now=q[top--];inq[now]=;
belong[now]=scc;
sv[scc]+=v[now];
sw[scc]+=w[now];
}
}
}
void rebuild()
{
cnt=;
for(int x=;x<=n;x++)
for(int i=last[x];i;i=e[i].next)
if(belong[e[i].to]!=belong[x])
insert2(belong[x],belong[e[i].to]);
}
void dp(int x)
{
for(int i=last2[x];i;i=ed[i].next)
{
dp(ed[i].to);
for(int j=m-sw[x];j>=;j--)
{
for(int k=;k<=j;k++)
f[x][j]=max(f[x][j],f[x][k]+f[ed[i].to][j-k]);
}
}
for(int j=m;j>=;j--)
{
if(j>=sw[x])f[x][j]=f[x][j-sw[x]]+sv[x];
else f[x][j]=;
}
}
int main()
{
n=read();m=read();
for(int i=;i<=n;i++) w[i]=read();
for(int i=;i<=n;i++) v[i]=read();
for(int i=;i<=n;i++)
{
int x=read();
if(x)insert(x,i);
}
for(int i=;i<=n;i++)
if(!dfn[i])tarjan(i);
rebuild();
for(int i=;i<=scc;i++)
if(!in[i])
insert2(scc+,i);
dp(scc+);
printf("%d\n",f[scc+][m]);
}

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,022
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,359
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,142
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,773
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,851