CF815C Karen and Supermarket [樹形DP]

  題目傳送門

Karen and Supermarket

?

On the way home, Karen decided to stop by the supermarket to buy some groceries.

She needs to buy a lot of goods, but since she is a student her budget is still quite limited. In fact, she can only spend up to?b?dollars.

The supermarket sells?n?goods. The?i-th good can be bought for?ci?dollars. Of course, each good can only be bought once.

Lately, the supermarket has been trying to increase its business. Karen, being a loyal customer, was given?n?coupons. If Karen purchases the?i-th good, she can use the?i-th coupon to decrease its price by?di. Of course, a coupon cannot be used without buying the corresponding good.

There is, however, a constraint with the coupons. For all?i?≥?2, in order to use the?i-th coupon, Karen must also use the?xi-th coupon (which may mean using even more coupons to satisfy the requirement for that coupon).

Karen wants to know the following. What is the maximum number of goods she can buy, without exceeding her budget?b?

?

Input

The first line of input contains two integers?n?and?b?(1?≤?n?≤?5000,?1?≤?b?≤?109), the number of goods in the store and the amount of money Karen has, respectively.

The next?n?lines describe the items. Specifically:

  • The?i-th line among these starts with two integers,?ci?and?di?(1?≤?di?<?ci?≤?109), the price of the?i-th good and the discount when using the coupon for the?i-th good, respectively.
  • If?i?≥?2, this is followed by another integer,?xi?(1?≤?xi?<?i), denoting that the?xi-th coupon must also be used before this coupon can be used.

?

Output

Output a single integer on a line by itself, the number of different goods Karen can buy, without exceeding her budget.

?

Examples
input
Copy
6 16
10 9
10 5 1
12 2 1
20 18 3
10 2 3
2 1 5
output
Copy
4
input
Copy
5 10
3 1
3 1 1
3 1 2
3 1 3
3 1 4
output
Copy
5

?

Note

In the first test case, Karen can purchase the following?4?items:

  • Use the first coupon to buy the first item for?10?-?9?=?1?dollar.
  • Use the third coupon to buy the third item for?12?-?2?=?10?dollars.
  • Use the fourth coupon to buy the fourth item for?20?-?18?=?2?dollars.
  • Buy the sixth item for?2?dollars.

The total cost of these goods is?15, which falls within her budget. Note, for example, that she cannot use the coupon on the sixth item, because then she should have also used the fifth coupon to buy the fifth item, which she did not do here.

In the second test case, Karen has enough money to use all the coupons and purchase everything.


  分析:

  考試的時候遇到這道題結果還是一臉懵逼,果然我$DP$還是太弱了,還是得好好補補。

  考慮用樹形$DP$,根據題意構建出一棵樹,然后從根節點開始深搜,把每個節點遍歷一遍,然后從葉子節點開始向根節點轉移狀態。

  定義$f[x][j][0/1]$表示遍歷到了第$x$個商品時已經購買了$j$個商品,$0/1$表示第$x$個商品是否打折。

  那么狀態轉移方程為:

  $f[x][j+k][0]=Min(f[x][j+k][0],f[x][j][0]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][0]);$

  $f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][1]);$

  其中$j$和$k$分別表示當前節點和其子節點已經搜過的子樹中購買的商品個數。可能有點繞,可以結合代碼理解。

  Code:

?

//It is made by HolseLee on 15th Aug 2018
//CF815C
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<algorithm>
#define Max(a,b) (a)>(b)?(a):(b)
#define Min(a,b) (a)<(b)?(a):(b)
#define Swap(a,b) (a)^=(b)^=(a)^=(b)
using namespace std;const int N=5005;
int n,m,head[N],siz,w[N],c[N],rt[N];
long long f[N][N][2];
struct Node{int to,nxt;
}edge[N<<1];inline int read()
{char ch=getchar();int num=0;bool flag=false;while(ch<'0'||ch>'9'){if(ch=='-')flag=true;ch=getchar();}while(ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}return flag?-num:num;
}inline void add(int x,int y)
{edge[++siz].to=y;edge[siz].nxt=head[x];head[x]=siz;
}void dfs(int x)
{rt[x]=1;f[x][0][0]=0;f[x][1][0]=w[x];f[x][1][1]=c[x];for(int i=head[x];i;i=edge[i].nxt){int y=edge[i].to;dfs(y);for(int j=rt[x];j>=0;--j)for(int k=0;k<=rt[y];++k){f[x][j+k][0]=Min(f[x][j+k][0],f[x][j][0]+f[y][k][0]);f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][0]);f[x][j+k][1]=Min(f[x][j+k][1],f[x][j][1]+f[y][k][1]);}rt[x]+=rt[y];}
}int main()
{//freopen("shopping.in","r",stdin);//freopen("shopping.out","w",stdout);n=read();m=read();memset(f,127/3,sizeof(f));w[1]=read();c[1]=read();c[1]=w[1]-c[1];int x,y,z;for(int i=2;i<=n;++i){x=read();y=read();z=read();w[i]=x;c[i]=x-y;add(z,i);}dfs(1);for(int i=n;i>=0;--i){if(f[1][i][0]<=m||f[1][i][1]<=m){printf("%d\n",i);break;}}return 0;
}

?

?

?

?

轉載于:https://www.cnblogs.com/cytus/p/9481893.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/397203.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/397203.shtml
英文地址,請注明出處:http://en.pswp.cn/news/397203.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux命令積累之egrep命令

學搭建Nginx環境&#xff0c;必須要配置的Nginx.conf文件中&#xff0c;如下&#xff1a;#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worke…

Sublime Text 3 安裝及插件推薦

本篇介紹跨平臺編輯器Sublime Text 3的安裝和其插件推薦。 目錄&#xff1a; 1.介紹 2.下載安裝 3.插件 4.參考資料 1.介紹 Sublime Text具有漂亮的用戶界面和強大的功能&#xff0c;例如代碼縮略圖&#xff0c;Python的插件&#xff0c;代碼段等。還可自定義鍵綁定&#xff0c…

6工程文件夾作用_data_dragon數據工程小工具收集

最近在GitHub上創建了一個新工程&#xff0c;收集個人在數據工程工作的小工具集合&#xff0c;命名為data_dragon (數據一條龍)。取這個名字的是希望這些腳本或代碼能夠復用&#xff0c;端到端地減少臨時數據處理的時間。最近因為工作上的一些變化&#xff0c;寫作節奏有點被打…

暑假第十七測

題解&#xff1a; 第一題 #include<bits/stdc.h> using namespace std; #define ll long long const int M 1e5 10; ll a[M], b[M], ans; priority_queue <ll, vector<ll> , greater<ll> > Q; int main(){freopen("buy.in","r",…

Uva 11354 LCA 倍增祖先

題目鏈接&#xff1a;https://vjudge.net/contest/144221#problem/B 題意&#xff1a;找一條從 s 到 t 的路&#xff0c;使得瓶頸路最小。 點的數目是10^4&#xff0c;如果向之前的方案求 maxcost數組&#xff0c;O(n*n)時間是過不了的&#xff0c;這個時候&#xff0c;用到了…

Nginx搭建flv視頻點播服務器

Nginx搭建flv視頻點播服務器前一段時間使用Nginx搭建的多媒體服務器只能在緩沖過的時間區域內拖放, 而不能拖放到未緩沖的地方. 這就帶來了一個問題: 如果視頻限速的速率很小, 那么客戶端觀看視頻時肯定不流暢, 而且用戶不能向前拖放, 用戶體驗很不好. 如果視頻限速的速率很大或…

編碼拾遺

1 #!/usr/bin/env python32 #-*- coding:utf-8 -*-3 4 Administrator 5 2018/8/16 6 7 8 # fopen("demo","r",encoding"utf8")9 # dataf.read() 10 # print(data) 11 # f.close() 12 13 14 # print("沈哲子") 15 16 s"中國&qu…

Xcode:Foundation框架找不到,或者是自動提示出現問題

問題描述&#xff1a;Foundation框架找不到&#xff0c;或者是自動提示出現問題 之前的操作&#xff1a;手賤&#xff0c;不少心把編譯器里面的源碼改了處理辦法&#xff1a;清理緩存緩存位置&#xff1a;點擊桌面后&#xff0c;選擇系統菜單欄&#xff1a;前往—電腦—硬盤—用…

mybatis 不生效 參數_Mybatis-日志配置

日志Mybatis 的內置日志工廠提供日志功能&#xff0c;內置日志工廠將日志交給以下其中一種工具作代理&#xff1a;SLF4JApache Commons LoggingLog4j 2Log4jJDK loggingMyBatis 內置日志工廠基于運行時自省機制選擇合適的日志工具。它會使用第一個查找得到的工具(按上文列舉的順…

PS通過濾色實現簡單的圖片拼合

素材如下&#xff1a; 素材一&#xff1a; 雪山 素材二&#xff1a; 月亮 效果&#xff1a; 實現步驟 1、在PS中打開雪山素材一 2、將月亮素材直接拖入雪山所在的圖層中 3、鎖定置入素材的高寬比&#xff08;點擊一下鏈狀按鈕&#xff09; 4、調整月亮到合適大小合適位置 5、…

預處理:主成分分析與白化

主成分分析 引言 主成分分析&#xff08;PCA&#xff09;是一種能夠極大提升無監督特征學習速度的數據降維算法。更重要的是&#xff0c;理解PCA算法&#xff0c;對實現白化算法有很大的幫助&#xff0c;很多算法都先用白化算法作預處理步驟。 假設你使用圖像來訓練算法&#x…

jQuery Ajax

jQuery load()方法&#xff1a;是簡單但強大的Ajax 方法load() 方法從服務器(URL,data,callback);必須的URL 參數規定您希望架加載的URL可選的data參數 規定與請求一同發送的差字符串鍵/值對集合。可選的callback參數時load()方法完成后所執行的函數名稱$(documnet).ready(…

swagger 修改dto注解_Web服務開發:Spring集成Swagger,3步自動生成API文檔

目錄&#xff1a;1&#xff0c;Spring Boot集成Swagger2&#xff0c;Swagger接口文檔頁面3&#xff0c;常見問題和解決方法在Sping開發REST接口服務時&#xff0c;API文檔是不可缺少的一個重要部分。Swagger框架定義了完整的REST接口文檔規范&#xff0c;提供了強大的頁面測試功…

WPF自定義控件之列表滑動特效 PowerListBox

列表控件是應用程序中常見的控件之一&#xff0c;對其做一些絢麗的視覺特效&#xff0c;可以讓軟件增色不少。 本人網上看過一個視頻&#xff0c;是windows phone 7系統上的一個App的列表滾動效果&#xff0c;效果非常炫 現在在WPF上用ListBox重現此效果 首先我們來分析一下&am…

去除inline-block元素間間距

根本原因&#xff1a;inline-block元素之間之所以有空白間距是因為空格有字體大小原因。 第一種&#xff1a; 把代碼之間的換行空白都去掉。 例如&#xff1a; <div>第一個inline-block元素</div><div>第二個inline-block元素</div> 第二種&#xff1a…

python - 定時清理ES 索引

只保留三天 #!/usr/bin/env python3 # -*- coding:utf-8 -*- import os import datetime# 時間轉化為字符串now_time datetime.datetime.now().strptime(datetime.datetime.now().strftime("%Y.%m.%d"),"%Y.%m.%d") os.system("curl -XGET http://12…

CnosDB如何確保多步操作的最終一致性?

背景 在時序數據庫中&#xff0c;資源的操作是一個復雜且關鍵的任務。這些操作通常涉及到多個步驟&#xff0c;每個步驟都可能會失敗&#xff0c;導致資源處于不一致的狀態。例如&#xff0c;一個用戶可能想要在CnosDB集群中刪除一個租戶&#xff0c;這個操作可能需要刪除租戶…

頸椎前路caspar撐開器_“骨質增生”導致的頸椎病怎么破?

來源&#xff1a;《脊柱外科微創手術精要》作者&#xff1a;中日友好醫院 鄒海波此文是區別于頸椎間盤軟性突出診治一文&#xff0c;主要針對“骨質增生”導致的頸椎病(Spondylosis)進行介紹。傳統的頸椎前路手術主要為頸椎病而設計。一度認為對頸椎病采用前路手術的主要好處在…

Struts2整合Freemarker生成靜態頁面

2019獨角獸企業重金招聘Python工程師標準>>> 這是生成靜態頁面的預覽&#xff1a; 其對應的模板文件&#xff1a; <table style"text-align:center;FONT-SIZE: 11pt; WIDTH: 600px; FONT-FAMILY: 宋體; BORDER-COLLAPSE: collapse" borderColor#3399ff…

使用flot.js 發現x軸y軸無法顯示軸名稱

添加此插件解決問題 flot-axislabels https://github.com/markrcote/flot-axislabels 轉載于:https://www.cnblogs.com/feehuang/p/4993920.html