欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - POJ1487


题解概括

   给出多个树形结构,由小写字母和数字表示,每个小写字母表示一棵小树。现在,以a为根节点,构建一棵大树,树可能是无限的。现在,一个人从树根往叶子走,直到无法走为止,得到该叶子结点上数值所表示的相应分数,人在分叉的地方走每条路的概率是一样的,求得分期望。


题解

  首先通过关系建立方程组。

  这个貌似很麻烦,但是很暴力,有码量没有难度。

  然后高斯消元解方程。

  要注意精度的问题。

  解的时候要标记自由元。

  也有点麻烦。

  具体的看代码吧。


代码

  1. #include <cstring>
  2. #include <algorithm>
  3. #include <cstdio>
  4. #include <cmath>
  5. #include <cstdlib>
  6. using namespace std;
  7. typedef long double LD;
  8. const LD Eps=1e-8;
  9. const int N=30;
  10. int n,Case=0,now,pos[N];
  11. char str[300];
  12. bool free_x[N];
  13. LD a[N][N],x[N];
  14. void GetLn(){
  15. while (getchar()!='\n');
  16. }
  17. int Match_Pracket(int now){//now为当前要匹配的左括号位置。
  18. int len=strlen(str+1),p=0;
  19. for (int i=now;i<=len;i++){
  20. if (str[i]=='(')
  21. p++;
  22. if (str[i]==')')
  23. p--;
  24. if (!p)
  25. return i;
  26. }
  27. return -1;
  28. }
  29. bool Is_Num_Part(char ch){
  30. return ch=='-'||('0'<=ch&&ch<='9');
  31. }
  32. int GetNum(int now,int &Next){//now为当前要计算的数字的最左位置,Next返回跳过数字后的位置。
  33. int len=strlen(str+1),f=1,x=0;
  34. if (str[now]=='-')
  35. f=-1;
  36. else
  37. x=str[now]-'0';
  38. while (Is_Num_Part(str[++now]))
  39. x=x*10+str[now]-'0';
  40. Next=now;
  41. return x*f;
  42. }
  43. void dfs(int L,int R,LD p){
  44. if (L>R)
  45. return;
  46. int tot=0,i;
  47. for (i=L;i<=R;){
  48. if (Is_Num_Part(str[i])){
  49. int j,v=GetNum(i,j);
  50. i=j,tot++;
  51. continue;
  52. }
  53. if (str[i]=='('){
  54. i=Match_Pracket(i)+1;
  55. tot++;
  56. continue;
  57. }
  58. if ('a'<=str[i]&&str[i]<='z'){
  59. i++,tot++;
  60. continue;
  61. }
  62. i++;
  63. }
  64. p=p/tot;
  65. for (int i=L;i<=R;){
  66. if (Is_Num_Part(str[i])){
  67. int j,v=GetNum(i,j);
  68. i=j,a[now][n]-=p*v;
  69. continue;
  70. }
  71. if (str[i]=='('){
  72. int j=Match_Pracket(i);
  73. dfs(i+1,j-1,p);
  74. i=j+1;
  75. continue;
  76. }
  77. if ('a'<=str[i]&&str[i]<='z')
  78. a[now][str[i]-'a']+=p;
  79. i++;
  80. }
  81. }
  82. int Gauss(){
  83. memset(free_x,0,sizeof free_x);
  84. memset(pos,0,sizeof pos);
  85. int k,c;
  86. for (k=c=0;k<n&&c<n;k++,c++){
  87. int Mk=k;
  88. for (int i=k+1;i<n;i++)
  89. if (fabs(a[Mk][c])<fabs(a[i][c]))
  90. Mk=i;
  91. if (Mk!=k)
  92. for (int i=c;i<=n;i++)
  93. swap(a[Mk][i],a[k][i]);
  94. if (fabs(a[k][c])<Eps){
  95. k--;
  96. free_x[c]=1;
  97. continue;
  98. }
  99. pos[k]=c;
  100. for (int i=k+1;i<n;i++)
  101. if (fabs(a[i][c])>Eps){
  102. for (int j=n;j>=c;j--)
  103. a[i][j]=a[i][j]-a[k][j]/a[k][c]*a[i][c];
  104. a[i][c]=0;
  105. }
  106. }
  107. for (int i=k;i<n;i++)
  108. if (fabs(a[i][n])>Eps)
  109. return -1;
  110. memset(x,0,sizeof x);
  111. for (int i=k-1;i>=0;i--){
  112. if (free_x[pos[i]])
  113. continue;
  114. LD tmp=a[i][n];
  115. for (int j=pos[i]+1;j<n;j++){
  116. if (fabs(a[i][j])<Eps)
  117. continue;
  118. if (free_x[j]){
  119. free_x[pos[i]]=1;
  120. break;
  121. }
  122. tmp-=a[i][j]*x[j];
  123. }
  124. if (!free_x[pos[i]])
  125. x[pos[i]]=tmp/a[i][pos[i]];
  126. if (fabs(x[pos[i]])<Eps)
  127. x[pos[i]]=0;
  128. }
  129. return 0;
  130. }
  131. int main(){
  132. while (~scanf("%d",&n)&&n){
  133. GetLn();
  134. memset(a,0,sizeof a);
  135. for (now=0;now<n;now++){
  136. a[now][now]-=1;
  137. gets(str+1);
  138. int pos=1;
  139. while (str[pos]!='=')
  140. pos++;
  141. dfs(pos+1,strlen(str+1),1);
  142. }
  143. int ans=Gauss();
  144. printf("Game %d\n",++Case);
  145. for (int i=0;i<n;i++)
  146. if (free_x[i])
  147. printf("Expected score for %c undefined\n",i+'a');
  148. else
  149. printf("Expected score for %c = %.3Lf\n",i+'a',x[i]);
  150. puts("");
  151. }
  152. return 0;
  153. }

  

POJ1487 Single-Player Games 高斯消元的更多相关文章

  1. 【CF446D】DZY Loves Games 高斯消元+矩阵乘法

    [CF446D]DZY Loves Games 题意:一张n个点m条边的无向图,其中某些点是黑点,1号点一定不是黑点,n号点一定是黑点.问从1开始走,每次随机选择一个相邻的点走过去,经过恰好k个黑点到 ...

  2. POJ 1487:Single-Player Games 浮点数高斯消元

    Single-Player Games Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1287   Accepted: 36 ...

  3. 单(single):换根dp,表达式分析,高斯消元

    虽说这题看大家都改得好快啊,但是为什么我感觉这题挺难.(我好菜啊) 所以不管怎么说那群切掉这题的大佬是不会看这篇博客的所以我要开始自嗨了. 这题,明显是树dp啊.只不过出题人想看你发疯,询问二合一了而 ...

  4. Codeforces 446D - DZY Loves Games(高斯消元+期望 DP+矩阵快速幂)

    Codeforces 题目传送门 & 洛谷题目传送门 神仙题,%%% 首先考虑所有格子都是陷阱格的情况,那显然就是一个矩阵快速幂,具体来说,设 \(f_{i,j}\) 表示走了 \(i\) 步 ...

  5. HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)

    Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players tak ...

  6. hdu 5833 Zhu and 772002 高斯消元

    Zhu and 772002 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test ...

  7. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  8. POJ 1681---Painter's Problem(高斯消元)

    POJ   1681---Painter's Problem(高斯消元) Description There is a square wall which is made of n*n small s ...

  9. HDU4870_Rating_双号从零单排_高斯消元求期望

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4870 原题: Rating Time Limit: 10000/5000 MS (Java/Other ...

随机推荐

  1. C# 与 SQL Server 的数据类型对应关系

    (一)C#与SQL Server 2005(或以下版本): C# C#取值 SQL Server SQL Server取值 System.DateTime samlltime System.Objec ...

  2. C# 批量修改文件名

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

  4. JS的call方法的作用解释,简单易懂

    先看看关于call()的官方解释,“调用一个对象的一个方法,以另一个对象替换当前对象.”,看了这样的解释,或许让你更摸不着头脑了.看例子: var x = "我是全局变量"; // ...

  5. Ngnix + Tomcat负载均衡架构

    一.nginx Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.  其特点是占有内 ...

  6. 2017-2018-2 20155303『网络对抗技术』Exp9:Web安全基础

    2017-2018-2 『网络对抗技术』Exp9:Web安全基础 --------CONTENTS-------- 一.基础问题回答 1.SQL注入攻击原理,如何防御? 2.XSS攻击的原理,如何防御 ...

  7. C++ explicit 关键字

    原文转自:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造 ...

  8. Linux文件系统1---概述

      1.引言 本文所述关于文件管理的系列文章主要是对陈莉君老师所讲述的文件系统管理知识讲座的整理.Linux可以支持不同的文件系统,它源于unix文件系统,也是unix文件系统的一大特色. 本文主要先 ...

  9. [转]RJ45接口说明

    [转]http://blog.csdn.net/dog0138/article/details/7016351 1.前言 常见的RJ45接口有两类: 用于以太网网卡.路由器以太网接口等的DTE类型,可 ...

  10. 使用C#进行应用程序间通信(WPF与Unity通信)

    首先程序主体来自网络,我只是应用在我自己的项目中,其中出现了一系列的问题,有些已经解决,有些使用了折中的方案,如果有大神能够给予知道,感激不尽! 首先是发送端程序: 这是我的程序任务执行主界面,此处已 ...