HDU-2262 Where is the canteen 概率DP,高斯消元
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2262
题意:LL在一个迷宫里面转,每次走向周围能走的点的概率都是一样的,现在LL要随机的走到canteen哪里,求期望。
这个是带环的求期望问题,并且没有什么特殊性,只有列出方程,然后gauss消元了。首先用BFS求出能走的点,并判断能否走到canteen。然后列出期望方程,E[i]=Σ( E[j]*p[j] ) +1。然后好求了,注意题目中有多个canteen。。。
//STATUS:C++_AC_437MS_700KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e30;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End /* gauss_elimination O(n^3)
n个方程n个变元
要求系数矩阵可逆
A[][]是增广矩阵,即A[i][n]是第i个方程右边的常数bi
运行结束后A[i][n]是第i个未知数的值 */
int vis[][],cnt[][],e[][];
char g[][];
int n,m,tot,sx,sy; double A[N][N]; int gauss(int n)
{
int i,j,k,r;
for(i=;i<n;i++){
//选一行与r与第i行交换,提高数据值的稳定性
r=i;
for(j=i+;j<n;j++)
if(fabs(A[j][i]) > fabs(A[r][i]))r=j;
if(r!=i)for(j=;j<=n;j++)swap(A[r][j],A[i][j]);
//i行与i+1~n行消元
/* for(k=i+1;k<n;k++){ //从小到大消元,中间变量f会有损失
double f=A[k][i]/A[i][i];
for(j=i;j<=n;j++)A[k][j]-=f*A[i][j];
}*/
for(j=n;j>=i;j--){ //从大到小消元,精度更高
for(k=i+;k<n;k++)
A[k][j]-=A[k][i]/A[i][i]*A[i][j];
}
}
//判断方程时候有解
for(i=;i<n;i++)if(sign(A[i][i])==)return ;
//回代过程
for(i=n-;i>=;i--){
for(j=i+;j<n;j++)
A[i][n]-=A[j][n]*A[i][j];
A[i][n]/=A[i][i];
}
return ;
} int bfs()
{
int i,j,x,y,nx,ny,t;
queue<int> q;
q.push(sx*m+sy);
mem(vis,-);mem(cnt,);
vis[sx][sy]=tot=;
tot++;
while(!q.empty()){
t=q.front();q.pop();
x=t/m;y=t%m;
for(i=;i<;i++){
nx=x+dx[i];
ny=y+dy[i];
if(nx>=&&nx<n && ny>=&&ny<m && g[nx][ny]!='#'){
cnt[x][y]++;
if(vis[nx][ny]!=-)continue;
vis[nx][ny]=tot++;
q.push(nx*m+ny);
}
}
}
for(i=;i<n;i++){
for(j=;j<m;j++)
if(vis[i][j]!=- && e[i][j])return ;
}
return ;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,k;
while(~scanf("%d%d",&n,&m))
{
mem(e,);
for(i=;i<n;i++){
scanf("%s",g[i]);
for(j=;j<m;j++){
if(g[i][j]=='@')sx=i,sy=j;
else if(g[i][j]=='$')e[i][j]=;
}
} if(!bfs()){
printf("-1\n");
continue;
}
mem(A,);
for(i=;i<n;i++){
for(j=;j<m;j++){
if(vis[i][j]==-)continue;
int u=vis[i][j];
double p=1.0/cnt[i][j];
if(e[i][j]){
A[u][u]=;
A[u][tot]=;
continue;
}
A[u][u]=A[u][tot]=;
for(k=;k<;k++){
int x=i+dx[k],y=j+dy[k];
if(x>=&&x<n && y>=&&y<m && vis[x][y]!=-){
A[u][vis[x][y]]=-p;
}
}
}
}
gauss(tot);
printf("%.6lf\n",A[vis[sx][sy]][tot]);
}
return ;
}
HDU-2262 Where is the canteen 概率DP,高斯消元的更多相关文章
- HDU 2262 Where is the canteen 期望dp+高斯消元
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2262 Where is the canteen Time Limit: 10000/5000 MS ...
- BZOJ_1778_[Usaco2010 Hol]Dotp 驱逐猪猡_概率DP+高斯消元
BZOJ_1778_[Usaco2010 Hol]Dotp 驱逐猪猡_概率DP+高斯消元 题意: 奶牛们建立了一个随机化的臭气炸弹来驱逐猪猡.猪猡的文明包含1到N (2 <= N <= 3 ...
- LightOJ - 1151概率dp+高斯消元
概率dp+高斯消元 https://vjudge.net/problem/LightOJ-1151 题意:刚开始在1,要走到100,每次走的距离1-6,超过100重来,有一些点可能有传送点,可以传送到 ...
- 【bzoj1778】[Usaco2010 Hol]Dotp 驱逐猪猡 矩阵乘法+概率dp+高斯消元
题目描述 奶牛们建立了一个随机化的臭气炸弹来驱逐猪猡.猪猡的文明包含1到N (2 <= N <= 300)一共N个猪城.这些城市由M (1 <= M <= 44,850)条由两 ...
- BZOJ3270 博物館 概率DP 高斯消元
BZOJ3270 博物館 概率DP 高斯消元 @(XSY)[概率DP, 高斯消元] Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博 ...
- BZOJ 3270: 博物馆 [概率DP 高斯消元]
http://www.lydsy.com/JudgeOnline/problem.php?id=3270 题意:一张无向图,一开始两人分别在$x$和$y$,每一分钟在点$i$不走的概率为$p[i]$, ...
- 【BZOJ3640】JC的小苹果 概率DP+高斯消元
[BZOJ3640]JC的小苹果 Description 让我们继续JC和DZY的故事. “你是我的小丫小苹果,怎么爱你都不嫌多!” “点亮我生命的火,火火火火火!” 话说JC历经艰辛来到了城市B,但 ...
- 2016ACM/ICPC亚洲区沈阳站H - Guessing the Dice Roll HDU - 5955 ac自动机+概率dp+高斯消元
http://acm.hdu.edu.cn/showproblem.php?pid=5955 题意:给你长度为l的n组数,每个数1-6,每次扔色子,问你每个串第一次被匹配的概率是多少 题解:先建成ac ...
- 【概率DP/高斯消元】BZOJ 2337:[HNOI2011]XOR和路径
2337: [HNOI2011]XOR和路径 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 682 Solved: 384[Submit][Stat ...
随机推荐
- (转)eclipse快捷键
Eclipse常用快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编 ...
- Android GridView、ListView、ScrollView上下拉刷新
实现方法是将显示的内容最外层的ViewGroup做成一个LinearLayout,并扩展它,使其可以上下拖动. 重点是实现View的onTouch方法. 下载:http://files.cnblogs ...
- 多线程 (五)NSOperation
NSOperation是对GCD的分装,OC语言,更简单方便 NSOperation和NSOperationQueue一起使用也能实现多线程编程 基本步骤: 将操作封装到一个NSOperation对象 ...
- 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求
- winform学习日志(十九)----------真正三层架构之登录
摘要:一:三层构架的基础知识在项目开发的过程中,有时把整个项目分为三层架构,其中包括:表示层(UI).业务逻辑层(BLL)和数据访问层(DAL).三层的作用分别如下: 表示层:为用户提供交互操作界面, ...
- Android:常见错误提示
记录开发中常出现的错误 1.遇到这样的错误时,应该立马想到是书写错误或语法错误,常见为android:name写成了name Attribute is missing the Android name ...
- ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map
原文地址: ArcGIS Runtime for Android开发教程V2.0(3)基础篇---Hello World Map - ArcGIS_Mobile的专栏 - 博客频道 - CSDN.NE ...
- JBoss7 局域网无法访问 解决方法
JBoss7 局域网无法访问 解决方法 在standalone模式,修改/standalone/configuration/standalone.xml.如下 修改或新增一个interface. &l ...
- [HIS] HIT行业常用名词及缩写定义
[HIS] HIT行业常用名词及缩写定义 1. EHR 居民个人电子健康记录 2. MPI 居民个人主索引 3. HIS 医院管理信息系统 4. CIS 医院临床信息系统 5. P ...
- javascript 一些需要知道的东西
这里我直接贴出代码,注释已经补全,欢迎指正: <script type="text/javascript"> /** 1,js中一切皆是对象,函数也是, 2,当定义变量 ...