2014
11-27

# Information Disturbing

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.

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 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.

5 5
1 3 2
1 4 3
3 5 5
4 2 6
0 0

3

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<memory.h>
using namespace std;
int dp[1002],n,m,NE,mid;
const int INF=1000001;
struct node
{
int u,v,val,next;
} Edge[2002];
{
Edge[NE].val=w;
}
int dfs(int u,int fa)
{
int i,sum=0;
vis[u]=1;
{
int r=Edge[i].v;
if(fa==r) continue;
int rr=dfs(r,u);
if(rr>Edge[i].val&&Edge[i].val<=mid)
{
rr=Edge[i].val;
}
sum+=rr;
}
if(!sum)return INF;
return sum;
}
int main()
{
while(cin>>n>>m,(n||m))
{
NE=0;
int i,j,k,a,b,w,ans=INF;
int l=1002,r=0;
memset(vis,0,sizeof(vis));
for(i=1; i<n; i++)
{
cin>>a>>b>>w;
l=min(l,w);
r=max(r,w);
}
while(l<=r)
{
mid=(l+r)>>1;
if(dfs(1,0)<=m)
{
ans=min(ans,mid);
r=mid-1;
}
else l=mid+1;
}
if(ans>=INF)cout<<-1<<endl;
else cout<<ans<<endl;
}
return 0;
}


1. #include <cstdio>
#include <algorithm>

struct LWPair{
int l,w;
};

int main() {
//freopen("input.txt","r",stdin);
const int MAXSIZE=5000, MAXVAL=10000;
LWPair sticks[MAXSIZE];
int store[MAXSIZE];
int ncase, nstick, length,width, tmp, time, i,j;
if(scanf("%d",&ncase)!=1) return -1;
while(ncase– && scanf("%d",&nstick)==1) {
for(i=0;i<nstick;++i) scanf("%d%d",&sticks .l,&sticks .w);
std::sort(sticks,sticks+nstick,[](const LWPair &lhs, const LWPair &rhs) { return lhs.l>rhs.l || lhs.l==rhs.l && lhs.w>rhs.w; });
for(time=-1,i=0;i<nstick;++i) {
tmp=sticks .w;
for(j=time;j>=0 && store >=tmp;–j) ; // search from right to left
if(j==time) { store[++time]=tmp; }
else { store[j+1]=tmp; }
}
printf("%dn",time+1);
}
return 0;
}

2. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;