HDU - 3586

Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 5471 Accepted Submission(s): 1774

Problem Description
In the battlefield , an effective way to defeat enemies is to break their communication system.
The information department told you that there are n enemy soldiers and their network which have n-1 communication routes can cover all of their soldiers. Information can exchange between any two soldiers by the communication routes. The number 1 soldier is the total commander and other soldiers who have only one neighbour is the frontline soldier.
Your boss zzn ordered you to cut off some routes to make any frontline soldiers in the network cannot reflect the information they collect from the battlefield to the total commander( number 1 soldier).
There is a kind of device who can choose some routes to cut off . But the cost (w) of any route you choose to cut off can’t be more than the device’s upper limit power. And the sum of the cost can’t be more than the device’s life m.
Now please minimize the upper limit power of your device to finish your task.

Input
The input consists of several test cases.
The first line of each test case contains 2 integers: n(n<=1000)m(m<=1000000).
Each of the following N-1 lines is of the form:
ai bi wi
It means there’s one route from ai to bi(undirected) and it takes wi cost to cut off the route with the device.
(1<=ai,bi<=n,1<=wi<=1000)
The input ends with n=m=0.

Output
Each case should output one integer, the minimal possible upper limit power of your device to finish your task.
If there is no way to finish the task, output -1.

Sample Input
5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0

Sample Output
3


题目大意:要我们切断根节点与叶子节点的联系,并最小化最大值,并同时总和不超过指定的值。


比较明显的树形dp,然后我们 dp[x] 表示,切断x节点与x的叶子节点的最小总花费。

同时我们怎么最小化最大值呢?很明显的二分。我们二分最大值即可,令v是节点u的儿子节点,w为u到v的花费,那么我们的转移方程为:

if(w<=mid)	dp[u]+=min(dp[v],w);//mid为二分的值
else	dp[u]+=dp[v];

这样从根节点dfs即可。(注意特判叶子节点)


AC代码:

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2e4+10;
const int inf=0x3f3f3f3f;
int n,m,dp[N],mid;
vector<int> v1[N],v2[N];
void dfs(int x,int pre){
	if(v1[x].size()==1&&pre!=-1){
		dp[x]=inf;	return ;
	}
	dp[x]=0;
	for(int i=0;i<v1[x].size();i++){
		if(v1[x][i]==pre)	continue;
		dfs(v1[x][i],x);
		if(v2[x][i]<=mid)	dp[x]+=min(dp[v1[x][i]],v2[x][i]);
		else	dp[x]+=dp[v1[x][i]];
	}
}
int check(){
	dfs(1,-1);	return (dp[1]<=m);
}
int bsearch(int l,int r){
	int res=-1;
	while(l<=r){
		mid=l+r>>1;
		if(check()){
			res=mid;	r=mid-1;
		}
		else	l=mid+1;
	}
	return res;
}
signed main(){
	while(cin>>n>>m,n||m){
		for(int i=1;i<=n;i++)	v1[i].clear(),v2[i].clear();	int r=0;
		for(int i=1;i<n;i++){
			int a,b,c;	cin>>a>>b>>c;	r=max(r,c);
			v1[a].push_back(b);	v2[a].push_back(c);
			v1[b].push_back(a);	v2[b].push_back(c);
		}
		cout<<bsearch(1,r)<<endl;
	}
	return 0;
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
04-30 17:45
本人简历上&nbsp;1&nbsp;个&nbsp;RAG&nbsp;项目&nbsp;+&nbsp;1&nbsp;个&nbsp;Agent&nbsp;demo;这次面的是AI岗一面前我以为:背完八股&nbsp;+&nbsp;把项目讲清楚,应该能稳过。0-5&nbsp;min:自我介绍&nbsp;+&nbsp;项目背景-&nbsp;顺利。讲清楚了我的&nbsp;RAG&nbsp;是给法律咨询场景做的,痛点是大模型不懂行业术语。5-20&nbsp;min:项目深挖(开始崩)-&nbsp;Q1:你的法律文档总共多少?切了多少个&nbsp;chunk?-&nbsp;我:约&nbsp;500&nbsp;份&nbsp;PDF,5&nbsp;万个&nbsp;chunk-&nbsp;Q2:500&nbsp;份&nbsp;PDF&nbsp;加起来才&nbsp;5&nbsp;万&nbsp;chunk?平均每份&nbsp;100&nbsp;个&nbsp;chunk,你切片粒度是多少?-&nbsp;我:512&nbsp;token-&nbsp;Q3:法律文档里"第三条第二款"和"第三条之二"是不同含义,你的切片会不会把它切散?-&nbsp;我:(沉默&nbsp;5&nbsp;秒)……应该会-&nbsp;Q4:那你怎么解决?-&nbsp;我:我可以加一个&nbsp;metadata……(开始编)❌&nbsp;第一次崩:切片粒度没考虑业务语义。20-35&nbsp;min:评测体系(继续崩)-&nbsp;Q:你怎么知道你的&nbsp;RAG&nbsp;有效?-&nbsp;我:我用&nbsp;Recall@5……-&nbsp;Q:评测集多少条?怎么构造的?-&nbsp;我:100&nbsp;条,我手工标注的-&nbsp;Q:100&nbsp;条够吗?分布怎么样?-&nbsp;我:分布……我没分-&nbsp;Q:那你的&nbsp;Recall@5&nbsp;是&nbsp;0.81,你怎么知道这个数字是好是坏?baseline&nbsp;是什么?-&nbsp;我:(沉默&nbsp;10&nbsp;秒)❌&nbsp;第二次崩:没有&nbsp;baseline,没分布分析,纯靠"看起来还行"。35-55&nbsp;min:Agent&nbsp;部分(彻底崩)-&nbsp;Q:你的&nbsp;Agent&nbsp;demo&nbsp;用了几个工具?-&nbsp;我:3&nbsp;个,搜索、计算器、文档查询-&nbsp;Q:当用户问一个问题,你的&nbsp;Agent&nbsp;怎么决定调哪个工具?-&nbsp;我:用&nbsp;ReAct,让模型自己决定-&nbsp;Q:模型决策错了怎么办?-&nbsp;我:我加了个&nbsp;reflection……-&nbsp;Q:reflection&nbsp;失败&nbsp;3&nbsp;次后怎么处理?-&nbsp;我:(沉默&nbsp;15&nbsp;秒)……我没想过❌&nbsp;第三次崩:异常路径完全没设计。55-65&nbsp;min:业务理解&nbsp;+&nbsp;反问-&nbsp;Q:你觉得字节做&nbsp;AI&nbsp;应用最大的瓶颈是什么?-&nbsp;我:算力?数据?-&nbsp;Q:你看过哪些字节最近发的&nbsp;AI&nbsp;产品?-&nbsp;我:豆包、扣子……-&nbsp;Q:扣子是&nbsp;Agent&nbsp;平台还是工作流平台?-&nbsp;我:(再次沉默)❌&nbsp;第四次崩:对面试公司业务一无所知。
面试官拷打AI项目都会问...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务