Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 1351 Accepted Submission(s): 496
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.
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.
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.
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题意:给出一棵树,根结点为1,每条边都有一个代价,删除一条边就要付出该边的代价。现在要删去一些边,使得每个叶子结点与根结点都不连通,问删去的边中最大的边最小可以是多少,并且要保证付出的总代价少于m。思路:二分答案,然后用树dp判断是否满足题目要求。设dp[u]为以u为根的子树切去其下方结点的最小代价。显然,对于叶子结点,dp[u] = INF;对于非叶子结点,dp[u] = min(dp[v], w(u, v))。AC代码:#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <cmath> #define ll long long #define L(rt) (rt<<1) #define R(rt) (rt<<1|1) using namespace std; const int maxn = 10005; const int maxm = 1000005; const int INF = 1 << 20; struct Edge{ int v, w, next; }et[maxn * 2]; int n, m, num, limit; int eh[maxn], dp[maxn]; void init(){ memset(eh, -1, sizeof(eh)); num = 0; } void add(int u, int v, int w){ Edge e = {v, w, eh[u]}; et[num] = e; eh[u] = num++; } void dfs(int u, int pre){ dp[u] = 0; for(int i = eh[u]; i != -1; i = et[i].next) { int v = et[i].v, w = et[i].w; if(v == pre) continue; if(w > limit) w = INF; dfs(v, u); dp[u] += min(dp[v], w); } if(!dp[u]) dp[u] = INF; } void solve(){ int low = 1, high = 1000; int ans = -1; while(low <= high) { int mid = (low + high) >> 1; limit = mid; dfs(1, -1); if(dp[1] <= m) { ans = mid; high = mid - 1; } else low = mid + 1; } printf("%d\n", ans); } int main() { int a, b, c; while(scanf("%d%d", &n, &m), n || m) { init(); for(int i = 0; i < n - 1; i++) { scanf("%d%d%d", &a, &b, &c); add(a, b, c); add(b, a, c); } solve(); } return 0; }