传送门:https://www.luogu.org/problemnew/show/P3317

这道题的推导公式还是比较好理解的,但是由于这个矩阵是小数的,要注意高斯消元方法的使用;

  1. #include <algorithm>
  2. #include <iterator>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <cstdlib>
  6. #include <iomanip>
  7. #include <bitset>
  8. #include <cctype>
  9. #include <cstdio>
  10. #include <string>
  11. #include <vector>
  12. #include <stack>
  13. #include <cmath>
  14. #include <queue>
  15. #include <list>
  16. #include <map>
  17. #include <set>
  18. #include <cassert>
  19.  
  20. /*
  21.  
  22. ⊂_ヽ
  23.   \\ Λ_Λ 来了老弟
  24.    \('ㅅ')
  25.     > ⌒ヽ
  26.    /   へ\
  27.    /  / \\
  28.    レ ノ   ヽ_つ
  29.   / /
  30.   / /|
  31.  ( (ヽ
  32.  | |、\
  33.  | 丿 \ ⌒)
  34.  | |  ) /
  35. 'ノ )  Lノ
  36.  
  37. */
  38.  
  39. using namespace std;
  40. #define lson (l , mid , rt << 1)
  41. #define rson (mid + 1 , r , rt << 1 | 1)
  42. #define debug(x) cerr << #x << " = " << x << "\n";
  43. #define pb push_back
  44. #define pq priority_queue
  45.  
  46. typedef long long ll;
  47. typedef unsigned long long ull;
  48. typedef long double ld;
  49. //typedef __int128 bll;
  50. typedef pair<ll ,ll > pll;
  51. typedef pair<int ,int > pii;
  52. typedef pair<int,pii> p3;
  53.  
  54. //priority_queue<int> q;//这是一个大根堆q
  55. //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
  56. #define fi first
  57. #define se second
  58. //#define endl '\n'
  59.  
  60. #define boost ios::sync_with_stdio(false);cin.tie(0)
  61. #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
  62. #define max3(a,b,c) max(max(a,b), c);
  63. #define min3(a,b,c) min(min(a,b), c);
  64.  
  65. const ll oo = 1ll<<;
  66. const ll mos = 0x7FFFFFFF; //
  67. const ll nmos = 0x80000000; //-2147483648
  68. const int inf = 0x3f3f3f3f;
  69. const ll inff = 0x3f3f3f3f3f3f3f3f; //
  70. const int mod = 1e9;
  71. const double esp = 1e-;
  72. const double PI=acos(-1.0);
  73. const double PHI=0.61803399; //黄金分割点
  74. const double tPHI=0.38196601;
  75.  
  76. template<typename T>
  77. inline T read(T&x){
  78. x=;int f=;char ch=getchar();
  79. while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
  80. while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
  81. return x=f?-x:x;
  82. }
  83.  
  84. inline void cmax(int &x,int y){if(x<y)x=y;}
  85. inline void cmax(ll &x,ll y){if(x<y)x=y;}
  86. inline void cmin(int &x,int y){if(x>y)x=y;}
  87. inline void cmin(ll &x,ll y){if(x>y)x=y;}
  88.  
  89. /*-----------------------showtime----------------------*/
  90. const int maxn = ;
  91. double mp[maxn][maxn];
  92. double a[maxn][maxn];
  93.  
  94. int n;
  95.  
  96. double Gauss(int n){
  97. for(int i=; i<n; i++){
  98. int mx = i;
  99. for(int j=i+; j<n; j++){
  100. if(fabs(a[mx][i]) < fabs(a[j][i])) mx = j;
  101. }
  102. swap(a[i], a[mx]);
  103. for(int j=i+; j<n; j++){
  104. double tmp = a[j][i]/a[i][i];
  105. for(int k=i+; k<n; k++)
  106. a[j][k] -= a[i][k] * tmp;
  107. }
  108. }
  109. double ans = ;
  110. for(int i=; i<n; i++) ans *= a[i][i];
  111. return fabs(ans);
  112. }
  113.  
  114. int main(){
  115. scanf("%d", &n);
  116. double tmp = ;
  117. for(int i=; i<=n; i++) {
  118. for(int j=; j<=n; j++) {
  119. scanf("%lf", &mp[i][j]);
  120. if(fabs(1.0 - mp[i][j]) < esp) mp[i][j] = 1.0- esp;
  121. if(i < j)tmp = tmp * (1.0-mp[i][j]);
  122. mp[i][j] = mp[i][j] / (1.0 - mp[i][j]);
  123. }
  124. }
  125.  
  126. for(int i=; i<=n; i++){
  127. for(int j=i+; j<=n; j++){
  128. a[i][j] = a[j][i] = -mp[i][j];
  129. a[i][i] += mp[i][j];
  130. a[j][j] += mp[i][j];
  131. }
  132. }
  133. printf("%.10f\n", Gauss(n) * tmp);
  134. return ;
  135. }

P3317 [SDOI2014]重建 变元矩阵树定理 高斯消元的更多相关文章

  1. 【BZOJ3534】【Luogu P3317】 [SDOI2014]重建 变元矩阵树,高斯消元

    题解看这里,主要想说一下以前没见过的变元矩阵树还有前几个题见到的几个小细节. 邻接矩阵是可以带权值的.求所有生成树边权和的时候我们有一个基尔霍夫矩阵,是度数矩阵减去邻接矩阵.而所谓变元矩阵树实际上就是 ...

  2. [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)

    In some countries building highways takes a lot of time... Maybe that's because there are many possi ...

  3. BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】

    题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...

  4. CF917D-Stranger Trees【矩阵树定理,高斯消元】

    正题 题目链接:https://www.luogu.com.cn/problem/CF917D 题目大意 给出\(n\)个点的一棵树,对于每个\(k\)求有多少个\(n\)个点的树满足与给出的树恰好有 ...

  5. Wannafly Camp 2020 Day 1D 生成树 - 矩阵树定理,高斯消元

    给出两幅 \(n(\leq 400)\) 个点的无向图 \(G_1 ,G_2\),对于 \(G_1\) 的每一颗生成树,它的权值定义为有多少条边在 \(G_2\) 中出现.求 \(G_1\) 所有生成 ...

  6. 洛谷4208 JSOI2008最小生成树计数(矩阵树定理+高斯消元)

    qwq 这个题目真的是很好的一个题啊 qwq 其实一开始想这个题,肯定是无从下手. 首先,我们会发现,对于无向图的一个最小生成树来说,只有当存在一些边与内部的某些边权值相同的时候且能等效替代的时候,才 ...

  7. SP104 Highways (矩阵树,高斯消元)

    矩阵树定理裸题 //#include <iostream> #include <cstdio> #include <cstring> #include <al ...

  8. luoguP3317 [SDOI2014]重建 变元矩阵树定理 + 概率

    首先,我们需要求的是 $$\sum\limits_{Tree} \prod\limits_{E \in Tree} E(u, v) \prod\limits_{E \notin Tree} (1 - ...

  9. CF917D. Stranger Trees & TopCoder13369. TreeDistance(变元矩阵树定理+高斯消元)

    题目链接 CF917D:https://codeforces.com/problemset/problem/917/D TopCoder13369:https://community.topcoder ...

随机推荐

  1. SSM-员工管理系统Demo---带分页和校验(含源码)

    页面展示: 前端JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  2. mybatis动态插入数据库

    <insert id="dynamicAddUser"> insert into t_user <!-- trim 对所有的表中列名 进行动态处理 --> ...

  3. 实战SpringCloud响应式微服务系列教程(第二章)

    接上一篇:实战SpringCloud响应式微服务系列教程(第一章) 1.1.2背压 背压是响应式编程的核心概念,这一节也是我们了解响应式编程的重点. 1.背压的机制 在生产者/消费者模型中,我们意识到 ...

  4. 使用用树莓派打造远程WEB服务器

    简介:系统配置Raspberry Pi 3B + Raspbian + MySQL5.7 + Tomcat 9 + Nginx + 公网IP. 工具:Win32DiskImager .FileZill ...

  5. html+css+dom补充

    补充1:页面布局 一般像京东主页左侧右侧都留有空白,用margin:0 auto居中,一般.w. <!DOCTYPE html> <html lang="en"& ...

  6. H3C软件开发笔试面试总结

    注:我目前是陕西师范大学计算机科学学院本科生,在西安参加笔试以及面试 先是笔试,我选择的是JAVA方向,笔试选择题目主要是一些基础性的题目,然后简答题问了final.finally.finallize ...

  7. 如何使用Arrays工具类操作数组

    介绍 我们要先知道Arrays 是什么. java.util.Arrays 类是 JDK 提供的一个工具类主要用来操作数组,比如数组的复制转换等各种方法,Arrays 的方法都是静态方法可以通过Arr ...

  8. js中slice和splice的区别

    言简意赅,直接上货. slice():该方法会返回一个新的数组,强调:新数组,并不会影响原来的数组.先来看看语法咋说:arrayObject.slice(start,end).其中,start必需,e ...

  9. Ubuntu 10.04下实现双网卡负载均衡

    摘要:本文主要介绍和配置 在Ubuntu下 实现 bonding,双网卡负载,bonding模式为0,好处是负载平衡,另一网卡断了,也能工作. 什么是bonding Linux bonding 驱动提 ...

  10. 理解MySQL(二)--数据库事务

    1.事务:事务内的语句,要么全部执行成功,要么全部执行失败. a)      数据库事务四要素:ACID,原子性,一致性,隔离性,持久性. b)      原子性:一个事务必须被视为不可分割的最小单元 ...