链接: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. 水题 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  2. glassfish应用服务器安装配置

    1.Glassfish4.0下载地址:https://glassfish.java.net/download.html#gfoseTab 2.将下载的glassfish-4.0.zip传输到服务器/h ...

  3. jmeter(十七)逻辑控制器

    JMeter中的Logic Controller用于为Test Plan中的节点添加逻辑控制器. JMeter中的Logic Controller分为两类:一类用来控制Test Plan执行过程中节点 ...

  4. XML读取的小例子

    public void CalculateLeave(string userAcount, string xml) //传过来的是xml内容 { try { var xmlDoc = new Syst ...

  5. GIT配置及用法

    ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...

  6. kafaka

    http://www.360doc.com/content/15/0429/12/9350055_466788393.shtml 一.Kafka中的核心概念 Producer: 特指消息的生产者 Co ...

  7. Spark学习笔记--Spark在Windows下的环境搭建(转)

    本文主要是讲解Spark在Windows环境是如何搭建的 一.JDK的安装 1.1 下载JDK 首先需要安装JDK,并且将环境变量配置好,如果已经安装了的老司机可以忽略.JDK(全称是JavaTM P ...

  8. springMvc(初识+操作步骤)

    1.导入包2.配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:x ...

  9. Jmeter中之各种乱码问题解决方案

    一.Jmeter中之请求乱码问题 如果你参数化的数据是中文,那么应该怎么解决这个问题呢? 1.在脚本的参数接设置数据的接收编码为UTF-8,如下图,这里只保证请求参数的不乱码. 2.从本地txt文件中 ...

  10. chsh - 改变登录 shell

    总览 (SYNOPSIS) chsh [ -s shell ] [ -l ] [ -u ] [ -v ] [ username ] 描述 (DESCRIPTION) chsh 用于 改变 用户的 登录 ...