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 ...
随机推荐
- linux中xargs用法
参数代换: xargs xargs 是在做什么的呢?就以字面上的意义来看, x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思 ...
- Ruby之Rspec的报错解决
#enconding:utf-8 require 'selenium-webdriver' require 'rspec' describe "baidu main page" d ...
- Lua学习七----------Lua函数
© 版权声明:本文为博主原创文章,转载请注明出处 1.Lua函数 - 完成指定的任务,这种情况下函数作为调用语句使用 - 计算并返回值,这种情况下函数作为赋值语句的表达式使用 - Lua函数可以返回多 ...
- myql5.7.7优化配置參数
# Other default tuning values # MySQL Server Instance Configuration File # ------------------------- ...
- 2个YUV视频拼接技术
http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...
- sealed,new,virtual,abstract与override关键字的区别?
1. sealed——“断子绝孙” 密封类不能被继承.密封方法可以重写基类中的方法,但其本身不能在任何派生类中进一步重写.当应用于方法或属性时,sealed修饰符必须始终与override一起使用. ...
- 推荐TED演讲:20岁光阴不再来(Why 30 is not the new 20)
缘起 早上起来在电脑上看到"自强不息"群(群号)中骆宏给大家分享的视频."20岁光阴不再来",利用短暂的时间浏览了一下.就像把这个TED视频分享给很多其它的朋友 ...
- WinForm程序打包工具InnoSetup使用说明图文教程
WinForm程序打包工具InnoSetup使用说明图文教程 WinForm程序开发测试好了,如果将Debug/Release里面的文件发给客户使用,会让客户觉得你不够专业,但是使用VS自带的打包工具 ...
- 局部描述符表LDT的作用+定义+初始化+跳转相关
[0]写在前面 0.1)本代码的作用: 旨在说明局部描述符表的作用,及其相关定义,初始化和跳转等内容: 0.2)文末的个人总结是干货,前面代码仅供参考的,且source code from orang ...
- Python学习总结之四 -- 这就是Python的字典
字典原来是这么回事儿 Python学习到现在,我们已经知道,如果想将值分组到结构中,并且通过编号对其进行引用,列表就可以派上用场.不过,今天,我们将学到一种通过名字引用值的数据结构,应该知道这种数据类 ...