LightOJ - 1287 Where to Run —— 期望、状压DP
题目链接:https://vjudge.net/problem/LightOJ-1287
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, 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.
题意:
有n个街口和m条街道, 你后边跟着警察,需要进行逃亡,在每个街口你都有≥1个选择:
1)停留在原地5分钟。
2)如果这个街口可以到xi这个街口, 并且, 通过xi可以遍历完所有未走过的街口,那么就加入选择。
每个选择都是等概率的,求警察抓住你所用时间的期望, 即你无路可走时的时间期望。
题解:
1. 对于每个点, 先找出所有的选择, 假设有k个选择。
2. 那么E[u] 表示在街口u出发后走完剩余点的时间期望。
则:E[u] = 1 / k * sigma (E[v] + time[u][v]) + 1 / k * (E[u] + 5)。
化简得:E[u] = (sigma (E[v] + time[u][v]) + 5) / (k - 1)
3. 用[S][u]表示从u点出发, 已完成状态为S,之后走完剩余点所用时间的期望。
代码如下:
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include <queue>
- #include <stack>
- #include <map>
- #include <string>
- #include <set>
- using namespace std;
- typedef long long LL;
- const int INF = 2e9;
- const LL LNF = 9e18;
- const int MOD = 1e9+;
- const int MAXN = +;
- int maze[MAXN][MAXN];
- bool vis[<<MAXN][MAXN], canReach[<<MAXN][MAXN];
- double dp[<<MAXN][MAXN];
- bool dfs(int S, int u, int n)
- {
- if(S==(<<n)-) return dp[S][u] = , true;
- if(vis[S][u]) return canReach[S][u];
- vis[S][u] = ;
- int cnt = ;
- double sum = ;
- for(int v = ; v<n; v++)
- {
- if(!maze[u][v] || (S&(<<v))) continue;
- if(dfs(S|(<<v),v,n))
- {
- canReach[S][u] = true;
- sum += dp[S|(<<v)][v]+maze[u][v];
- cnt++;
- }
- }
- if(!canReach[S][u]) return dp[S][u] = , false;
- return dp[S][u] = (5.0+sum)/cnt, true;
- }
- int main()
- {
- int T, kase = ;
- scanf("%d", &T);
- while(T--)
- {
- int n, m;
- scanf("%d%d",&n,&m);
- memset(maze, , sizeof(maze));
- for(int i = ; i<=m; i++)
- {
- int u, v, w;
- scanf("%d%d%d", &u,&v,&w);
- maze[u][v] = maze[v][u] = w;
- }
- memset(vis, false, sizeof(vis));
- memset(canReach, false, sizeof(canReach));
- dfs(,,n);
- printf("Case %d: %.10lf\n", ++kase, dp[][]);
- }
- }
LightOJ - 1287 Where to Run —— 期望、状压DP的更多相关文章
- 2018.09.23 bzoj1076: [SCOI2008]奖励关(期望+状压dp)
传送门 一道神奇的期望状压dp. 用f[i][j]f[i][j]f[i][j]表示目前在第i轮已选取物品状态为j,从现在到第k轮能得到的最大贡献. 如果我们从前向后推有可能会遇到不合法的情况. 所以我 ...
- 【xsy1596】旅行 期望+状压DP
题目大意:有$m$个人要从城市$1$开始,依次游览城市$1$到$n$. 每一天,每一个游客有$p_i$的概率去下一个城市,和$1-p_i$的概率结束游览. 当游客到达城市$j$,他会得到$(1+\fr ...
- lightoj 1119 - Pimp My Ride(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1119 题解:状压dp存一下车有没有被搞过的状态就行. #include < ...
- lightoj 1061 - N Queen Again(状压dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1061 题解:显然能满足情况的8皇后的摆法不多,于是便可以用题目给出的状态来匹配 ...
- “景驰科技杯”2018年华南理工大学程序设计竞赛 A. 欧洲爆破(思维+期望+状压DP)
题目链接:https://www.nowcoder.com/acm/contest/94/A 题意:在一个二维平面上有 n 个炸弹,每个炸弹有一个坐标和爆炸半径,引爆它之后在其半径范围内的炸弹也会爆炸 ...
- LightOJ 1287 Where to Run(期望)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1287 题意:给定一个n个点的无向图(0到n-1),你开始在0.你开始遍历这个图 ...
- BZOJ3925: [Zjoi2015]地震后的幻想乡【概率期望+状压DP】
Description 傲娇少女幽香是一个很萌很萌的妹子,而且她非常非常地有爱心,很喜欢为幻想乡的人们做一些自己力所能及的事情来帮助他们. 这不,幻想乡突然发生了地震,所有的道路都崩塌了.现在的首要任 ...
- BZOJ5006 THUWC2017随机二分图(概率期望+状压dp)
下称0类为单边,1类为互生边,2类为互斥边.对于一种匹配方案,考虑其出现的概率*2n后对答案的贡献,初始为1,如果有互斥边显然变为0,否则每有一对互生边其贡献*2.于是有一个显然的dp,即设f[S1] ...
- 状压DP小拼盘
有的DP题,某一部分的状态只有两种,选或不选. 开数组记录,代价太大,转移不方便. 状态压缩意为,用 “0/1“ 表示 “选/不选“ . 把状态表示为二进制整数. There are 10 kinds ...
随机推荐
- x86 Android游戏开发专题篇之使用google breakpad捕捉c++崩溃(以cocos2dx为例)
近期一直都在x86设备上进行游戏开发.就c++层和Android java层倒没有什么要特别注意的(除了须要注意一下改动Application.mk指定平台外),在c++崩溃的时候,非常多时候看不到堆 ...
- 处理中文空格.replace((char)12288,' ')
trim()只能替换英文空格.replace((char)12288,' ')是替换中文
- rename命令
rename命令用字符串替换的方式批量改变文件名. 语法 rename(参数) 参数 原字符串:将文件名需要替换的字符串: 目标字符串:将文件名中含有的原字符替换成目标字符串: 文件:指定要改变文件名 ...
- HDU 5042 GCD pair 预处理+二分 分段
点击打开链接 #include <stdio.h> #include <string.h> #include <iostream> #include <cma ...
- 在Ubuntu 14.04 64bit中永久添加DNS的方法
DNS信息是由/etc/resolv.conf提供的,它是每次开机时,由/sbin/resolvconf生成的/etc/resolv.conf是/run/resolvconf/resolv.conf的 ...
- 一篇文章说完Java的垃圾回收过程
想要了解java的垃圾回收过程首先要理解java堆的内存模型,简单表示如下: 从上面的图片可以看出,java的堆内存可以简单的分为,新生代和老年代,在新生代中有分为三个区域,1个Eden区和2个S ...
- android:scrollbar的一些属性
1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- 面试宝典之预处理、const与sizeof
#include <stdio.h> #define SUB(x, y) x - y #define ACCESS_BEFORE(element, offset, value) *SUB( ...
- Linux trace使用入门
概念 trace 顾名思义追踪信息,可通俗理解为一种高级打印机制,用于debug,实现追踪kernel中函数事件的框架.源代码位于:\kernel\trace\trace.c,有兴趣能够研究 撰写不易 ...
- 浏览器前缀-----[译]Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的. 所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可 ...