[CodeVS4438]YJQ Runs Upstairs

题目大意:

一个\(n(n\le50)\)个点\(m(m\le300)\)条边的DAG,保证从\(1\)到\(n\)的所有路径经过边数均小于等于\(20\)。每条边有一个边权\(w_i(w_i\le50)\),求从\(1\)到\(n\)经过边权方差最小值。

思路:

\[\frac1n\sum(x_i-\overline{x})^2=\frac1n\left[\sum x_i^2-\frac{(\sum x_i)^2}n\right]
\]

\(f[i][j][k]\)表示\(1\sim i\)的路径经过\(j\)条边,\(\sum w_i=k\)时,\(\sum w_i^2\)的最小值。根据拓扑序DP即可。

源代码:

#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=51,D=21,W=1001;
struct Edge {
int to,w;
};
std::vector<Edge> e[N];
inline void add_edge(const int &u,const int &v,const int &w) {
e[u].push_back((Edge){v,w});
}
int n,m,f[N][D][W],ind[N];
std::queue<int> q;
inline void upd(int &a,const int &b) {
a=std::min(a,b);
}
inline void kahn() {
f[1][0][0]=0;
for(register int i=1;i<=n;i++) {
if(!ind[i]) q.push(i);
}
while(!q.empty()) {
const int &x=q.front();
for(unsigned i=0;i<e[x].size();i++) {
const int &y=e[x][i].to,&w=e[x][i].w;
for(register int i=0;i+1<D;i++) {
for(register int j=0;j+w<W;j++) {
upd(f[y][i+1][j+w],f[x][i][j]+w*w);
}
}
if(!--ind[y]) q.push(y);
}
q.pop();
}
}
int main() {
memset(f,0x3f,sizeof f);
n=getint(),m=getint();
for(register int i=0;i<m;i++) {
const int u=getint(),v=getint();
add_edge(u,v,getint());
ind[v]++;
}
kahn();
double ans=1e9;
for(register int i=1;i<D;i++) {
for(register int j=0;j<W;j++) {
ans=std::min(ans,(f[n][i][j]-1.*j*j/i)/i);
}
}
printf("%.4f\n",ans);
return 0;
}

[CodeVS4438]YJQ Runs Upstairs的更多相关文章

  1. memory runs at single channel问题解决

    memory runs at single channel 解决方案:开机后按DEL ,然后进入BIOS 选择第一项,回车! advanced下面的有个momori什么什么的,选择disable. m ...

  2. What To Do When MySQL Runs Out of Memory: Troubleshooting Guide

    In this article, I will show you how to use the new version of MySQL (5.7+) and how to troubleshoot ...

  3. 时间同步ctss与ntp的关系【CTSSD Runs in Observer Mode Even Though No Time Sync Software is Running (Doc ID 1054006.1) 】

    CTSSD Runs in Observer Mode Even Though No Time Sync Software is Running (Doc ID 1054006.1) In this ...

  4. 【CF896D】Nephren Runs a Cinema 卡特兰数+组合数+CRT

    [CF896D]Nephren Runs a Cinema 题意:一个序列中有n格数,每个数可能是0,1,-1,如果一个序列的所有前缀和都>=0且总和$\in [L,R]$,那么我们称这个序列是 ...

  5. Jmeter-Maven-Plugin高级应用:Configuring the jvm that the jmeter process runs in

    Configuring the jvm that the jmeter process runs in The JMeter Maven plugin will run the JMeter proc ...

  6. CodeForces - 896D :Nephren Runs a Cinema(卡特兰数&组合数学---比较综合的一道题)

    Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. Howev ...

  7. 【CZY选讲·Yjq的棺材】

    题目描述 Yjq想要将一个长为宽为的矩形棺材(棺材表面绝对光滑,所以棺材可以任意的滑动)拖过一个L型墓道. 如图所示,L型墓道两个走廊的宽度分别是和,呈90°,并且走廊的长度远大于. 现在Hja ...

  8. MCP|LQ|DIAlignR provides precise retention time alignment across distant runs in DIA and targeted proteomics

    文献名: DIAlignR provides precise retention time alignment across distant runs in DIA and targeted prot ...

  9. PPID=1 runs as a background process, rather than being under the direct control of an interactive user

    https://en.wikipedia.org/wiki/Daemon_(computing) [后台进程,非互动] d 结尾 syslogd 系统日志记录 sshd 响应ssh连接请求 In mu ...

随机推荐

  1. gpio_request 原形代码【转】

    转自:http://blog.csdn.net/maopig/article/details/7428561 其原型为 int gpio_request(unsigned gpio, const ch ...

  2. 重装系统,出现:Units specified don't exist SHSUCDX can't install

    重装系统,出现:Units specified don't exist SHSUCDX can't install 解决方案1: 首先是你的硬盘分区不对吧 先用PQ格成ntfs或far32 进PE把C ...

  3. db_recovery_file_dest_size

    select name,space_limit,space_used,number_of_files from v$recovery_file_dest; alter system set db_re ...

  4. 001_linux下的log

    如果愿意在Linux环境方面花费些时间,首先就应该知道日志文件的所在位置以及它们包含的内容.在系统运行正常的情况下学习了解这些不同的日志文件有助于你在遇到紧急情况时从容找出问题并加以解决. 以下介绍的 ...

  5. 一个shell的面试题

    5.写一个脚本,实现判断192.168.1.024网络里,当前在线的D有哪些,能ping通则认为在线,在线输出"TP地址UP",不在线输出TP地址DOWN",无其他输出. ...

  6. 转载:编译安装Nginx(1.5.1)《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19618.html 1.5 configure详解 可以看出,configure命令至关重要,下文将详细介绍如何使用configure ...

  7. Pandas详解一

    pandas简介 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具.pandas提 ...

  8. CNN中各种各样的卷积

    https://zhuanlan.zhihu.com/p/29367273 https://zhuanlan.zhihu.com/p/28749411 以及1*1卷积核:https://www.zhi ...

  9. poj1436水平可见线

    还是线段树区间更新,这次不需要对线段离散化,但是要把线段纵坐标*2,可以举例模拟 #include<iostream> #include<cstring> #include&l ...

  10. hdu4942线段树模拟rotate操作+中序遍历 回头再做

    很有意思的题目,详细题解看这里 https://blog.csdn.net/qian99/article/details/38536559 自己的代码不知道哪里出了点问题 /* rotate操作不会改 ...