P3317 [SDOI2014]重建 变元矩阵树定理 高斯消元
传送门:https://www.luogu.org/problemnew/show/P3317
这道题的推导公式还是比较好理解的,但是由于这个矩阵是小数的,要注意高斯消元方法的使用;
- #include <algorithm>
- #include <iterator>
- #include <iostream>
- #include <cstring>
- #include <cstdlib>
- #include <iomanip>
- #include <bitset>
- #include <cctype>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <stack>
- #include <cmath>
- #include <queue>
- #include <list>
- #include <map>
- #include <set>
- #include <cassert>
- /*
- ⊂_ヽ
- \\ Λ_Λ 来了老弟
- \('ㅅ')
- > ⌒ヽ
- / へ\
- / / \\
- レ ノ ヽ_つ
- / /
- / /|
- ( (ヽ
- | |、\
- | 丿 \ ⌒)
- | | ) /
- 'ノ ) Lノ
- */
- using namespace std;
- #define lson (l , mid , rt << 1)
- #define rson (mid + 1 , r , rt << 1 | 1)
- #define debug(x) cerr << #x << " = " << x << "\n";
- #define pb push_back
- #define pq priority_queue
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- //typedef __int128 bll;
- typedef pair<ll ,ll > pll;
- typedef pair<int ,int > pii;
- typedef pair<int,pii> p3;
- //priority_queue<int> q;//这是一个大根堆q
- //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
- #define fi first
- #define se second
- //#define endl '\n'
- #define boost ios::sync_with_stdio(false);cin.tie(0)
- #define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
- #define max3(a,b,c) max(max(a,b), c);
- #define min3(a,b,c) min(min(a,b), c);
- const ll oo = 1ll<<;
- const ll mos = 0x7FFFFFFF; //
- const ll nmos = 0x80000000; //-2147483648
- const int inf = 0x3f3f3f3f;
- const ll inff = 0x3f3f3f3f3f3f3f3f; //
- const int mod = 1e9;
- const double esp = 1e-;
- const double PI=acos(-1.0);
- const double PHI=0.61803399; //黄金分割点
- const double tPHI=0.38196601;
- template<typename T>
- inline T read(T&x){
- x=;int f=;char ch=getchar();
- while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
- while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
- return x=f?-x:x;
- }
- inline void cmax(int &x,int y){if(x<y)x=y;}
- inline void cmax(ll &x,ll y){if(x<y)x=y;}
- inline void cmin(int &x,int y){if(x>y)x=y;}
- inline void cmin(ll &x,ll y){if(x>y)x=y;}
- /*-----------------------showtime----------------------*/
- const int maxn = ;
- double mp[maxn][maxn];
- double a[maxn][maxn];
- int n;
- double Gauss(int n){
- for(int i=; i<n; i++){
- int mx = i;
- for(int j=i+; j<n; j++){
- if(fabs(a[mx][i]) < fabs(a[j][i])) mx = j;
- }
- swap(a[i], a[mx]);
- for(int j=i+; j<n; j++){
- double tmp = a[j][i]/a[i][i];
- for(int k=i+; k<n; k++)
- a[j][k] -= a[i][k] * tmp;
- }
- }
- double ans = ;
- for(int i=; i<n; i++) ans *= a[i][i];
- return fabs(ans);
- }
- int main(){
- scanf("%d", &n);
- double tmp = ;
- for(int i=; i<=n; i++) {
- for(int j=; j<=n; j++) {
- scanf("%lf", &mp[i][j]);
- if(fabs(1.0 - mp[i][j]) < esp) mp[i][j] = 1.0- esp;
- if(i < j)tmp = tmp * (1.0-mp[i][j]);
- mp[i][j] = mp[i][j] / (1.0 - mp[i][j]);
- }
- }
- for(int i=; i<=n; i++){
- for(int j=i+; j<=n; j++){
- a[i][j] = a[j][i] = -mp[i][j];
- a[i][i] += mp[i][j];
- a[j][j] += mp[i][j];
- }
- }
- printf("%.10f\n", Gauss(n) * tmp);
- return ;
- }
P3317 [SDOI2014]重建 变元矩阵树定理 高斯消元的更多相关文章
- 【BZOJ3534】【Luogu P3317】 [SDOI2014]重建 变元矩阵树,高斯消元
题解看这里,主要想说一下以前没见过的变元矩阵树还有前几个题见到的几个小细节. 邻接矩阵是可以带权值的.求所有生成树边权和的时候我们有一个基尔霍夫矩阵,是度数矩阵减去邻接矩阵.而所谓变元矩阵树实际上就是 ...
- [spoj104][Highways] (生成树计数+矩阵树定理+高斯消元)
In some countries building highways takes a lot of time... Maybe that's because there are many possi ...
- BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】
题目链接 BZOJ4031 题解 第一眼:这不裸的矩阵树定理么 第二眼:这个模\(10^9\)是什么鬼嘛QAQ 想尝试递归求行列式,发现这是\(O(n!)\)的.. 想上高斯消元,却又处理不了逆元这个 ...
- CF917D-Stranger Trees【矩阵树定理,高斯消元】
正题 题目链接:https://www.luogu.com.cn/problem/CF917D 题目大意 给出\(n\)个点的一棵树,对于每个\(k\)求有多少个\(n\)个点的树满足与给出的树恰好有 ...
- Wannafly Camp 2020 Day 1D 生成树 - 矩阵树定理,高斯消元
给出两幅 \(n(\leq 400)\) 个点的无向图 \(G_1 ,G_2\),对于 \(G_1\) 的每一颗生成树,它的权值定义为有多少条边在 \(G_2\) 中出现.求 \(G_1\) 所有生成 ...
- 洛谷4208 JSOI2008最小生成树计数(矩阵树定理+高斯消元)
qwq 这个题目真的是很好的一个题啊 qwq 其实一开始想这个题,肯定是无从下手. 首先,我们会发现,对于无向图的一个最小生成树来说,只有当存在一些边与内部的某些边权值相同的时候且能等效替代的时候,才 ...
- SP104 Highways (矩阵树,高斯消元)
矩阵树定理裸题 //#include <iostream> #include <cstdio> #include <cstring> #include <al ...
- luoguP3317 [SDOI2014]重建 变元矩阵树定理 + 概率
首先,我们需要求的是 $$\sum\limits_{Tree} \prod\limits_{E \in Tree} E(u, v) \prod\limits_{E \notin Tree} (1 - ...
- CF917D. Stranger Trees & TopCoder13369. TreeDistance(变元矩阵树定理+高斯消元)
题目链接 CF917D:https://codeforces.com/problemset/problem/917/D TopCoder13369:https://community.topcoder ...
随机推荐
- SSM-员工管理系统Demo---带分页和校验(含源码)
页面展示: 前端JSP: <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- mybatis动态插入数据库
<insert id="dynamicAddUser"> insert into t_user <!-- trim 对所有的表中列名 进行动态处理 --> ...
- 实战SpringCloud响应式微服务系列教程(第二章)
接上一篇:实战SpringCloud响应式微服务系列教程(第一章) 1.1.2背压 背压是响应式编程的核心概念,这一节也是我们了解响应式编程的重点. 1.背压的机制 在生产者/消费者模型中,我们意识到 ...
- 使用用树莓派打造远程WEB服务器
简介:系统配置Raspberry Pi 3B + Raspbian + MySQL5.7 + Tomcat 9 + Nginx + 公网IP. 工具:Win32DiskImager .FileZill ...
- html+css+dom补充
补充1:页面布局 一般像京东主页左侧右侧都留有空白,用margin:0 auto居中,一般.w. <!DOCTYPE html> <html lang="en"& ...
- H3C软件开发笔试面试总结
注:我目前是陕西师范大学计算机科学学院本科生,在西安参加笔试以及面试 先是笔试,我选择的是JAVA方向,笔试选择题目主要是一些基础性的题目,然后简答题问了final.finally.finallize ...
- 如何使用Arrays工具类操作数组
介绍 我们要先知道Arrays 是什么. java.util.Arrays 类是 JDK 提供的一个工具类主要用来操作数组,比如数组的复制转换等各种方法,Arrays 的方法都是静态方法可以通过Arr ...
- js中slice和splice的区别
言简意赅,直接上货. slice():该方法会返回一个新的数组,强调:新数组,并不会影响原来的数组.先来看看语法咋说:arrayObject.slice(start,end).其中,start必需,e ...
- Ubuntu 10.04下实现双网卡负载均衡
摘要:本文主要介绍和配置 在Ubuntu下 实现 bonding,双网卡负载,bonding模式为0,好处是负载平衡,另一网卡断了,也能工作. 什么是bonding Linux bonding 驱动提 ...
- 理解MySQL(二)--数据库事务
1.事务:事务内的语句,要么全部执行成功,要么全部执行失败. a) 数据库事务四要素:ACID,原子性,一致性,隔离性,持久性. b) 原子性:一个事务必须被视为不可分割的最小单元 ...