nowcoder多校2B(基环树DP)

先断边成树分析。。

需要用优惠一的时候没有任何条件。。所以把子树的最优方案和自身加起来就可以了。。

用优惠二需要子树即没用优惠一,也没通过他的儿子使用优惠二。。

所以可以这么设方程

设dp[i][1]为i结点在满足可以为其父亲提供优惠二下的最优方案

dp[i][0]为结点i不能为父亲提供优惠下的最优方案

dp[i][1]直接就是子树最优加自身无优惠

dp[i][0]分为2种方案,一种是子树最优加自身优惠一;一种是选一个子树给自己提供优惠二,这个可以在dp[i][1]的基础上选出最小的dp[son][1]-min{dp[son][0],dp[son][1]}(即换成可提供优惠二的最小代价)的子树,加上优惠代价就可以了。。。

然后考虑环的情况。。

首先断环,即不用这种优惠的话就可以随意跑dp。。

再考虑这边(设为<u,v>)的影响,就是一定要用这个优惠二,那么v就不能再用任何优惠,u也不能使用优惠一,为了保证优惠二的顺利使用,可以考虑切断v原来的边,加上<u,v>,以v为根跑DP,这样可以再求出答案,而这个答案可能用了这个优惠二,也可能没有,反正包含了就算把情况考虑清楚了。。

 

 

/**
 *          ┏┓    ┏┓
 *          ┏┛┗━━━━━━━┛┗━━━┓
 *          ┃       ┃  
 *          ┃   ━    ┃
 *          ┃ >   < ┃
 *          ┃       ┃
 *          ┃... ⌒ ...  ┃
 *          ┃              ┃
 *          ┗━┓          ┏━┛
 *          ┃          ┃ Code is far away from bug with the animal protecting          
 *          ┃          ┃   神兽保佑,代码无bug
 *          ┃          ┃           
 *          ┃          ┃        
 *          ┃          ┃
 *          ┃          ┃           
 *          ┃          ┗━━━┓
 *          ┃              ┣┓
 *          ┃              ┏┛
 *          ┗┓┓┏━━━━━━━━┳┓┏┛
 *           ┃┫┫       ┃┫┫
 *           ┗┻┛       ┗┻┛
 */ 
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-12
#define succ(x) (1LL<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 200010
#define N 1000005
#define M(x,y) x=max(x,y)
const double pi=acos(-1);
const ll inf=1e16+7;
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return f*x;
}



struct edge{int t;edge*next;}e[nm],*h[NM],*o=e;
void add(int x,int y){o->t=y;o->next=h[x];h[x]=o++;}
int n,a[NM],b[NM],_t,_x,_y,f[NM],ls[NM],rs[NM],tot;
ll d[NM][2],ans,_ans;
bool v[NM];

int find(int x){return f[x]==x?x:f[x]=find(f[x]);}

void dfs(int x){
    v[x]++;d[x][1]=a[x];
    link(x)if(j->t!=_x&&f[x]!=j->t){
	f[j->t]=x;
	dfs(j->t);
	d[x][1]+=min(d[j->t][0],d[j->t][1]);
    }
    ll t=inf;
    link(x)if(f[x]!=j->t&&j->t!=_x)t=min(t,d[j->t][1]-min(d[j->t][0],d[j->t][1]));
    d[x][0]=min(d[x][1]+t-a[x],d[x][1]-b[x]);
}

int main(){
    n=read();
    inc(i,1,n)a[i]=read();inc(i,1,n)b[i]=read();
    inc(i,1,n)f[i]=i;
    inc(i,1,n){
	_t=read();
	int x=find(_t),y=find(i);
	if(x==y){rs[++tot]=_t;ls[tot]=i;continue;}
	add(_t,i);f[x]=y;
    }
//    inc(k,1,tot)printf("::%d %d\n",ls[k],rs[k]);
    inc(k,1,tot){
        f[ls[k]]=0;_x=0;
	dfs(ls[k]);
//	inc(i,1,n)printf("%d %d\n",d[i][0],d[i][1]);
        ans=min(d[ls[k]][1],d[ls[k]][0]);
	add(rs[k],ls[k]);f[rs[k]]=0;_x=rs[k];
        dfs(rs[k]);
//	inc(i,1,n)printf("%d %d\n",d[i][0],d[i][1]);
        ans=min(ans,d[rs[k]][1]);
	ans=min(ans,d[rs[k]][0]);
	_ans+=ans;
    }
    return 0*printf("%lld\n",_ans);
}

 

discount

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

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值