YAIMH1993的笔记
如果奇迹木有出现,就去创造一个
posts - 29,comments - 0,trackbacks - 0
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define inf 1000100
const int maxn = 1010 , maxm = 3030;
struct Edge {
    int v,w,next;   
}edge[maxm];
int E,head[maxn];
int n , m;
int dp[maxn] , d[maxn] , p[maxn];
void init() {
    E = 0;
    for(int i=1;i<=n;i++)
        head[i] = p[i] = -1, d[i] = dp[i] = 0;
}
void addedge(int u,int v,int w) {
    edge[E].v=v;edge[E].w=w;edge[E].next=head[u];head[u]=E++;
    edge[E].v=u;edge[E].w=w;edge[E].next=head[v];head[v]=E++;
}
void dfs(int u,int mid) {
    for(int i=head[u];i!=-1;i=edge[i].next) {
        int v = edge[i].v;
        if(v == p[u]) continue;
        p[v] = u;
        dfs(v,mid);
        if(edge[i].w <= mid) dp[u] += min(dp[v] , edge[i].w);
        else dp[u] += dp[v];   
    }   
}
int main() {
    while(~scanf("%d%d",&n,&m) && n+m) {
        init();
        int u,v,w;
        for(int i=1;i<n;i++) {
           scanf("%d%d%d",&u,&v,&w);
           addedge(u,v,w);
           d[u]++; d[v]++;   
        }   
        int ans = -1;
        int left = 1 , right = m;
        while(left <= right) {
            dp[1] = 0;
            for(int i=2;i<=n;i++)
                if(d[i]==1) dp[i] = inf;
                else dp[i] = 0;
            int mid = (left + right) >> 1;
            dfs(1,mid);
            if(dp[1] <= m) {
                ans = mid;
                right = mid - 1;   
            }   
            else left = mid + 1;
        }
        printf("%d\n",ans);
    }   
}
posted on 2012-10-16 00:10 YouAreInMyHeart 阅读(106) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理