链接:https://www.nowcoder.com/acm/contest/140/B
来源:牛客网

题目描述

White Rabbit wants to buy some drinks from White Cloud.
There are n kinds of drinks, and the price of i-th drink is p[i] yuan per bottle.
Since White Cloud is a good friend of White Rabbit, when White Rabbit buys a bottle of i-th drink, White Rabbit can choose only one of the following two discounts :
1.White Rabbit can get a d[i](d[i]<=p[i]) yuan discount. Specifically, White Rabbit only need to pay p[i]-d[i] yuan.
2.White Rabbit can buy a bottle of f[i]-th drink for free(than bonus drink can't use any discount).
White Rabbit wants to have at least a bottle of i-th drink for each i between 1 to n. You need to tell White Rabbit what is the minimal cost.

输入描述:

The first line of input contains an integer n(n<=100000)
In the next line,there are n integers p[1..n] in range [0,1000000000].
In the next line,there are n integers d[1..n] in range [0,1000000000].(d[i]<=p[i])
In the next line,there are n integers f[1..n] in range [1,n].

输出描述:

Print the minimum cost.
示例1

输入

复制

3
10 3 5
5 0 5
1 3 2

输出

复制

8

分析:考虑被赠送的商品->商品,这些商品的赠送关系就形成了基环树森林;
   不考虑环,环外树形dp,dp[i][0]表示购买i的子树最小代价,dp[i][1]表示购买i的子树且i以原价购买(考虑到对父亲的赠送);
   那么dp[i][1]可以直接由儿子的dp[j][0]和自身的原价更新到;
   dp[i][0]有两种情况,自身被赠送得来或不赠送得来而已;
   考虑环上,需要断环为链进行dp,记为g[i][0]和g[i][1],其中g[i]与dp[i]同理;
   需要注意的是环上的第一件商品要分是否由最后一件商品赠送而来;
代码:
  
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+,mod=1e9+,inf=0x3f3f3f3f;
int n,m,k,t,p[maxn],d[maxn],pr[maxn],cir[maxn],tot,vis[maxn];
long long dp[maxn][],g[maxn][];
bool iscir[maxn];
vector<int>e[maxn];
void dfs(int x)
{
if(vis[x]==)
{
int pos=x;
while()
{
cir[++tot]=pos;
iscir[pos]=true;
pos=pr[pos];
if(pos==x)return;
}
}else if(vis[x]==)return;
vis[x]=;
for(auto y:e[x])pr[y]=x,dfs(y);
vis[x]=;
}
void dfs1(int x)
{
dp[x][]=p[x]-d[x];
dp[x][]=p[x];
long long cnt1=;
long long cnt2=1e18;
for(auto y:e[x])
{
if(iscir[y])continue;
dfs1(y);
dp[x][]+=dp[y][];
dp[x][]+=dp[y][];
}
for(auto y:e[x])
{
if(iscir[y])continue;
dp[x][]=min(dp[x][],dp[x][]-p[x]-dp[y][]+dp[y][]);
}
}
int main()
{
int i,j;
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(i=;i<=n;i++)scanf("%d",&p[i]);
for(i=;i<=n;i++)scanf("%d",&d[i]);
for(i=;i<=n;i++)scanf("%d",&j),e[j].push_back(i);
long long ret=;
for(j=;j<=n;j++)
{
if(vis[j])continue;
tot=;
dfs(j);
if(!tot)continue;
for(i=;i<=tot;i++)dfs1(cir[i]);
if(tot==){ret+=dp[cir[]][];continue;}
g[][]=dp[cir[]][],g[][]=dp[cir[]][];
for(i=;i<=tot;i++)
{
g[i][]=min(g[i-][]+dp[cir[i]][],g[i-][]+dp[cir[i]][]-p[cir[i]]);
g[i][]=dp[cir[i]][]+g[i-][];
}
long long cur=g[tot][];
g[][]=dp[cir[]][]-p[cir[]];
g[][]=1e18;
for(i=;i<=tot;i++)
{
g[i][]=min(g[i-][]+dp[cir[i]][],g[i-][]+dp[cir[i]][]-p[cir[i]]);
g[i][]=dp[cir[i]][]+g[i-][];
}
ret+=min(cur,g[tot][]);
}
printf("%lld\n",ret);
return ;
} 

牛客网暑期ACM多校训练营(第二场)B discount的更多相关文章

  1. 牛客网暑期ACM多校训练营 第九场

    HPrefix Sum study from : https://blog.csdn.net/mitsuha_/article/details/81774727 k较小.分离x和k. 另外的可能:求a ...

  2. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  3. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  4. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  5. 牛客网 暑期ACM多校训练营(第一场)A.Monotonic Matrix-矩阵转化为格子路径的非降路径计数,Lindström-Gessel-Viennot引理-组合数学

    牛客网暑期ACM多校训练营(第一场) A.Monotonic Matrix 这个题就是给你一个n*m的矩阵,往里面填{0,1,2}这三种数,要求是Ai,j⩽Ai+1,j,Ai,j⩽Ai,j+1 ,问你 ...

  6. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  7. 2018牛客网暑期ACM多校训练营(第二场)I- car ( 思维)

    2018牛客网暑期ACM多校训练营(第二场)I- car 链接:https://ac.nowcoder.com/acm/contest/140/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 ...

  8. 牛客网暑期ACM多校训练营(第七场)Bit Compression

    链接:https://www.nowcoder.com/acm/contest/145/C 来源:牛客网 题目描述 A binary string s of length N = 2n is give ...

  9. 牛客网暑期ACM多校训练营(第一场) - J Different Integers(线段数组or莫队)

    链接:https://www.nowcoder.com/acm/contest/139/J来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 524288K,其他语言1048 ...

  10. 牛客网暑期ACM多校训练营(第九场) A题 FWT

    链接:https://www.nowcoder.com/acm/contest/147/A来源:牛客网 Niuniu has recently learned how to use Gaussian ...

随机推荐

  1. 51nod 1119 机器人走方格 V2

    1119 机器人走方格 V2  基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少 ...

  2. 51nod 1186 质数检测 V2

    1186 质数检测 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题  收藏  关注 给出1个正整数N,检测N是否为质数.如果是,输出"Yes&quo ...

  3. That Nice Euler Circuit UVALive - 3263 || 欧拉公式

    欧拉定理: 简单多面体的顶点数V.棱数E及面数F间有关系有著名的欧拉公式:V-E+F=2. 设G为任意的连通的平面图,则v-e+f=2,v是G的顶点数,e是G的边数,f是G的面数.(引) 证明(?) ...

  4. 转】用Hadoop构建电影推荐系统

    原博文出自于: http://blog.fens.me/hadoop-mapreduce-recommend/ 感谢! 用Hadoop构建电影推荐系统 Hadoop家族系列文章,主要介绍Hadoop家 ...

  5. 纯css实现的三级水平导航菜单

    vscode练习使用开发纯css的三级水平导航菜单.先上图: 1.html5布局 <html> <head> <meta charset="UTF-8" ...

  6. 免费公共DNS服务器IP地址大全(2017年6月24日)

    收集全球各个常用公共DNS服务器 IP地址,欢迎各位朋友评论补充! 国内常用公共DNS 114 DNS: (114.114.114.114:    114.114.115.115) 114DNS安全版 ...

  7. (转)淘淘商城系列——分布式文件系统FastDFS

    http://blog.csdn.net/yerenyuan_pku/article/details/72801777 商品添加的实现,包括商品的类目选择,即商品属于哪个分类?还包括图片上传,对于图片 ...

  8. leetcode_Stone Game_dp_思维

    Alex和Lee玩游戏,共有偶数堆石头,石头总数为奇数,两人每次要么拿第一堆,要么拿最后一堆,两人以最优策略拿石堆(一次拿走完整的一堆),Alex先手,Alex赢返回True,否则返回False. 思 ...

  9. Jmeter之WebService接口测试

    一.简介  1.JMeter3.2前的版本,可以使用SOAP/XML-RPC Request插件直接进行webservice接口,而3.2后的版本则已经取消了这个接口,需要另外的方法才能进行测试. 2 ...

  10. OpenMP入门教程(一)

    什么是OpenMP Open Multi-Processing的缩写,是一个应用程序接口(API),可用于显式指导多线程.共享内存的并行性. 在项目程序已经完成好的情况下不需要大幅度的修改源代码,只需 ...