bzoj4637: 期望
Description
Input
Output
一行一个整数,即满足总道路长度最小的情况下,设计方案的美学值期望。要求保留5位小数
按边权升序加边同时缩点,忽略缩点产生的自环,对同一权值且 加入此权值的边后在同个联通块内 的一组边,用矩阵树定理计算出生成树个数以及删去每条边后的生成树个数,于是可得一条边在最小生成树中的概率,统计答案。似乎要用long double才能过,但计算行列式时无论用long double还是模意义下的整数计算都能过。
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long double ld;
#define double ld
int _(){
int x=,c=getchar(),f=;
while(c<)c=='-'&&(f=-),c=getchar();
while(c>)x=x*+c-,c=getchar();
return x*f;
}
int es[],enx[],e0[],ep=,ev[];
struct edge{
int a,b,c,d;
void init(){
a=_();b=_();c=_();d=_();
}
void adde(){
es[ep]=b;enx[ep]=e0[a];ev[ep]=d;e0[a]=ep++;
es[ep]=a;enx[ep]=e0[b];ev[ep]=d;e0[b]=ep++;
}
}e[];
bool operator<(edge a,edge b){
return a.c<b.c;
}
double ans=;
int n,m,f[],id[],idp=,idt[],tk=,ed[],ID[],IDP;
int v[][],v1,ee[][],eep,os[][],op=;
int get(int x){
int a=x,c;
while(x!=f[x])x=f[x];
while(x!=f[a])c=f[a],f[a]=x,a=c;
return x;
}
void gid(int x){
if(idt[x]!=tk)idt[x]=tk,id[x]=++idp;
}
void dfs(int w){
if(ed[w]!=tk)ed[w]=tk,ID[w]=++IDP;
for(int i=e0[w],u;i;i=enx[i]){
u=es[i];
if(ed[u]!=tk)dfs(u);
if(ID[w]<ID[u]){
++v[ID[w]][ID[w]];
++v[ID[u]][ID[u]];
--v[ID[w]][ID[u]];
--v[ID[u]][ID[w]];
ee[eep][]=ID[w];
ee[eep][]=ID[u];
ee[eep++][]=ev[i];
}
}
}
const double _0=1e-;
double solve(int n){
static double a[][];
double s=;
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
a[i][j]=v[i][j];
for(int i=;i<=n;++i){
if(fabs(a[i][i])<_0){
int t=i;
for(int j=i+;j<=n;++j)if(fabs(a[j][i])>fabs(a[t][i]))t=j;
if(fabs(a[t][i])<_0)return ;
for(int j=i;j<=n;++j)swap(a[i][j],a[t][j]);
}
for(int j=i+;j<=n;++j)if(a[j][i]){
double x=a[j][i]/a[i][i];
for(int k=i;k<=n;++k)a[j][k]-=x*a[i][k];
}
}
for(int i=;i<=n;++i)s*=a[i][i];
return s;
}
void chk(int x){
if(ed[x]==tk)return;
IDP=;++tk;eep=;
dfs(x);
double v0=solve(IDP-);
for(int i=;i<eep;++i){
int x=ee[i][],y=ee[i][];
--v[x][x],--v[y][y],++v[x][y],++v[y][x];
double v1=v0-solve(IDP-);
ans+=v1/v0*ee[i][];
++v[x][x],++v[y][y],--v[x][y],--v[y][x];
}
for(int i=;i<=IDP;++i){
for(int j=;j<=IDP;++j)v[i][j]=;
}
}
int main(){
n=_();m=_();
for(int i=;i<=n;++i)f[i]=i;
for(int i=;i<m;++i)e[i].init();
std::sort(e,e+m);
for(int i=,j=;i<m;){
for(++tk,idp=;j<m&&e[i].c==e[j].c;++j);
for(int k=i;k<j;++k){
int x=get(e[k].a),y=get(e[k].b);
if(x==y){
e[k].a=-;
continue;
}
os[op][]=e[k].a;os[op++][]=e[k].b;
gid(x);gid(y);
e[k].a=id[x];
e[k].b=id[y];
e[k].adde();
}
while(op)--op,f[get(os[op][])]=get(os[op][]);
for(;i<j;++i)if(~e[i].a){
chk(e[i].a);
chk(e[i].b);
}
for(int t=;t<=idp;++t)e0[t]=;
ep=;
}
printf("%.5Lf",ans);
return ;
}
bzoj4637: 期望的更多相关文章
- 【BZOJ4637】期望 Kruskal+矩阵树定理
[BZOJ4637]期望 Description 在米国有一所大学,名叫万国歌剧与信息大学(UniversalOperaandInformaticasUniversity).简称UOI大学.UOI大学 ...
- bzoj4637:期望
思路:最小生成树计数只不过加了一个期望,由于期望具有线性性质,就可以转化为每条边的期望之和,那么一条边的期望如何求呢,在最小生成树记数中,是把相同边权的一起处理,之后把属于连通块内的点缩点,也就是说, ...
- 【BZOJ-3143】游走 高斯消元 + 概率期望
3143: [Hnoi2013]游走 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2264 Solved: 987[Submit][Status] ...
- bzoj1415[NOI2005]聪聪和可可-期望的线性性
这道题之前我写过一个巨逗比的写法(传送门:http://www.cnblogs.com/liu-runda/p/6220381.html) 当时的原因是这道题可以抽象出和"绿豆蛙的归宿&qu ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 【BZOJ3036】绿豆蛙的归宿 概率与期望
最水的概率期望,推荐算法合集之<浅析竞赛中一类数学期望问题的解决方法> #include <iostream> #include <cstdio> using na ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- 【BZOJ-1426】收集邮票 概率与期望DP
1426: 收集邮票 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 261 Solved: 209[Submit][Status][Discuss] ...
- 【BZOJ-1419】Red is good 概率期望DP
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 660 Solved: 257[Submit][Status][Di ...
随机推荐
- 蓝牙SIG
蓝牙SIG 蓝牙SIG是一个国际性的非营利组织,它的目的是制定蓝牙的技术规范和推广蓝牙技术的应用.该组织由发起会员(Promoter).合作会员(Associate Member)和接受会员(Adop ...
- C# string[,]与string[][]的区别
对于这两者的区别: 1.入门:string[,]可读可写,而string[][]与string[]相同,不可对第二位进行写操作 static void Main(string[] args) { // ...
- HQL查询语言的使用介绍
@SuppressWarnings("deprecation") public class HibernateUtil { private static final Session ...
- xctest错误问题解决
xctest xctest.h file not found(null): Framework not found XCTest 在FrameWork Search Path里增加以下内容$(PLAT ...
- ubuntuPC机安装JLink驱动
摘要: 打开你仿真用的机器人的配置文化,这个应该是local_costmap_params.yaml transform tolerance g改为1 本文介绍了如何在Ubuntu平台配置J- ...
- 越狱Season 1-Episode 15: By the Skin and the Teeth
Season 1, Episode 15: By the Skin and the Teeth -Pope: doctor...you can leave. 医生你得离开 -Burrows: It's ...
- JS数组随机排序
var arr=[1,2,3,4,5]; arr.sort(function(a,b){ var v=Math.random()>0.5?1:-1; console.log(a,b,v); re ...
- ARTICLES
https://blogs.msdn.microsoft.com/tess/2005/12/20/things-to-ignore-when-debugging-an-asp-net-hang/ ht ...
- memcached +mysql+php 测试例子
最近看了看memcached 缓存 mysql,经过一段时间的研究·把自己测试方法写出来,给像我一样的初学者用~我的环境是 nginx + php-fpm + apc + mysql 编译安装memc ...
- Java的位运算 待整理
位运算:二进制运算 Java的异或运算^ 真^假=真 假^真=真 假^假=假 真^真= 假,这四个是在网上copy的例子,真是1,假是0 但它却是说明了Java异或运算的基本法则,那就是:只要两个条件 ...