bzoj 1415
莫名互测题...
这题一看就是期望dp,可是不会转移,所以考试写50分暴力走人...
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
struct Edge
{
int next;
int to;
}edge[2005];
int head[1005];
int dis[1005][1005];
bool used[1005];
int cnt=1;
int n,m;
int p0,p1;
void init()
{
memset(head,-1,sizeof(head));
cnt=1;
}
void add(int l,int r)
{
edge[cnt].next=head[l];
edge[cnt].to=r;
head[l]=cnt++;
}
void spfa(int rt)
{
queue <int> M;
memset(dis[rt],0x3f,sizeof(dis[rt]));
memset(used,0,sizeof(used));
dis[rt][rt]=0;
used[rt]=1;
M.push(rt);
while(!M.empty())
{
int u=M.front();
M.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(dis[rt][to]>dis[rt][u]+1)
{
dis[rt][to]=dis[rt][u]+1;
if(!used[to])
{
used[to]=1;
M.push(to);
}
}
}
}
}
double ans=0;
void dfs(double p,int v,int pr,int pz)
{
if(pr==pz)
{
ans+=(double)v*p;
return;
}
int maxp=0;
int temp=0x3f3f3f3f;
for(int i=head[pr];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(dis[to][pz]<temp)
{
temp=dis[to][pz];
maxp=to;
}else if(dis[to][pz]==temp)
{
maxp=min(maxp,to);
}
}
pr=maxp;
if(pr==pz)
{
ans+=(double)v*p;
return;
}
maxp=0;
temp=0x3f3f3f3f;
for(int i=head[pr];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(dis[to][pz]<temp)
{
temp=dis[to][pz];
maxp=to;
}else if(dis[to][pz]==temp)
{
maxp=min(maxp,to);
}
}
pr=maxp;
if(pr==pz)
{
ans+=(double)v*p;
return;
}
int cot=0;
for(int i=head[pz];i!=-1;i=edge[i].next)
{
cot++;
}
cot++;
for(int i=head[pz];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(to==pr)
{
ans+=(double)p*(double)1.0/(double)cot*(double)v;
continue;
}
dfs(p*(double)1.0/(double)cot,v+1,pr,to);
}
dfs(p*(double)1.0/(double)cot,v+1,pr,pz);
}
int main()
{
// freopen("eat.in","r",stdin);
// freopen("eat.out","w",stdout);
scanf("%d%d",&n,&m);
scanf("%d%d",&p0,&p1);
if(p0==p1)
{
printf("0\n");
return 0;
}
init();
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add(x,y);
add(y,x);
}
for(int i=1;i<=n;i++)
{
spfa(i);
}
dfs(1,1,p0,p1);
printf("%.3lf\n",ans);
return 0;
}
正解:期望dp+记忆华搜索
设状态f[i][j]代表聪聪在i点,可可在j点时聪聪追上可可的期望
然后用dfs更新即可,注意先预处理出最短路和tpos[i][j]表示聪聪在i点,可可在j点时聪聪走一步时会走到的位置
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
struct Edge
{
int next;
int to;
}edge[2005];
int head[1005];
int cnt=1;
int dis[1005][1005];
int tpos[1005][1005];
double f[1005][1005];
bool used[1005];
int inr[1005];
int n,m,p0,p1;
void add(int l,int r)
{
edge[cnt].next=head[l];
edge[cnt].to=r;
head[l]=cnt++;
}
void bfs(int rt)
{
queue <int> M;
memset(used,0,sizeof(used));
memset(dis[rt],0x3f,sizeof(dis[rt]));
M.push(rt);
dis[rt][rt]=0;
used[rt]=1;
while(!M.empty())
{
int u=M.front();
M.pop();
for(int i=head[u];i;i=edge[i].next)
{
int to=edge[i].to;
if(used[to])
{
continue;
}
used[to]=1;
dis[rt][to]=dis[rt][u]+1;
M.push(to);
}
}
}
double dfs(int pr,int pz)
{
if(f[pr][pz]!=-1.0)
{
return f[pr][pz];
}
if(pr==pz)
{
return f[pr][pz]=0.0;
}
if(tpos[pr][pz]==pz)
{
return f[pr][pz]=1.0;
}
if(tpos[tpos[pr][pz]][pz]==pz)
{
return f[pr][pz]=1.0;
}
f[pr][pz]=0.0;
for(int i=head[pz];i;i=edge[i].next)
{
int to=edge[i].to;
f[pr][pz]+=dfs(tpos[tpos[pr][pz]][pz],to);
}
f[pr][pz]=(f[pr][pz]+dfs(tpos[tpos[pr][pz]][pz],pz))/(double)inr[pz]+1;
return f[pr][pz];
}
inline int read()
{
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
// freopen("eat.in","r",stdin);
// freopen("eat.out","w",stdout);
n=read(),m=read();
p0=read(),p1=read();
for(int i=1;i<=m;i++)
{
int x=read(),y=read();
add(x,y);
add(y,x);
inr[x]++;
inr[y]++;
}
for(int i=1;i<=n;i++)
{
inr[i]++;
bfs(i);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
f[i][j]=-1.0;
int temp=0x3f3f3f3f;
for(int k=head[i];k;k=edge[k].next)
{
int to=edge[k].to;
if(dis[to][j]<temp)
{
tpos[i][j]=to;
temp=dis[to][j];
}else if(dis[to][j]==temp)
{
tpos[i][j]=min(tpos[i][j],to);
}
}
}
}
printf("%.3lf\n",dfs(p0,p1));
return 0;
}
bzoj 1415的更多相关文章
- BZOJ 1415 聪聪和可可(概率DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1415 题意:一个无向图,一个猫.一只老鼠.在任意时刻猫知道老鼠在哪个顶点上.每次移动猫先 ...
- BZOJ 1415: [Noi2005]聪聪和可可( 最短路 + 期望dp )
用最短路暴力搞出s(i, j)表示聪聪在i, 可可在j处时聪聪会走的路线. 然后就可以dp了, dp(i, j) = [ dp(s(s(i,j), j), j) + Σdp(s(s(i,j), j), ...
- 【BZOJ 1415】 1415: [Noi2005]聪聪和可可 (bfs+记忆化搜索+期望)
1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1640 Solved: 962 Description I ...
- bzoj 1415(概率dp和bfs预处理)
感觉挺经典的一道题目. 先用 bfs 预处理下一步走到的位置.因为每一步走法都是固定的,所以可以用dp的方法来做. 1415: [Noi2005]聪聪和可可 Time Limit: 10 Sec M ...
- bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1415 乍一看和“游走”一样.于是高斯消元.n^2状态,复杂度n^6…… 看看TJ,发现因为聪 ...
- BZOJ 1415 【NOI2005】 聪聪和可可
题目链接:聪聪和可可 一道水题--开始还看错题了,以为边带权--强行\(O(n^3)\)预处理-- 首先,我们显然可以预处理出一个数组\(p[u][v]\)表示可可在点\(u\),聪聪在点\(v\)的 ...
- bzoj 1415 期望+记忆化搜索 ****
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn
- BZOJ 1415 聪聪和可可
f[i][j]表示i点追j点的期望步数... 这题必须spfa不能bfs. 且复杂度不会炸(仅1000条边) #include<iostream> #include<cstdio&g ...
- bzoj 1415: [Noi2005]聪聪和可可
直接上记忆化搜索 #include<queue> #include<cstdio> #include<algorithm> using namespace std; ...
- BZOJ 1415: [Noi2005]聪聪和可可 [DP 概率]
传送门 题意:小兔子乖乖~~~ 题意·真:无向图吗,聪抓可,每个时间聪先走可后走,聪一次可以走两步,朝着里可最近且点编号最小的方向:可一次只一步,等概率走向相邻的点或不走 求聪抓住可的期望时间 和游走 ...
随机推荐
- 使用Excel过滤重复数据
如题 由于数据太多,没有办法人工过滤,所以借助Excel 我的数据是这样的 需要找出里面重复的数据 首先,选中需要筛选的数据,点击开始 --> 条件格式 --> 突出显示单元格规则 --& ...
- GB GBRT XgBoost
https://blog.csdn.net/github_38414650/article/details/76061893 https://www.cnblogs.com/wxquare/p/554 ...
- Spring重温(一)--Spring快速入门
1.spring官网(https://repo.spring.io)下载依赖jar. 2.配置spring环境时还需要commons-logging相关jar. 3.打开eclise创建一个工程,并将 ...
- pip问题
“ModuleNotFoundError: No module named 'pip'”的解决方法:http://www.pianshen.com/article/476461202/ pip错误 I ...
- 【NLP CS224N笔记】Lecture 3 GloVe: Global Vectors for Word Representation
I. 复习word2vec的核心思路 1. Skip-gram 模型示意图: 2.word vectors的随机梯度 假设语料库中有这样一行句子: I love deep learning and N ...
- Invalid character found in the request target.
背景:springboot项目内置tomcat9.0 调用的接口中有{}就会报错 解决办法: 新的tomcat新版本增加了一个新特性,就是严格按照 RFC 3986规范进行访问解析,而 RFC 398 ...
- Windows中Anaconda,Tensorflow 和 Pycharm的安装和配置
Anaconda完全入门指南 https://www.jianshu.com/p/eaee1fadc1e9 [安装不要按此条链接进行] Windows中 Anacond ...
- 统计分析与R软件-chapter2-3
2.3 对象和它的模式与属性 R是一种基于对象的语言,R的对象包含了若干个元素作为其数据,另外还可以有一些特殊数据称为属性,并规定了一些特定操作(如打印.绘图).比如,一个向量是一个对象,一个图形也是 ...
- BIM开发引挈
BIM开发引挈: 0.three.js https://threejs.org/ 1. 陕西葛兰岱尔网络科技有限公司 www.glendale.com.cn 基于WebGL BIM轻 ...
- Webpack2 中的 NamedModulesPlugin 与 HashedModuleIdsPlugin
要讨论Webpack 2中新增的这两个plugin的功能,还要先从使用Webpack打包的项目的前端资源缓存方案说起. 通常在使用了Webpack的项目中我们会使用CommonsChunkPlugin ...