【题解】hdu 3586 Information Disturbing 二分 树形dp
题目描述#
Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 4003 Accepted Submission(s): 1391
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
大意#
题目大意:给定n个敌方据点,1为司令部,其他点各有一条边相连构成一棵树,每条边都有一个权值cost表示破坏这条边的费用,叶子节点为前线。现要切断前线和司令部的联系,每次切断边的费用不能超过上限limit,问切断所有前线与司令部联系所花费的总费用少于m时的最小limit。1<=n<=1000,1<=m<=1000000
思路#
- 二分+树形dp
- 二分可能的limit,再判断该limit下的花费是否符合m;
- dp: 对于一个节点u及其子节点v,要切断u子树下所有叶子节点与u的联系,有两种方式.
- 设dp[u]表示切断u子树下所有叶子节点与u的联系的最小花费
- 不切断u->v,dp[u]+=dp[v];
- 切断u->v,dp[u]+=dis(u,v);
代码#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | #include<cstdio> #include<string> #include<vector> #include<cstring> #include<iostream> #define re register int using namespace std; const int maxn=1e3+50; inline int read(){ int x=0,w=1; char ch= getchar (); while (ch!= '-' &&(ch< '0' ||ch> '9' )) ch= getchar (); if (ch== '-' ) w=-1,ch= getchar (); while (ch>= '0' &&ch<= '9' ) x=(x<<1)+(x<<3)+ch-48,ch= getchar (); return x*w; } int head[maxn<<1]; long long dp[maxn]; int tot=1,INF=1e6; struct data{ int to,nxt; long long w; }edge[maxn<<1]; void DFS( int u, int fa, int lim){ int flag=0; dp[u]=0; for ( int i=head[u];i;i=edge[i].nxt){ int v=edge[i].to; if (v!=fa){ flag=1; DFS(v,u,lim); if (edge[i].w<=lim) dp[u]+=min(dp[v],edge[i].w); else dp[u]+=dp[v]; } } if (!flag) dp[u]=INF; } inline void add( int u, int v, int w){ edge[tot].to=v; edge[tot].w=w; edge[tot].nxt=head[u]; head[u]=tot++; } int main(){ int MAXX=-1; int n,m; while (~ scanf ( "%d%d" ,&n,&m)) { // cout<<n<<m<<endl; if (n==0&&m==0) break ; tot=1; memset (head,0, sizeof (head)); for (re i=1;i<n;++i) { int a,b,w; a=read(),b=read(),w=read(); add(a,b,w); add(b,a,w); MAXX=max(MAXX,w); } int l=0,r=MAXX,ans=-1,mid; while (l<=r) { //cout<<endl; mid=(l+r)>>1; DFS(1,-1,mid); //cout<<l<<" "<<r<<endl; //cout<<dp[1]<<endl; if (dp[1]<=m){ ans=mid; r=mid-1; } else l=mid+1; } printf ( "%d\n" ,ans); } return 0; } /* 5 5 1 3 2 1 4 3 3 5 5 4 2 6 0 0 */ |
作者: bbqub
出处:https://www.cnblogs.com/bbqub/p/9073077.html
版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· 腾讯元宝接入 DeepSeek R1 模型,支持深度思考 + 联网搜索,好用不卡机!
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战