HDU 3586 : Information Disturbing
2015-09-23 22:41
260 查看
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
5 5
1 3
2
1
4 3
3 5
5
4
2 6
0 0
[align=left]Sample Output[/align]
3
题目大意:
大意就是给你一棵树(n<=1000),树的根恒定为1,然后你需要删掉一些边,使得任何叶子都没有路径到达结点1(根).
题目的要求是在删掉的边的总花费不超过m的情况下,最大的花费最小。
解题报告:
首先本题卡了stl的vector,所以需要手写前向星链表.
因为本题中的 m 太大,而单条边的花费最多只有1000,所以我们不妨有 dp( i , j )表示将i为根的子树没有任何叶子可以到达点i,且其中的最大花费是m的最小总边权花费。
那么我们考虑转移,考虑到现在已经处理完了点i的前 k - 1 个子树,现在正在处理点i的第 k 个子树,我们将前面的状态进行转移。
不妨有这条边的花费为 w ,
我们考虑 j 从 0 -> w 的转移,他们的转移只有两种选择,第一种还是维护最大花费为 j , 此时肯定不能切掉这条w的边,只能在子树中切掉他,还有一种就是切掉w这条边,状态转移到dp(u,w_
之后我们考虑 j 从 w + 1 -> 1000 的转移,他们的转移就是取min好了,第一种是在子树中切掉,还有一种是切掉这条w的边,这样我们转移就完成了,初始化dp都是0(因为还没有考虑子树)
同时叶子的话dp初始化成inf,注意转移的时候inf+inf.....可能会爆int,需要谨慎的进行判断.
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
5 5
1 3
2
1
4 3
3 5
5
4
2 6
0 0
[align=left]Sample Output[/align]
3
题目大意:
大意就是给你一棵树(n<=1000),树的根恒定为1,然后你需要删掉一些边,使得任何叶子都没有路径到达结点1(根).
题目的要求是在删掉的边的总花费不超过m的情况下,最大的花费最小。
解题报告:
首先本题卡了stl的vector,所以需要手写前向星链表.
因为本题中的 m 太大,而单条边的花费最多只有1000,所以我们不妨有 dp( i , j )表示将i为根的子树没有任何叶子可以到达点i,且其中的最大花费是m的最小总边权花费。
那么我们考虑转移,考虑到现在已经处理完了点i的前 k - 1 个子树,现在正在处理点i的第 k 个子树,我们将前面的状态进行转移。
不妨有这条边的花费为 w ,
我们考虑 j 从 0 -> w 的转移,他们的转移只有两种选择,第一种还是维护最大花费为 j , 此时肯定不能切掉这条w的边,只能在子树中切掉他,还有一种就是切掉w这条边,状态转移到dp(u,w_
之后我们考虑 j 从 w + 1 -> 1000 的转移,他们的转移就是取min好了,第一种是在子树中切掉,还有一种是切掉这条w的边,这样我们转移就完成了,初始化dp都是0(因为还没有考虑子树)
同时叶子的话dp初始化成inf,注意转移的时候inf+inf.....可能会爆int,需要谨慎的进行判断.
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int maxn = 1e3 + 15; struct Edge { int v , w , nxt; }; int n , m ,dp[maxn][maxn],temp[maxn] , head[maxn],tot,maxv,sz[maxn]; Edge e[maxn*2]; void add(int u ,int v ,int w) { e[tot].v = v , e[tot].nxt = head[u] , e[tot].w = w;sz[u]++; head[u] = tot ++ ; } void initiation() { memset(head,-1,sizeof(head));tot=0;maxv=0;memset(sz,0,sizeof(sz)); for(int i = 1 ; i < n ; ++ i){int u , v , w;scanf("%d%d%d",&u,&v,&w);add(u,v,w);add(v,u,w);maxv=max(maxv,w);}memset(dp,0x00,sizeof(dp)); } inline void updata(int & x,int v) {x = min(x,v);} void dfs(int u ,int fa) { if(sz[u] == 1 && u != 1) memset(dp[u],0x3f,sizeof(dp[u])); for(int i = head[u] ; ~i ; i = e[i].nxt) { int v = e[i].v , w = e[i].w; if(v == fa) continue;dfs(v,u);int newe=dp[v][w]+dp[u][w]; for(int j = 0 ; j <= w ; ++ j) { updata(newe,dp[u][j]+w); if(dp[u][j] >= 1000005 || dp[v][j] >= 1000005) dp[u][j] = 1000005; else dp[u][j] += dp[v][j]; }dp[u][w]=newe; for(int j = w + 1 ; j <= maxv ; ++ j) dp[u][j] += min(dp[v][j] , w); } } int solve() { dfs(1,0);int ans = 1<<29; for(int i = 0 ; i <= maxv ; ++ i) if(dp[1][i] <= m) {ans = i;break;} if(ans == (1<<29)) ans = -1; return ans; } int main(int argc , char * argv[]) { while(scanf("%d%d",&n,&m) && n) { initiation(); printf("%d\n",solve()); } return 0; }
相关文章推荐
- uva 1459 - Flowers Placement(二分图匹配+暴力)
- JavaScript中Ajax传数组到后台怎么利用request接收
- hdu5185 Equation DP
- 计算机内存中的对齐
- web服务器控件
- iOS-UI开发 UIScrollView思维导图
- C语言数据结构之一元多项式的求导
- Codeforces#321 (Div. 2) C. Kefa and Park(dfs)
- 对STL中函数对象的认识
- Android自定义View---前奏篇(Paint和Canvas的使用)
- ML基石_56_TheoryOfGeneralization
- phper談談最近重構代碼的感受(3)
- nancy中的Stateless验证
- 统计学习方法概论
- android模拟器中SDCard创建全解析
- c语言中常见的关键字及其基本用法分析
- MySQL安装配置过程
- TCP/IP 抓包分析
- mrc 单例
- Hive定义、Hive与HBase关系、Hive与RDBMS的关系、数据库与数据仓库的区别