裸题,考虑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) ;
}

P1642 规划 [01分数规划]的更多相关文章

  1. P1642 规划 01分数规划+树形DP

    $ \color{#0066ff}{ 题目描述 }$ 某地方有N个工厂,有N-1条路连接它们,且它们两两都可达.每个工厂都有一个产量值和一个污染值.现在工厂要进行规划,拆除其中的M个工厂,使得剩下的工 ...

  2. POJ3621Sightseeing Cows[01分数规划 spfa(dfs)负环 ]

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9703   Accepted: 3299 ...

  3. ZOJ 2676 Network Wars ★(最小割算法介绍 && 01分数规划)

    [题意]给出一个带权无向图,求割集,且割集的平均边权最小. [分析] 先尝试着用更一般的形式重新叙述本问题.设向量w表示边的权值,令向量c=(1, 1, 1, --, 1)表示选边的代价,于是原问题等 ...

  4. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

  5. 【Earthquake, 2001 Open 】 0-1 分数规划

    71  奶牛施工队一场地震把约翰家园摧毁了,坚强的约翰决心重建家园.约翰已经修复了 N 个牧场,他需要再修复一些道路把它们连接起来.碰巧的是,奶牛们最近也成立了一个工程队,专门从事道路修复.而然,奶牛 ...

  6. POJ 2976 Dropping tests 01分数规划

    给出n(n<=1000)个考试的成绩ai和满分bi,要求去掉k个考试成绩,使得剩下的∑ai/∑bi*100最大并输出. 典型的01分数规划 要使∑ai/∑bi最大,不妨设ans=∑ai/∑bi, ...

  7. 【转】[Algorithm]01分数规划

    因为搜索关于CFRound277.5E题的题解时发现了这篇文章,很多地方都有值得借鉴的东西,因此转了过来 原文:http://www.cnblogs.com/perseawe/archive/2012 ...

  8. codevs 1183 泥泞的道路 01分数规划

    题目链接 题目描述 Description CS有n个小区,并且任意小区之间都有两条单向道路(a到b,b到a)相连.因为最近下了很多暴雨,很多道路都被淹了,不同的道路泥泞程度不同.小A经过对近期天气和 ...

  9. Dropping tests(01分数规划)

    Dropping tests Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8176   Accepted: 2862 De ...

随机推荐

  1. Entity Framework 实体状态

    从今天开始我们开始讲解EF中的实体状态和数据操作,这篇文章先讲解实体状态. 我们通过前面的学习,知道EF通过上下位负责跟踪实体的状态,实体状态的位置是在命名空间 System.Dat.Entity 里 ...

  2. Java框架之SpringSecurity-权限系统

    SpringSecurity SpringSecurity融合Spring技术栈,提供JavaEE应用的整体安全解决方案:提供全面的安全服务.Spring Security支持广泛的认证模型 模块划分 ...

  3. [Effective Java 读书笔记] 第二章 创建和销毁对象 第二条

    第二条 遇到多个构造器参数时,可以考虑用构建器 当遇到有多个构造器参数时,常见的是用重叠构造器,即: public class TestClass{ public TestClass(int para ...

  4. Java HashMap 四种遍历方式

    HashMap遍历方式包含以下4种: 1.遍历KeySet,再通过Key来getValue. 2.使用entrySet的迭代器. 3.foreach entrySet的方式. 3.foreache v ...

  5. pytoch之 encoder,decoder

    import torch import torch.nn as nn import torch.utils.data as Data import torchvision import matplot ...

  6. 对CAN signal 的一点理解

      首先每个 ECU是一个网络节点,每个网络节点可收发一些 Message,每个Message 由CAN signals构成.每个 CAN signal利用一个或多个连续的2进制位来表示承载的信息.下 ...

  7. 搭建fastdfs文件服务器

    一.安装FastDFS环境 1.跟踪服务器(Tracker Server) tracker1:192.168.2.134 tracker2:192.168.2.135 2.存储服务器(Storage ...

  8. -bash: warning: setlocale: LC_CTYPE: cannot change locale (zh_CN.UTF-8mb4): No such file or directory

    前几天登录服务器发现出现了这些个警告,一直没时间去处理他,今天难得有空,处理一下并记录下来,希望可以帮助到有需要的朋友. 警告信息如下: Last login: Tue May :: from 192 ...

  9. HTTP下帐号密码的截取

    用到工具: arpspoof     -->>IP欺骗 ettercap     -->>抓包 攻击者:192.168.100.110 kali 实验者:192.168.100 ...

  10. CDC+ETL实现数据集成方案

    欢迎咨询,合作! weix:wonter 名词解释: CDC又称变更数据捕获(Change Data Capture),开启cdc的源表在插入INSERT.更新UPDATE和删除DELETE活动时会插 ...