P1642 规划 [01分数规划]

裸题,考虑size完了跑一个树上背包,这题没了。

// by Isaunoya
#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
#define int long long

const int _ = 1 << 21;
struct I {
	char fin[_], *p1 = fin, *p2 = fin;
	inline char gc() {
		return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
	}
	inline I& operator>>(int& x) {
		bool sign = 1;
		char c = 0;
		while (c < 48) ((c = gc()) == 45) && (sign = 0);
		x = (c & 15);
		while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
		x = sign ? x : -x;
		return *this;
	}
	inline I& operator>>(double& x) {
		bool sign = 1;
		char c = 0;
		while (c < 48) ((c = gc()) == 45) && (sign = 0);
		x = (c - 48);
		while ((c = gc()) > 47) x = x * 10 + (c - 48);
		if (c == '.') {
			double d = 1.0;
			while ((c = gc()) > 47) d = d * 0.1, x = x + (d * (c - 48));
		}
		x = sign ? x : -x;
		return *this;
	}
	inline I& operator>>(char& x) {
		do
			x = gc();
		while (isspace(x));
		return *this;
	}
	inline I& operator>>(string& s) {
		s = "";
		char c = gc();
		while (isspace(c)) c = gc();
		while (!isspace(c) && c != EOF) s += c, c = gc();
		return *this;
	}
} in;
struct O {
	char st[100], fout[_];
	signed stk = 0, top = 0;
	inline void flush() {
		fwrite(fout, 1, top, stdout), fflush(stdout), top = 0;
	}
	inline O& operator<<(int x) {
		if (top > (1 << 20)) flush();
		if (x < 0) fout[top++] = 45, x = -x;
		do
			st[++stk] = x % 10 ^ 48, x /= 10;
		while (x);
		while (stk) fout[top++] = st[stk--];
		return *this;
	}
	inline O& operator<<(char x) {
		fout[top++] = x;
		return *this;
	}
	inline O& operator<<(string s) {
		if (top > (1 << 20)) flush();
		for (char x : s) fout[top++] = x;
		return *this;
	}
} out;
#define pb emplace_back
#define fir first
#define sec second

template < class T > inline void cmax(T & x , const T & y) {
	(x < y) && (x = y) ;
}
template < class T > inline void cmin(T & x , const T & y) {
	(x > y) && (x = y) ;
}

int n , m ;
const int N = 100 + 10 ;
int a[N] , b[N] ;

struct edge {
	int v , nxt ;
} e[N << 1] ;
int head[N] , cnt = 0 ;
double val[N] , dp[N][N] ;
void add(int u , int v) {
	e[++ cnt] = { v , head[u] } ;
	head[u] = cnt ;
}
int sz[N] , fa[N] ;
void dfs1(int u) {
	sz[u] = 1 ;
	for(int i = head[u] ; i ; i = e[i].nxt) {
		int v = e[i].v ;
		if(v == fa[u]) continue ;
		fa[v] = u ;
		dfs1(v) ;
		sz[u] += sz[v] ;
	}
}
const int inf = 1e9 ;
const double eps = 1e-6 ;
void dfs(int u) {
	dp[u][0] = 0 ;
	for(int i = head[u] ; i ; i = e[i].nxt) {
		int v = e[i].v ;
		if(v == fa[u]) continue ;
		dfs(v) ;
		for(int j = min(m , sz[u]) ; ~ j ; j --)
			for(int k = 0 ; k <= min(j , sz[v]) ; k ++)
				dp[u][j] = max(dp[u][j] , dp[u][j - k] + dp[v][k]) ;
	}
	for(int i = min(m , sz[u]) ; i ; i --) dp[u][i] = dp[u][i - 1] + val[u] ;
}
bool chk(double mid) {
	rep(i , 1 , n) val[i] = 1.0 * a[i] - 1.0 * mid * b[i] ;
	rep(i , 1 , n) rep(j , 1 , m) dp[i][j] = -inf ;
	dfs(1) ;
	rep(i , 1 , n) if(dp[i][m] > eps) return 1 ;
	return 0 ;
}
signed main() {
#ifdef _WIN64
	freopen("testdata.in" , "r" , stdin) ;
#endif
	in >> n >> m ; m = n - m ;
	rep(i , 1 , n) in >> a[i] ;
	rep(i , 1 , n) in >> b[i] ;
	rep(i , 1 , n - 1) {
		int u , v ;
		in >> u >> v ;
		add(u , v) ;
		add(v , u) ;
	}
	double l = 0 , r = 100000 ;
	dfs1(1) ;
	while(r - l > eps) {
		double mid = (l + r) / 2.00 ;
		if(chk(mid)) l = mid ;
		else r = mid ;
	}
	printf("%.1lf\n" , l) ;
}
posted @ 2020-01-15 20:23  _Isaunoya  阅读(72)  评论(0编辑  收藏  举报