NOI2005 : 聪聪和可可
2016-04-11 14:05
369 查看
传送门:
发现自己还是太弱了,其实这题的状态什么的真的是很显然的。
『我绝对不会告诉你们我的代码是看了hzwer的之后才写的』
/* bzoj : 1415 Noi: 2005 聪聪和可可 Solution : 概率好像还是挺头疼的。 但是大多数算期望都是用dp来算的。 不会算期望只是证明你dp不好,而不是你概率不好,说白了就是自己不会。 设f[x][y]表示x - > y的期望步数,p[x][y]为x - > y的最短路中的标号最小的序号。 显然猫走两步老鼠走一步,DAG。 f[x][y] = {[\sigma dfs(p[p[x][y]][y],V)] / Size[V]} + 1;V = {v | dis[v][y] <= 1;} 翻译成人话就是x - > y的期望步数为:x向y走两步后y随机走一步(或者不走)的期望步数之和 / y的选择数 + 1; 记忆化搜索即可. */ #include <cmath> #include <cstdio> #include <cstring> #define Rep(i,n) for(int i = 1; i <= n ; i ++) #define RepG(i,x) for(int i = head[x] ;~ i ; i = edge[i].next) #define CLR(a,b) memset(a,b,sizeof(a)) #define v edge[i].to using namespace std; const int inf = 1 << 30; typedef long long ll; int read(){ char ch = getchar(); while((ch < '0' || ch > '9') && ch != '-')ch = getchar (); int x = 0; bool flag = 0; if(ch == '-')ch = getchar(),flag = 1; while(ch >= '0' && ch <= '9')x = (x << 1) + (x << 3) + ch - '0',ch = getchar (); return flag ? -x : x; } const int N = 1005; double f ; int dis ,p ; int cnt = 0 ,head ; void Init(){ CLR(head,-1); cnt = 0; } struct Edge{int next,to;}edge[N << 1]; int n,m,S,T; void save(int a,int b){ edge[cnt] = (Edge){head[a],b},head[a] = cnt ++; a ^= b,b ^= a,a ^= b; edge[cnt] = (Edge){head[a],b},head[a] = cnt ++; } double dfs(int x,int y) { if(f[x][y])return f[x][y]; if(x == y)return 0; if(p[x][y] == y || p[p[x][y]][y] == y)return f[x][y] = 1; double tot = dfs(p[p[x][y]][y],y),tp = 0; RepG(i,y){ tot += dfs(p[p[x][y]][y],v); tp ++; } return f[x][y] = tot / (tp + 1) + 1; } int q ; void bfs(int s){ int h = 0,t = 1; q[0] = s,dis[s][s] = 0; while(h != t) { int x = q[h ++],tmp = p[s][x]; if(h == 1001)h = 0; RepG(i,x) if(dis[s][v] == -1 || (dis[s][x] + 1 == dis[s][v] && tmp < p[s][v])) { dis[s][v] = dis[s][x] + 1; p[s][v] = tmp; if(!tmp)p[s][v] = v; q[t ++] = v; if(t == 1001)t = 0; } } } int main (){ Init(); CLR(dis,-1); n = read(),m = read(); S = read(),T = read(); Rep(i,m) { int a,b; a = read(),b = read(); save(a,b); } Rep(i,n)bfs(i); printf("%.3f\n",dfs(S,T)); return 0; }
发现自己还是太弱了,其实这题的状态什么的真的是很显然的。
『我绝对不会告诉你们我的代码是看了hzwer的之后才写的』
相关文章推荐
- 309. Best Time to Buy and Sell Stock with Cooldown
- maertSataDmorfnaideMdniF.295
- sql 用户角色
- netbeans for php常用快捷键
- 案例分析:基于消息的分布式架构
- 架构漫谈(三):如何做好架构之识别问题
- 选择排序法
- Axis2调用WebService
- 【js】:JS获得标签名的方法
- bootstrap 划分12列
- Android hotfix 方案
- Qt之等待提示框(QMovie播放gif图片,等待文字变化等)
- 使用Apache Ant编译打包运行Java项目
- OC 和swift混编网址
- 架构漫谈(二):认识概念是理解架构的基础
- 架构漫谈(一):什么是架构?
- Git 常用命令速查表(图文+表格)【转】
- 暴走的图灵机
- ZOJ 3780 Paint the Grid Again-贪心模拟/拓扑排序
- Win10预览版14316彩色主题下把标题栏改成白色的方法