[WC 2010]重建计划
Description
Input
第一行包含一个正整数N,表示X国的城市个数. 第二行包含两个正整数L和U,表示政策要求的第一期重建方案中修建道路数的上下限 接下来的N-1行描述重建小组的原有方案,每行三个正整数Ai,Bi,Vi分别表示道路(Ai,Bi),其价值为Vi 其中城市由1..N进行标号
Output
输出最大平均估值,保留三位小数
Sample Input
2 3
1 2 1
1 3 2
1 4 3
Sample Output
HINT
N<=100000,1<=L<=U<=N-1,Vi<=1000000
题解
本机实测是可以 $A$ 的,但爆炸 $oj$ 的老爷机实在不可恭维,并且还多加了一组更为毒瘤的数据...
加了所有的常数优化都过不了,气愤的不想写题解,直接丢链接。
//It is made by Awson on 2018.1.5
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define RE register
#define lowbit(x) ((x)&(-(x)))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = ;
const int INF = ~0u>>;
const double eps = 4e-;
void read(int &x) {
char ch; x = ;
ch = getchar(); while (ch < '' || ch > '') ch = getchar();
while (ch >= '' && ch <= '') x = (x<<)+(x<<)+ch-, ch = getchar();
} int n, L, U, a, b, c;
struct tt {
int to, next;
double cost, c;
}edge[(N<<)+];
int path[N+], top;
int root[N+];
void add(int u, int v, double c) {
edge[++top].to = v;
edge[top].cost = edge[top].c = c;
edge[top].next = path[u];
path[u] = top;
} namespace PRE {
int size[N+], mx[N+], minsize, rt, vis[N+], tot;
void get_root(int o, int pa, int fa) {
mx[o] = Max(mx[o], size[pa]-size[o]);
if (mx[o] < minsize) minsize = mx[o], rt = o;
for (RE int i = path[o]; i; i = edge[i].next)
if (edge[i].to != fa && !vis[edge[i].to]) get_root(edge[i].to, pa, o);
}
void get_size(int o, int fa) {
size[o] = , mx[o] = ;
for (RE int i = path[o]; i; i = edge[i].next)
if (edge[i].to != fa && !vis[edge[i].to]) {
get_size(edge[i].to, o);
size[o] += size[edge[i].to];
if (size[edge[i].to] > mx[o]) mx[o] = size[edge[i].to];
}
}
void work(int o) {
minsize = INF;
get_size(o, ), get_root(o, o, );
root[++tot] = rt, vis[rt] = ;
for (RE int i = path[rt]; i; i = edge[i].next)
if (!vis[edge[i].to]) work(edge[i].to);
}
void main() {work(); }
} double mx[N+], dist[N+];
int q[N+], vis[N+], dep[N+], dq[N+], fa[N+]; bool cal(int o) {
int maxdep = ;
for (RE int I = path[o]; I; I = edge[I].next)
if (!vis[edge[I].to]) {
int head = , tail = ; q[tail] = edge[I].to, dep[q[]] = , dist[q[]] = edge[I].cost, fa[q[]] = o, ++tail;
while (head < tail) {
int now = q[head]; ++head;
for (RE int i = path[now]; i; i = edge[i].next)
if (fa[now] != edge[i].to && !vis[edge[i].to]) {
q[tail] = edge[i].to, ++tail;
dep[edge[i].to] = dep[now]+;
dist[edge[i].to] = dist[now]+edge[i].cost;
fa[edge[i].to] = now;
}
}
int head1 = , tail1 = , now = maxdep;
for (RE int i = ; i < tail; ++i) {
int x = q[i];
while (dep[x]+now >= L && now >= ) {
while (head1 < tail1 && mx[dq[tail1-]] < mx[now]) --tail1;
dq[tail1] = now; ++tail1, --now;
}
while (head1 < tail1 && dq[head1]+dep[x] > U) ++head1;
if (head1 < tail1 && dist[x]+mx[dq[head1]] >= ) return true;
}
maxdep = Max(maxdep, dep[q[tail-]]);
for (RE int i = ; i < tail; ++i) {
fa[q[i]] = ; if (mx[dep[q[i]]] < dist[q[i]]) mx[dep[q[i]]] = dist[q[i]];
}
}
for (RE int i = ; i <= maxdep; i++) mx[i] = -INF;
return false;
}
bool solve(int o, int &num) {
vis[o] = ;
if (cal(o)) return true;
for (RE int i = path[o]; i ;i = edge[i].next)
if (!vis[edge[i].to]) {
++num; if (solve(root[num], num)) return true;
}
return false;
}
void pre(double key) {
for (RE int i = ; i <= n; ++i) {
edge[i<<].cost = edge[i<<].c-key, edge[(i<<)-].cost = edge[(i<<)-].c-key;
vis[i] = , mx[i] = -INF;
}
}
void work() {
read(n), read(L), read(U);
double L = , R = ;
for (RE int i = ; i < n; ++i) {
read(a), read(b), read(c);
add(a, b, c), add(b, a, c); if (R < c) R = c;
}
PRE::main();
while (R-L > eps) {
double mid = (L+R)/.; pre(mid);
int tot = ;
if (solve(root[], tot)) L = mid;
else R = mid;
}
printf("%.3lf\n", (L+R)/.);
}
int main() {
work();
return ;
}
[WC 2010]重建计划的更多相关文章
- BZOJ1758: [Wc2010]重建计划
题解: 这题我居然做了一星期?... 平均值的极值其实也可以算是一种分数规划,只不过分母上b[i]=1 然后我们就可以二分这个值.类似与 HNOI最小圈 如果没有 链的长度的限制的话,我们直接两遍df ...
- bzoj1758Wc10重建计划——solution
1758: [Wc2010]重建计划 Time Limit: 40 Sec Memory Limit: 162 MBSubmit: 4707 Solved: 1200[Submit][Status ...
- BZOJ 1758 【WC2010】 重建计划
题目链接:重建计划 这道题现在已经成为一道板子题了…… 这是个非常显然的0-1分数规划,可以二分答案之后树分治判定一下.注意树分治的时候如果使用单调队列,需要把所有儿子预先按最大深度排好序,否则会被扫 ...
- 洛谷 P4292 [WC2010]重建计划 解题报告
P4292 [WC2010]重建计划 题目描述 \(X\)国遭受了地震的重创, 导致全国的交通近乎瘫痪,重建家园的计划迫在眉睫.\(X\)国由\(N\)个城市组成, 重建小组提出,仅需建立\(N-1\ ...
- [WC2010]重建计划 长链剖分
[WC2010]重建计划 LG传送门 又一道长链剖分好题. 这题写点分治的人应该比较多吧,但是我太菜了,只会长链剖分. 如果你还不会长链剖分的基本操作,可以看看我的长链剖分总结. 首先一看求平均值最大 ...
- 【BZOJ1758】【WC2010】重建计划(点分治,单调队列)
[BZOJ1758][WC2010]重建计划(点分治,单调队列) 题面 BZOJ 洛谷 Description Input 第一行包含一个正整数N,表示X国的城市个数. 第二行包含两个正整数L和U,表 ...
- 「WC2010」重建计划(长链剖分/点分治)
「WC2010」重建计划(长链剖分/点分治) 题目描述 有一棵大小为 \(n\) 的树,给定 \(L, R\) ,要求找到一条长度在 \([L, R]\) 的路径,并且路径上边权的平均值最大 \(1 ...
- [bzoj 1758] 重建计划
bzoj 1758 重建计划 题意: 给定一棵有边权的树和两个数 \(L, R (L\leq R)\),求一条简单路径,使得这条路径经过的边数在 \(L, R\) 之间且路径经过的边的边权的平均值最大 ...
- bzoj 1758 [Wc2010]重建计划 分数规划+树分治单调队列check
[Wc2010]重建计划 Time Limit: 40 Sec Memory Limit: 162 MBSubmit: 4345 Solved: 1054[Submit][Status][Disc ...
随机推荐
- C语言:第0次作业
问题1: 你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 感性地讲,高中时意外看到了电影<社交网络>,自那时起就将将马克扎克伯格视为偶像,他天才的智慧和长远的眼光深深吸引了我 ...
- django BBS
https://github.com/triaquae/py_training/tree/master/OldboyBBS2 http://www.cnblogs.com/zhming26/p/592 ...
- Django 个性化管理员站点
from django.contrib import admin # Register your models here. from .models import Moment class Momen ...
- Tomcat 8项目无法启动,无报错
作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: Log4j:[2015-10-29 ...
- NFC驱动调试
1.NFC基本概念: NFC 又称为近场通信,是一种新兴技术,可以在彼此靠近的情况下进行数据交换,是由非接触式射频识别(RFID) 及互连互通技术整合演变而来,通过单一芯片集成感应式读卡器: NFC有 ...
- JAVA_SE基础——13.选择结构语句
if选择结构 语法: if(条件){ 代码块 } public class Test{ public static void main(String[] args){ int a = 5; if(a ...
- MySQL关系表查询两个表的数据
如下,有四张表:游戏类型表,游戏表,点卡和游戏关系表,点卡表 CREATE TABLE `gamesType`( `tId` INT AUTO_INCREMENT NOT NULL PRIMARY K ...
- URL编码和Base64编码 (转)
我们经常会遇到所谓的URL编码(也叫百分号编码)和Base64编码. 先说一下Bsae64编码.BASE64编码是一种常用的将二进制数据转换为64个可打印字符的编码,常用于在通常处理文本数据 ...
- GZip 压缩及解压缩
/// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...
- [UWP]针对UWP程序多语言支持的总结,含RTL
UWP 对 Globalization and localization 的支持非常好,可以非常容易地实现应用程序本地化. 所谓本地化,表现最为直观的就是UI上文字和布局方式了,针对文字,提供不同的语 ...