LightOJ1287---Where to Run (概率dp)

网络安全实战:从信息收集到权限提升的完整攻防技术体系 在网络安全领域,信息收集与权限提升是攻防对抗的核心基础。信息收集作为安全评估的起点,遵循从被动到主动、由外至内的技术原理,通过搜索引擎语法、DNS枚举、网络空间测绘等手段,系统性地发现和梳理目标资产,为后续行动奠定基础。权限提升则是在获得初始访问后,利用系统配置、软件漏洞或信任关系,突破权限边界的关键技术,其价值在于将有限的访问点转化为对整个系统的控制能力。这一技术体系广泛应用于渗透测试、红蓝对抗、应急响应等场景,深刻体现了攻击链的完整逻辑。本文结合渗透测试与内网横向移动等热词,深入剖析从外围信息测绘到系统 阅读详情

Last night you robbed a bank but couldn’t escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which are connected by some bidirectional roads.

Since police is behind, you have nothing to do but to run. You don’t know whether you would get caught or not, but if it is so, you want to run as long as you can. But the major problem is that if you leave a junction, next time you can’t come to this junction, because a group of police wait there for you as soon as you left it, while some other keep chasing you.

That’s why you have made a plan to fool the police as longer time as possible. The plan is, from your current junction, you first find the number of junctions which are safe (no police are there) and if you go to one of them; you are still able to visit all the safe junctions (in any order) maintaining the above restrictions. You named them ‘Elected Junction’ or EJ. If there is no such junction; you stop running, because you lose your mind thinking what to do, and the police catch you immediately.

But if there is at least one EJ, you can either fool around the police by staying in the current junction for 5 minutes (actually you just hide there, so the police lose your track thinking which road you might have taken), or you can choose to go to any EJ. The probability of choosing to stay in the current junction or to go to each of the EJ is equal. For example, from the current junction you can go to three EJs, that means the probability of staying in the current junction is 1/4 or the probability to go to any of the EJ is 1/4 since you have four options (either stay in the current junction or go to any of the three junctions).

You can fool the police (by hiding) multiple times in a city, but of course the above conditions should be satisfied. And you have decided not to stop in the middle of any road, because you have the fear that, if you stop in the middle of any road, then the police would surround you from both ends.

Now, given the map of the city and the required time for you to travel in each road of the map; you have to find the expected time for the police to catch you.
Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case starts with a blank line. Next line contains two integers n (1 ≤ n ≤ 15) denoting the number of junctions and m, denoting the number of roads in the city. The junctions are numbered from 0 to n - 1.

Each of the next m lines contains three integers u v w (0 ≤ u, v < n, 0 < w ≤ 100, u ≠ v) meaning that there is a road between junction u and v and you need w minutes to travel in the road. Your home is in junction 0 and you are initially in your home. And you may safely assume that there can be at most one road between a pair of junctions.
Output

For each case, print the case number and the expected time in minutes. Errors less than 10-6 will be ignored.
Sample Input

Output for Sample Input

3

3 2

0 1 3

1 2 3

4 6

0 1 75

0 2 86

0 3 4

1 2 1

1 3 53

2 3 10

5 5

0 1 10

1 2 20

2 3 30

1 3 20

3 4 10

Case 1: 16

Case 2: 106.8333333333

Case 3: 90
Note

For the 3rd case, initially you are in junction 0, and you can either stay here for 5 minutes, or you can move to 1. The probability of staying in 0 is 0.5 and the probability of going to junction 1 is also 0.5. Now if you are in junction 1, either you can stay here for 5 minutes or you can move to junction 2. From junction 1, you cannot move to junction 3, because if you go to junction 3, you can move to junction 2 or junction 4, but if you go to 2, you cannot visit junction 4 (since police would have occupied junction 3), and if you go to junction 4 from 3, you cannot visit junction 2 for the same reason. So, from 1, junction 2 is the only EJ, but junction 3 is not.

给你一张无向图,从0出发,每次都先观察,从当前点走到下一个点,如果从那个点开始可以遍历完全部的点,那么这个点成为EJ
如果EJ个数为0,不行动
否则,可以选择在往任意一个EJ走,还可以先在原地停留5分钟
概率都相同,求遍历完图的期望时间

很明显的概率dp
dp[u][sta]表示当前在点u,走过的点的状态为sta时,遍历完图的期望时间
至于那个EJ点个数判断,可以事先dfs下来然后保存好,注意也要记忆化搜索

    /*************************************************************************
        > File Name: K.cpp
        > Author: ALex
        > Mail: zchao1995@gmail.com
        > Created Time: 2015年05月18日 星期一 18时29分15秒
     ************************************************************************/

    #include <functional>
    #include <algorithm>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cstdio>
    #include <cmath>
    #include <cstdlib>
    #include <queue>
    #include <stack>
    #include <map>
    #include <bitset>
    #include <set>
    #include <vector>

    using namespace std;

    const double pi = acos(-1.0);
    const int inf = 0x3f3f3f3f;
    const double eps = 1e-15;
    typedef long long LL;
    typedef pair <int, int> PLL;

    vector <PLL> roads[20];
    int n;
    double dp[16][(1 << 15) + 10];
    int ok[16][(1 << 15) + 10];

    double DP(int u, int sta) {
        double &res = dp[u][sta];
        if (res != -1.0) {
            return res;
        }
        int cnt = 0;
        int size = roads[u].size();
        for (int i = 0; i < size; ++i) {
            int v = roads[u][i].first;
            if (sta & (1 << v)) {
                continue;
            }
            if (ok[v][sta | (1 << u)] == 1) {
                ++cnt;
            }
        }
        if (!cnt) {
            return res = 0;
        }
        res = 0;
        double p = 1.0 / (1 + cnt);
        for (int i = 0; i < size; ++i) {
            int v = roads[u][i].first;
            if (sta & (1 << v)) {
                continue;
            }
            if (!ok[v][sta | (1 << u)]) {
                continue;
            }
            int w = roads[u][i].second;
            res += p * w + p * DP(v, sta | (1 << u));
        }
        res += 5.0 * p;
        res /= (1 - p);
        return res;
    }

    void dfs(int u, int sta) {
        if ((sta | (1 << u)) == (1 << n) - 1) {
            ok[u][sta] = 1;
            dp[u][sta] = 0;
            return;
        }
        ok[u][sta] = 0;
        int size = roads[u].size();
        for (int i = 0; i < size; ++i) {
            int v = roads[u][i].first;
            if (!(sta & (1 << v))) {
                if (ok[v][sta | (1 << u)] == -1) {
                    dfs(v, sta | (1 << u));
                }
                ok[u][sta] = (ok[u][sta] || ok[v][sta | (1 << u)]);
            }  
        }
    }

    int main() {
        int t, icase = 1;
        scanf("%d", &t);
        while (t--) {
            int m;
            scanf("%d%d", &n, &m);
            for (int i = 0; i < n; ++i) {
                roads[i].clear();
            }
            int u, v, w;
            for (int i = 0; i < m; ++i) {
                scanf("%d%d%d", &u, &v, &w);
                roads[u].push_back(make_pair(v, w));
                roads[v].push_back(make_pair(u, w));
            }
            for (int i = 0; i < (1 << n); ++i) {
                for (int j = 0; j < n; ++j) {
                    dp[j][i] = -1.0;
                    ok[j][i] = -1;
                }
            }
            dfs(0, 0);
            printf("Case %d: %.12f\n", icase++, DP(0, 0));
        }
        return 0;
    }
51单片机电话键盘与LCD1602实战:从原理图到状态机 嵌入式开发入门常以单片机为核心载体,而51单片机因其寄存器直观、资源精简、成本低廉,成为硬件思维训练的理想平台。其核心价值在于暴露底层时序、IO控制与外设协同的真实约束,而非隐藏复杂性的抽象封装。矩阵键盘涉及行扫描与电平判别,LCD1602依赖严格忙信号检测与时序配合,二者共同构成‘感知-处理-输出’闭环的最小可行系统。该组合广泛应用于课程设计、毕业设计及初学者工程实践,支撑硬件原理图阅读、C语言指针操作、定时器中断与分层状态机等关键能力培养。本文聚焦STC89C52RC+4×4矩阵键盘+LCD1602的完 阅读详情

相关推荐

Mplus实战:随机截距交叉滞后模型(RI-CLPM)在心理学纵向研究中的进阶应用

本文深入探讨了随机截距交叉滞后模型(RI-CLPM)在心理学纵向研究中的进阶应用,通过Mplus实战演示了模型设定、结果解读及进阶建模策略。RI-CLPM能有效区分个体间差异和个体内变化,适用于追踪青少年手机使用与抑郁症状等研究。文章还提供了样本量规划、模型比较和结果可视化等实用技巧,助力研究者避免常见陷阱。

weixin_29220405的博客 374

LightOJ - 1287 Where to Run —— 期望、状压DP

题目链接:https://vjudge.net/problem/LightOJ-1287 1287 - Where to Run PDF (English) Statistics Forum Time Limit:2 second(s) Memory Limit:32 MB Last n...

alince20008的博客 275

多台树莓派(主机)通信

在做无人机实验时,有个任务需要 两台无人机 或 无人机与小车 进行通信。 由于我们飞机上用了树莓派,相当于一台电脑,索性选用Socket网络编程。 条件:树莓派、局域网(手机热点或路由器)。需要通信的两台树莓派接入同一个局域网。 如若现场没有网络,可将树莓派配置为无线路由器,发射wifi。网上很多教程,注意一点是树莓派需要先用网线接入路由器再配置。 发送数据: # -*- coding...

LNL_LNL的博客 7695

LightOJ-1287 Where to Run(期望dp)

K - Where to Run  LightOJ - 1287  题意:有n个城市,编号为0~n-1,有m条有权无向边,有个小偷从编号为0的城市出发,他不会走已经走过的城市, 他有2种决策:①停留在这个城市5分钟;②如果城市i与现在所在城市相邻,且能在走到i后还能把剩下的城市遍历,则称i为EJ。在当前城市如果有cnt个EJ,那么他选择走向每个EJ的概率和停留在该城市的概率均为1

十分残念的博客 793

17【Protues单片机仿真】基于51单片机的太阳能智能谷物翻晒机器人

基于51单片机,避障,低于50CM报警,LED灯亮起,自动翻晒用光敏电阻,光照强度大,电机转动,相当于翻晒粮食,远程控制用按键代替,按下去电机就转动,相当于翻晒粮食,然后光照强度和超声波的距离都在LCD1602显示屏显示出来。

weixin_70360261的博客 1452

文石NoteX3国行版安装 Google Play经验分享

文石NoteX3国行版安装 Google Play经验分享。

2301_80693261的博客 7658

LightOJ - 1287 Where to Run?(dfs+概率dp)

题目链接:Where to Run? - LightOJ 1287 - Virtual Judge (ppsucxtt.cn) 题意:有n个城市,编号为0~n-1,有m条有权无向边,边权代表经过这条边所要花费的时间,有个小偷从编号为0的城市出发,他不会走已经走过的城市,每到一个城市,他都会进行选择,要么选择在该城市停留5分钟,要么就前往下一个城市,他所前往的下一个城市具备以下性质: (1)在该城市可以不经过已经经过的城市且把剩余未经过的城市全部遍历 (2)选择任何一个满足性质的城市的概率是相同的,且与

AC__dream的博客 296

【C#学习记录】如何让界面控件实现自适应布局(Winform)

小伙伴们大家好,我是雷工!在软件界面设计中,客户常常要求设计的界面可以随意缩放,缩放过程中,界面中的按钮等控件也会随着窗体变大缩小自动调整显示位置和尺寸大小。在C#的Winform窗体中如何实现这个效果,下面我们一起学习下。

做有价值的事,积累有价值的笔记! 1万+

LightOJ 1287 Where to Run(期望)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0。你开始遍历这个图,每个点只能被遍历一次。当你在某个点u时,假设u可以到达v1,v2且到达v1或者v2后均可以将其他未遍历的点遍历完,则你在u有三种选择:1、在u再呆5分钟;2、去v1,;3、去v2。概率均为1/3。...

weixin_34281537的博客 267

pyecharts在jupyter notebook中不能够渲染图表问题。

在使用jupyter notebook中使用pyecharts绘制可视化图表的时候,发现图表不能渲染到页面中,生成的html是没问题的,本文主要解决在jupyter notebook中不能渲染这个问题。

猿小白的博客 1175

时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测

时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测 目录时间序列预测 | Python实现ARIMAX和VARMAX时间序列数据预测基本介绍ARIMAXVARVARMAVARMAXSESHWES参考资料 基本介绍 本文介绍ARIMAX、VAR、VARMA和VARMAX基本概念及其Python程序设计。 ARIMAX 具有外源回归量的自回归整合移动平均值(ARIMAX)是 ARIMA 模型的扩展,其还包括外生变量的建模。 外生变量也称为协变量,可以被认为是并行输入序列,其在与

前程算法屋的博客 6693

LightOJ - 1287 Where to Run (概率期望*)

题目:有一个小偷要遍历n个城市,每个城市只能走一次,在一个城市他可以选择在这等待5分钟或者选择下一个城市(条件是这下一个城市能把剩下的城市都走完)走,选择某下一个城市与当前停留在原点是等概率的,求遍历完整个图的期望时间。 思路:一共15个城市可以想到状压,dfs一遍把所有的城市的状态都判断下能不能到达vis[u][s],和这种状态下有多少个选择cnt[u][s],然后dp[u][s]表示遍历到u...

XFire的博客 437

爬虫实战:拆解WASM加密参数,破局2026电商动态接口反爬(实测某头部电商可用)

sign和wasm_token,而且每次请求这两个参数都在动态变化。用常规的参数拼接、MD5加密尝试还原,结果全是403;反编译前端JS,发现核心加密逻辑被打包成了.wasm,把关键参数加密、签名逻辑藏在二进制模块里,让爬虫无法直接逆向JS拿到加密规则。前后折腾了5天,从WASM文件解析、JS与WASM交互逻辑拆解,到本地还原加密参数、实战验证,踩了无数坑(比如WASM版本不兼容、内存数据读取失败、加密依赖环境缺失),最终成功还原了加密流程,稳定爬取商品详情、价格、销量等核心数据,连续运行72小时无封禁。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 1918

Where to Run LightOJ - 1287(概率dp)

Where to Run LightOJ - 1287(概率dp) 题面长长的,看了半天也没看懂题意 不清楚的地方,如何判断一个点是否是EJ 按照我的理解 在一个EJ点处,要么原地停留五分钟接着走,要么直接走,但是这样样例都对不上 mmp,原来是说,在原地停留五分钟后再继续这两种选择 而且题目问的是 求被警察抓的期望时间,实际上却是求遍历完整张图的期望时间,也就是说总是会走是EJ的点 对于每个点...

weixin_34203426的博客 149

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

lightoj 1287 Where to Run

lightoj 1287 概率DP,记忆化DP,状态压缩DP

ACdream 665

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

light1287Where to Run:概率DP

lightoj1287 题意:如果能通过下一个点走完所有的点,那么这个点就叫EJ。如果EJ个数为0,不行动。否则,可以选择往任意一个EJ走或者在原地停留5分钟。求警察抓到你的期望时间。 题解 状态压缩+概率DP。 假设在u点有k种选择,包括停留5分钟。那么那么状态转移方程为: 则。注意当往其它点走时,要保证能够走完所有的点。 dp[s][u]表示已经走完的状态为s,目前处于i点,走完所...

FlyingJack 167

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Loj-1287 Where to Run(状压期望DP)

Loj-1287 Where to Run(状压期望DP) 题目链接:LightOJ - 1287 题意: ​ 先给一个n个点m条无向边的图,每条边都有一个权值,顶点编号从0开始,刚开始自己站在0号点,现在要躲避警察的追踪。走的路线有如下限制: 离开一个顶点之后就不能回到该顶点 如果站在某个顶点u,剩下的未走的邻接顶点不存在 E J 顶点就停止并被警察抓到,E J 顶点指如果到该顶点后,...

Dch19990825的博客 290

LightOJ - 1287 - Where to Run (状压期望dp)

1287 - Where to Run PDF (English) Statistics Forum Time Limit:2 second(s) Memory Limit:32 MB Last night you robbed a bank but couldn't escape and when you just got outside today...

LSD20164388的博客 392

LightOJ 1287 Where to Run 期望DP+状压+记忆化搜索

Where to Run LightOJ - 1287 给一个有 nnn 个结点,mmm 条边的无向图,从 000 出发,定义 EJEJEJ 为未访问过的结点,并且通过该结点仍然能够遍历全图,假设当前结点的 EJEJEJ 结点有 cntcntcnt 个,在当前结点时可以进行如下选择: (1)在当前结点待 555 分钟 (2)以相同概率在 EJEJEJ 中选一个结点走下去 当走到图中最后一个结点时被...

wineandchord 224

LightOJ-1287 Where to Run

状压dp求期望,难点就在于怎么判断下一个点是不是EJ 题目说只会走EJ点,代表只有能走遍所有点的路径才会有概率走 如果图不连通,就直接不走,输出0 dfs搜索时,只有在搜到所有点都走过的情况返回一个非0值,代表可以走通 如果没有走遍所有点且没有其他EJ点,返回0,代表这个点不是EJ 中途假如接下来有cnt个EJ点,他们的期望加起来是sum,再加上原地待5分钟的情况 总期望为(sum+5...

wl16wzl的博客 285

LightOJ - 1287 Where to Run (期望dp+记忆化)

题面: Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which are connect...

weixin_30375427的博客 270

Where to Run LightOJ - 1287(期望+状压dp+记忆化搜索)

Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which are connected by som...

untilyouydc 373

图上期望——Where to Run LightOJ - 1287 期望+记忆化dfs+状态压缩

博客目录 返回二级目录——kaugnbin概率dp习题集 原题 传送门 Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of s...

GreyBtfly的博客 443

[LightOJ 1287] Where to Run

Where to Run Last night you robbed a bank but couldn't escape and when you just got outside today, the police started chasing you. The city, where you live in, consists of some junctions which a...

a8233821的博客 221
上一篇: LightOJ1274---Beating the Dataset (概率dp)
下一篇: URAL1353---Milliard Vasya's Function(简单数位dp)
tokers
博客等级 码龄12年 71粉丝 700原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值