删边类树形DP
Information Disturbing
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 2262 Accepted Submission(s): 815
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
Author
alpc86
Source
Recommend
重要的思想:
求值,求最小值,求最大值,都可以用二分,就是在一个范围内去寻找一个合适的答案。
这样比直接求解要容易得多。
如果没有范围,可以自己划出一个范围。
/**==========================================
* This is a solution for ACM/ICPC problem
*
* @source:
* @type:
* @author: wust_ysk
* @blog: http://blog.csdn.net/yskyskyer123
* @email: 2530094312@qq.com
*===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
const int INF =1000000+20;
const int maxn= 1000 ;
//const int maxV=12 ;
int n,m,maxw,limit;
int dp[maxn+4];
struct Edge
{
int from,to,w;
Edge(){}
Edge(int from,int to,int w):from(from),to(to),w(w)
{
}
};
vector<Edge>edges;
vector<int >G[maxn+4];
void add_edge(int s,int t,int w)
{
edges.push_back(Edge(s,t,w));
edges.push_back(Edge(t,s,w));
int m=edges.size();
G[s].push_back(m-2);
G[t].push_back(m-1);
}
void init()
{
for(int i=1;i<=n;i++) G[i].clear();
edges.clear();
}
void dfs(int x,int fa)
{
if(x!=1&&G[x].size()==1) {dp[x]=INF;return;}//x!=1掉了,也就是说叶子结点的判断是错的
dp[x]=0;
for(int i=0;i<G[x].size();i++)
{
Edge &e=edges [G[x][i]];
int y=e.to;
if(y==fa) continue;
dfs(y,x);
int w=e.w;
int ret=dp[y];
if(w<=limit) ret=min(ret,w);
dp[x]+=ret;
}
}
bool can(int x)
{
limit=x;
dfs(1,-1);
if(dp[1]<=m) return true;
return false;
}
int work()
{
int le=1,ri=maxw;
while(le<=ri)
{
int mid=(le+ri)/2;
if( can(mid) ) ri=mid-1;
else le=mid+1;
}
if(can(le)) return le;
return -1;
}
int main()
{
int x,y,w;
while(~scanf("%d%d",&n,&m)&&(n||m) )
{
init();
maxw=1;
for(int i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&w);
add_edge(x,y,w);
maxw=max(maxw,w);
}
printf("%d\n",work() );
}
return 0;
}
/*
5 5
1 3 2
1 4 3
3 5 5
4 2 6
5 5
1 3 2
1 4 3
3 5 5
4 2 6
2 3
1 2 3
*/