CF375E Red and Black Tree(线性规划)

Luogu

题解时间

很明显有一个略显复杂的 $ n^3 $ dp,但不在今天讨论范围内。

考虑一些更简单的方法。

设有 $ m $ 个点为黑,转化成线性规划问题,很明显有

\[minimum:\sum\limits_{i} ( 1 - col_{i} ) x_{i}
\]
\[\sum\limits_{ dis(i,j) \le lim } x_{j} \ge 1
\]
\[\sum\limits_{i} x_{i} =m
\]

最后的一个等式转化成两个不等式,之后将整个线性规划利用对偶原理转化成标准型直接单纯形法求解即可。

毫无疑问最终结果不会有 $ x_{i} > 1 $ ,而对于是否可能出现小数,很明显不会影响最终结果。

#include<bits/stdc++.h>
using namespace std;
typedef long long lint;
struct pat{int x,y;pat(int x=0,int y=0):x(x),y(y){}bool operator<(const pat &p)const{return x==p.x?y<p.y:x<p.x;}};
template<typename TP>inline void read(TP &tar)
{
TP ret=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){ret=ret*10+(ch-'0');ch=getchar();}
tar=ret*f;
}
template<typename TP,typename... Args>inline void read(TP& t,Args&... args){read(t),read(args...);}
namespace RKK
{
const int N=511;
const double eps=1e-8,inf=1e18;int cz(const double &a){return fabs(a)<=eps?0:(a>eps?1:-1);}
struct sumireko{int to,ne,w;}e[N<<1];int he[N],ecnt;
void addline(int f,int t,int w){e[++ecnt].to=t,e[ecnt].w=w,e[ecnt].ne=he[f],he[f]=ecnt;}
namespace lisp
{
int n,m,id[N<<1];double a[N][N];
void pivot(int l,int e)
{
swap(id[m+l],id[e]);
double k=-a[l][e];a[l][e]=-1;
for(int j=0;j<=m;j++) a[l][j]/=k;
for(int i=0;i<=n;i++)if(i!=l&&cz(a[i][e]))
{
k=a[i][e],a[i][e]=0;
for(int j=0;j<=m;j++) a[i][j]+=k*a[l][j];
}
}
int init()
{
for(int i=1;i<=m+n;i++) id[i]=i;
while(1)
{
int l=0,e=0;
for(int i=1;i<=n;i++)if(cz(a[i][0])<0&&(!l||rand()&1)) l=i;
if(!l) break;
for(int j=1;j<=m;j++)if(cz(a[l][j])>0&&(!e||rand()&1)) e=j;
if(!e) return -1;//no solution
pivot(l,e);
}return 0;
}
int simplex()
{
while(1)
{
int l=0,e=0;double mi=inf;
for(int j=1;j<=m;j++)if(cz(a[0][j])>0&&(!e||rand()&1)) e=j;
if(!e) break;
for(int i=1;i<=n;i++)if(cz(a[i][e])<0&&(!l||-a[i][0]/a[i][e]<mi)) l=i,mi=-a[i][0]/a[i][e];
if(!l) return -1;//unbounded
pivot(l,e);
}return 0;
}
void work()
{
if(init()==-1) return (void)(puts("-1"));
if(simplex()==-1) return (void)(puts("-1"));
printf("%.0lf\n",a[0][0]);
}
}
int n,m,lim,col[N];
void dfs(int x,int f,int sp,int dis)
{
if(dis>lim) return;
lisp::a[x][sp]=-1;
for(int i=he[x],t=e[i].to;i;i=e[i].ne,t=e[i].to)if(t!=f) dfs(t,x,sp,dis+e[i].w);
}
int main()
{
read(n,lim);for(int i=1;i<=n;i++) read(col[i]),m+=col[i];
for(int i=2,x,y,w;i<=n;i++) read(x,y,w),addline(x,y,w),addline(y,x,w);
lisp::n=n,lisp::m=n+2;
for(int i=1;i<=n;i++) lisp::a[i][0]=!col[i],lisp::a[0][i]=1;lisp::a[0][n+1]=m,lisp::a[0][n+2]=-m;
for(int i=1;i<=n;i++) lisp::a[i][n+1]=-1,lisp::a[i][n+2]=1;
for(int i=1;i<=n;i++) dfs(i,0,i,0);
lisp::work();
return 0;
}
}
int main(){return RKK::main();}

CF375E Red and Black Tree(线性规划)的更多相关文章

  1. [CC-BLREDSET]Black and Red vertices of Tree

    [CC-BLREDSET]Black and Red vertices of Tree 题目大意: 有一棵\(n(\sum n\le10^6)\)个结点的树,每个结点有一种颜色(红色.黑色.白色).删 ...

  2. BNUOJ 26229 Red/Blue Spanning Tree

    Red/Blue Spanning Tree Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on HDU. ...

  3. 「CF375E」Red and Black Tree「树形DP」

    题意 给定一个结点颜色红或黑的树,问最少进行多少次交换黑.红结点使得每个红结点离最近的黑结点距离\(\leq x\). \(1\leq n \leq 500, 1 \leq x \leq 10^9\) ...

  4. [Codeforces375E]Red and Black Tree

    Problem 给定一棵有边权的树.树上每个点是黑或白的.黑白点能两两交换. 求符合任意一个白点到最近黑点的距离小于等于x时,黑白点交换次数最少为多少. Solution 明显是一题树形DP.我们先跑 ...

  5. [CodeForces-375E]Red and Black Tree

    题目大意: 给你一棵带边权的树,每个结点可能是红色或者黑色,你可以交换若干个点对使得任意一个红点到达与其最近的黑点的距离小于等于m. 思路: 动态规划. f[i][j][k]表示以i为根的子树中,连向 ...

  6. 设计模式 --深入理解javascript

    /* 一.单例模式 */ var Universe; (function () { var instance; Universe = function Universe() { if (instanc ...

  7. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  8. RH133读书笔记(11)-Lab 11 System Rescue and Troubleshooting

    Lab 11 System Rescue and Troubleshooting Goal: To build skills in system rescue procedures. Estimate ...

  9. 解读Linux命令格式(转)

    解读Linux命令格式   环境 Linux HA5-139JK 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 x86_64 x8 ...

随机推荐

  1. 打造一款属于自己的CentOS操作系统

    文章目录 声明 关闭selinux以及firewalld 修改终端前缀显示 修改默认网卡名称为eth0 替换yum源 安装常用工具 优化history 配置回收站 迎宾显示 优化vim 清空yum缓存 ...

  2. [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- (6) --- Distributed hash表

    [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- (6) --- Distributed hash表 目录 [源码解析] NVIDIA HugeCTR,GPU版本参数服务器--- ...

  3. 【Azure API 管理】API Management如何有效且快速更新呢?如对APIs/Policy等设置内容

    问题描述 APIM中的内容(API, Policy)等内容,如果有需要更新时候,通常可以在Azure APIM门户上操作,通过一个接口一个设置的修改,也可以针对一个接口导入/导出的方式修改.当APIM ...

  4. angular访问后台服务及监控会话超时的封装实现

    以前一直没有想过写一些东西来把项目中用到的知识点及技术实现做一个归纳整理并分享出来.现在打算逐渐的把项目中的一些东西整理并分享出来,与大家共勉! angular本身自带访问组件http和httpcli ...

  5. [题解]RQNOJ PID85 三个袋子

    链接:http://www.rqnoj.cn/problem/85 思路:一个排列问题,递推式很简单,f(n+1)=3*f(n)-1 ,由此可以推出通项公式,f(n)=0.5*3^(n-1)+0.5 ...

  6. 洛谷训练P1008(循环+暴力)

    1 #include<stdio.h> 2 #include<string.h> 3 int a[10]; 4 int main(){ 5 for (int x=123;x&l ...

  7. LeetCode-028-实现 strStr()

    实现 strStr() 题目描述:实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下 ...

  8. python的变量与基本数据类型

    今日内容 python多版本共存 python的注释 python的变量与常量 变量的本质 变量的命名规范 python基本数据类型 内容详细 python多版本共存 先将两个版本的python解释器 ...

  9. linux su、sudo、sudo su、sudo -i的用法和区别

    sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码,而不是超级账户的密码.不过有时间限制,Ubuntu默认一次时长15分钟. su : 切换到某某用户模式,提 ...

  10. java IO流体系--通识篇

    1.I/O流是什么 Java的I/O流是实现编程语言的输入/输出的基础能力,操作的对象有外部设备.存储设备.网络连接等等,是所有服务器端的编程语言都应该具备的基础能力. I = Input(输入),输 ...