HDU-3586-Information Disturbing

本文介绍了一种在限定成本条件下,通过最小化设备功率来切断敌方前线士兵与指挥官间通讯的有效算法。利用二分查找确定最优切断方案,确保整体成本不超过限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Information DisturbingTime Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 3444    Accepted Submission(s): 1212


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

题意:敌人要进攻司令部,然后我们要切断敌人与司令部之间的路,切断一条路需要代价,求使得敌人无法进攻司令部切断路中代价最大的一条路代价最小,并且在当前状况下,需要切边总和小于m。也就是删除边的和要小于m,其中代价最大的一条路代价最小。

求最大的最小,当时在做二分的题的时候就经常做,这道题当然要用二分了,在输入的时候找出边的最大值作为上限,然后dp就行了。

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include<vector>
using namespace std;
struct f
{
int now,next,val;
}tree[2005];
int n,m,head[1005],p,maxx,flag,dp[1005];
void csh()
{
memset(dp,0,sizeof(dp));
memset(head,-1,sizeof(head));
p=0;
maxx=-99999999;
}
void build(int fa,int son,int val)
{
tree[p].val=val;
tree[p].now=son;
tree[p].next=head[fa];
head[fa]=p++;
}
void dfs(int now,int pre)
{
dp[now]=0;
int limit=1;
for(int i=head[now];i!=-1;i=tree[i].next)
    {
    int pp=tree[i].now;
    if(pp==pre)
        continue;
    limit=0;
    dfs(pp,now);
    if(tree[i].val<=flag)
        dp[now]+=min(dp[pp],tree[i].val);
    else
        dp[now]+=dp[pp];
    }
if(limit==1)
    dp[now]=1000007;
}
int judge()
{
dfs(1,0);
if(dp[1]>m)
    return 0;
return 1;
}
int solve(int l,int r)
{
int s=-1,mid;
while(l<=r)
    {
    mid=(l+r)/2;
    flag=mid;
    if(judge())
        {
        s=flag;
        r=mid-1;
        }
    else
        l=mid+1;
    }
return s;
}
int main()
{
int a,b,c;
while(cin>>n>>m&&n+m)
    {
    csh();
    for(int i=1;i<n;i++)
        {
        scanf("%d%d%d",&a,&b,&c);
        build(a,b,c);
        build(b,a,c);
        maxx=maxx>c?maxx:c;
        }
    cout<<solve(1,maxx)<<endl;
    }
return 0;
}



HDU-3480 是一个典型的动态规划问题,其题目标题通常为 *Division*,主要涉及二维费用背包问题或优化后的动态规划策略。题目大意是:给定一个整数数组,将其划分为若干个连续的子集,每个子集最多包含 $ m $ 个元素,并且每个子集的最大值与最小值之差不能超过给定的阈值 $ t $,目标是使所有子集的划分代价总和最小。每个子集的代价是该子集最大值与最小值的差值。 ### 动态规划思路 设 $ dp[i] $ 表示前 $ i $ 个元素的最小代价。状态转移方程如下: $$ dp[i] = \min_{j=0}^{i-1} \left( dp[j] + cost(j+1, i) \right) $$ 其中 $ cost(j+1, i) $ 表示从第 $ j+1 $ 到第 $ i $ 个元素构成一个子集的代价,即 $ \max(a[j+1..i]) - \min(a[j+1..i]) $。 为了高效计算 $ cost(j+1, i) $,可以使用滑动窗口或单调队列等数据结构来维护区间最大值与最小值,从而将时间复杂度优化到可接受的范围。 ### 示例代码 以下是一个简化版本的动态规划实现,使用暴力方式计算区间代价,适用于理解问题结构: ```cpp #include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int MAXN = 10010; int a[MAXN]; int dp[MAXN]; int main() { int T, n, m; cin >> T; for (int Case = 1; Case <= T; ++Case) { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> a[i]; dp[0] = 0; for (int i = 1; i <= n; ++i) { dp[i] = INF; int mn = a[i], mx = a[i]; for (int j = i; j >= max(1, i - m + 1); --j) { mn = min(mn, a[j]); mx = max(mx, a[j]); if (mx - mn <= T) { dp[i] = min(dp[i], dp[j - 1] + mx - mn); } } } cout << "Case " << Case << ": " << dp[n] << endl; } return 0; } ``` ### 优化策略 - **单调队列**:可以使用两个单调队列分别维护当前窗口的最大值与最小值,从而将区间代价计算的时间复杂度从 $ O(n^2) $ 降低到 $ O(n) $。 - **斜率优化**:若问题满足特定的决策单调性,可以考虑使用斜率优化技巧进一步加速状态转移过程。 ### 时间复杂度分析 原始暴力解法的时间复杂度为 $ O(n^2) $,在 $ n \leq 10^4 $ 的情况下可能勉强通过。通过单调队列优化后,可以稳定运行于 $ O(n) $ 或 $ O(n \log n) $。 ### 应用场景 HDU-3480 的问题模型可以应用于资源调度、任务划分等场景,尤其适用于需要控制子集内部差异的问题,如图像分块压缩、数据分段处理等[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值