UVA 10828 Back to Kernighan-Ritchie(高斯消元)
高斯消元求概率
对于非起点,期望x[i] = ∑x[j] / deg[j]
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
using namespace std;
typedef long long LL;
const int N = 108, INF = 0x3F3F3F3F;
const double eps = 1e-8;
double f[N][N];
std::vector<int> g[N];
bool inf[N]; template<typename T>
void gauss(T A[N][N], int n){
for(int i = 0; i < n; i++){
int r = i;
for(int j = i + 1; j < n; j++){
if(abs(A[j][i]) > abs(A[r][i])){
r = j;
}
}
if(abs(A[r][i]) < eps){
continue;
}
if(r != i){
for(int j = 0; j <= n; j++){
swap(A[r][j], A[i][j]);
}
}
for(int k = 0; k < n; k++){
if(k != i){
for(int j = n; j >= i; j--){
A[k][j] -= A[k][i] / A[i][i] * A[i][j];
}
}
}
}
} int main(){
int cas = 0, n, q;
while(~scanf("%d", &n) && n){
for(int i = 0; i <= n; i++){
g[i].clear();
}
int u, v;
while(scanf("%d %d", &u, &v) && u && v){
u--;
v--;
g[u].push_back(v);
}
memset(f, 0, sizeof(f));
for(int i = 0; i < n; i++){
f[i][i] = -1;
for(int j = 0; j < g[i].size(); j++){
f[g[i][j]][i] += 1.0 / (double)g[i].size();
}
}
f[0][n] = -1;
gauss(f, n);
memset(inf, 0, sizeof(inf));
for(int i = n - 1; i >= 0; i--){
if(abs(f[i][i]) < eps && abs(f[i][n]) > eps){
inf[i] = 1;
}
for(int j = i + 1; j < n; j++){
if(inf[j] && abs(f[i][j]) > eps){
inf[i] = 1;
break;
}
}
}
printf("Case #%d:\n", ++cas);
scanf("%d", &q);
while(q--){
int u;
scanf("%d", &u);
u--;
if(inf[u]){
printf("infinity\n");
}else{
printf("%.3f\n", (abs(f[u][u]) < eps )? 0.0 : abs(f[u][n] / f[u][u]));
}
} }
return 0;
}
UVA 10828 Back to Kernighan-Ritchie(高斯消元)的更多相关文章
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
- uva 1560 - Extended Lights Out(枚举 | 高斯消元)
题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...
- uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...
- UVA 12849 Mother’s Jam Puzzle( 高斯消元 )
题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...
- UVa 10828 Back to Kernighan-Ritchie (数学期望 + 高斯消元)
题意:给定一个 n 个结点的有向图,然后从 1 结点出发,从每个结点向每个后继结点的概率是相同的,当走到一个没有后继结点后,那么程序终止,然后问你经过每个结点的期望是次数是多少. 析:假设 i 结点的 ...
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- UVA 1397 - The Teacher's Side of Math(高斯消元)
UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...
- UVA 1564 - Widget Factory(高斯消元)
UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...
- UVa 11542 Square (高斯消元)
题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500. 析:从题目素因子不超过 500,就知道要把每个数进行分解.因为结果要 ...
随机推荐
- NOIP 2010题解
唔..NOIP2010比较简单,总体感觉不错. Problem 1: 机器翻译 水题,队列的简单应用. 读入时判断是否在内存中,可以用hash优化.如果不在内存中push进内存,放不下了pop hea ...
- 转帖:如何建立与使用 Window setup project
原文地址: http://www.codeproject.com/Articles/12548/Visual-Studio-Windows-Application-Setup-Project
- Myeclipse8.5 反编译插件 jad 安装
准备工作 下载jad.exe文件和下载jadeclipse插件:http://pan.baidu.com/s/1pJKjVwn JadClipse 官网:http://jadclipse.source ...
- windows下打开VMware虚拟机时提示内存不足的处理方法
参考:http://thinkpig007.blog.51cto.com/971471/1589831 以管理员身份运行vmware.exe即可 错误的错误提示: Not enough physica ...
- python装饰器初探
一.含有一个装饰器 #encoding: utf-8 ############含有一个装饰器######### def outer(func): def inner(*args, **kwargs): ...
- ios 使用xib时,在UIScrollView中添建内容view时,使用约束的注意
请参与一下链接:http://segmentfault.com/a/1190000002462033 简单的说下,就是必须写满一个view的6个约束,就是上下左右高宽,让scrollview 能够根据 ...
- Application.AddMessageFilter(this);
开发环境:windows 8(x64), vs2013 只要“项目属性-调试”中选中“启用Visual Studio承载进程“,在VS2013中用F5调试,调用Application.AddMessa ...
- Java for LintCode 颜色分类
给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数0,1和2分别代表红,白,蓝. 解题思路: Java for Leet ...
- centos6.5Xen4.2安装
官方安装文档:http://xen.crc.id.au/support/guides/install/ 一.环境说明 1. 本文采用CentOS6.5 x64,安装开发包及开发工具. 2. 关闭sel ...
- 2078 Problem H Secret Message 中石油-未提交-->已提交
题目描述 Jack and Jill developed a special encryption method, so they can enjoy conversations without wo ...