当前位置: 首页 > news >正文

2019 ICPC Greater New York Region C - PassTheBuck(概率)

题目链接


题目大意

n个人开始传球,每次要么把球传递给相邻的人,要么自己保留(保留就会赢得游戏),现在需要求给定传球的起点,问终点的人获胜的概率

解题思路

第一次做概率题就被这种无限传递的情况给吓住了,但是看了看题解,实际上就是当某轮的概率小于 1 e − 8 1e^{-8} 1e8时直接结束游戏即可。设置两个数组,一个表示当前轮每人得到球的概率,另一个表示下一轮每个人得到球的概率,然后第一个数组每轮结束后更新为第二个数组,再清空第二个数组开始下一轮游戏即可

#include <set>
#include <map>
#include <stack>
#include <queue>
#include <math.h>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=2e5+10;

bool G[50][50]={ 0 };
int degree[50]={ 0 };
int n,m,num;

double solve(int u,int v){
    double ans=0.0,roud=0.0,cur[25],nxt[25];
    for(int i=0;i<21;i++) cur[i]=nxt[i]=0.0;
    cur[u]=1.0;  //初始只有起点的人有球,概率为1
    while(roud==0.0 || roud>eps){  //第一次roud为0,后面不会为0,这里相当于特判
        ans+=roud;
        roud=0.0;
        for(int i=0;i<=n;i++) nxt[i]=0.0;
        for(int i=1;i<=n;i++){
            if(cur[i]>0.0){
                for(int j=1;j<=n;j++)
                    if(G[i][j]){  //能传递就传
                        if(i==j){ //终点的人传给本身才更新答案,其他人不能传给自己不然会导致游戏结束
                            if(i==v) roud+=cur[i]/(double)degree[i];
                        }else nxt[j]+=cur[i]/(double)degree[i];
                    }
            }
        }
        for(int i=0;i<21;i++) cur[i]=nxt[i];  //更新此轮每人得到球的概率
    }
    return ans;
}

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int x,kase,u,v;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&num);
        degree[i]=num+1;
        while(num--){
            scanf("%d",&x);
            G[i][x]=G[x][i]=1;
        }
        G[i][i]=1;
    }
    while(m--){
        scanf("%d%d%d",&kase,&u,&v);
        //cout<<solve(u,v)<<endl;
        printf("%d %.5f\n",kase,solve(u,v));
    }
    return 0;
}

相关文章:

  • Oracle11gR2安装简介
  • 2019 ICPC Greater New York Region I - RationalBase(思维+进制转换)
  • 广告营销的创新
  • Educational Codeforces Round 86 C - Yet Another Counting Problem(规律+前缀和)
  • SQL Server2005重装Performance Monitor Counter Requirement错误解决
  • UVA1616 Caravan Robbers(二分答案+小数化分数)
  • UVA1619 Feel Good(链表+前缀和)
  • Codeforces Round #637 (Div. 2) D. Nastya and Scoreboard(位运算dfs/记忆化搜索)
  • Cocoa应用程序基本运行过程
  • 我们的内心都有伤痕
  • Codeforces Round #638 (Div. 2) B. Phoenix and Beauty (构造)
  • Codeforces Round #638 (Div. 2) C Phoenix and Distribution(思维/构造)
  • Mac Application GDB Usage
  • “Shopee杯“e起来编程暨武汉大学2020年大学生程序设计大赛决赛
  • iPhone开发秘籍一书中的翻译错误
  • ----------
  • 分享的文章《人生如棋》
  • 【笔记】你不知道的JS读书笔记——Promise
  • 【每日笔记】【Go学习笔记】2019-01-10 codis proxy处理流程
  • 08.Android之View事件问题
  • C# 免费离线人脸识别 2.0 Demo
  • es的写入过程
  • iOS | NSProxy
  • JavaScript新鲜事·第5期
  • Python 反序列化安全问题(二)
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • 程序员最讨厌的9句话,你可有补充?
  • 初识 webpack
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 浮动相关
  • 关于字符编码你应该知道的事情
  • 简单实现一个textarea自适应高度
  • 世界编程语言排行榜2008年06月(ActionScript 挺进20强)
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 推荐一个React的管理后台框架
  • 智能合约开发环境搭建及Hello World合约
  • 7行Python代码的人脸识别
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # 达梦数据库知识点
  • #、%和$符号在OGNL表达式中经常出现
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (09)Hive——CTE 公共表达式
  • (8)STL算法之替换
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (附源码)spring boot火车票售卖系统 毕业设计 211004
  • (附源码)计算机毕业设计ssm-Java网名推荐系统
  • (论文阅读31/100)Stacked hourglass networks for human pose estimation
  • (免费领源码)python#django#mysql公交线路查询系统85021- 计算机毕业设计项目选题推荐
  • (中等) HDU 4370 0 or 1,建模+Dijkstra。
  • (转)chrome浏览器收藏夹(书签)的导出与导入
  • (轉貼) VS2005 快捷键 (初級) (.NET) (Visual Studio)
  • ./configure,make,make install的作用
  • .NET Core IdentityServer4实战-开篇介绍与规划