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 ... 
随机推荐
- 通过apache,和nginx模块去除html中的空格和tab
			最近一个项目中,合作方要求去除html中的空格,不想改代码,所以百度了一下通过apache,和nginx模块去除html中的空格和tab的方案,下面记录下来: 一.nginx nginx可以通过mod ... 
- div下拉框(待改善)
			不说话,直接上代码,其中函数dealchose()没有实现,各位就不必纠结了 <%@ page language="java" import="java.util. ... 
- Linux的经常使用命令(2) - 关机
			关机命令 shutdown‑h now 马上进行关机 shutdown‑r now 如今又一次启动计算机 -t sec : -t后面加秒数,即"过几秒后关机" -k : ... 
- linux过滤ip段
			https://www.2cto.com/net/201307/227257.html 
- UNP学习笔记(第二十五章 信号驱动式I/O)
			信号驱动式I/O是指进程预先告知内核,使得当某个描述符发生某事时,内核使用信号通知相关进程. 套接字的信号驱动式I/O 针对一个套接字使用信号驱动式I/O(SIGIO)要求进程执行以下3个步骤: 1. ... 
- H5和CSS3新增内容总结
			CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴影(text-shadow.) 边框: 圆角(border-rad ... 
- 利用DataSet部分功能实现网站登录
			using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ... 
- 转:static关键字的总结
			static关键字的总结 C++的static有两种用法:面向过程程序设计中的static和面向对象程序设计中的static.前者应用于普通变量和函数,不涉及类:后者主要说明static在类中的作用. ... 
- phpQuery—基于jQuery的PHP实现(转)
			Query的选择器之强大是有目共睹的,phpQuery 让php也拥有了这样的能力,它就相当于服务端的jQuery. 先来看看官方简介: phpQuery is a server-side, chai ... 
- 基于JQuery实现表单元素值的回写
			form.jsp: <%@ page language="java" import="java.util.*" pageEncoding="GB ... 
