UPC 1053 Mysterious Treasure (记忆化搜索)
来源:互联网 发布:淘宝金利来男士钱包 编辑:程序博客网 时间:2024/05/01 21:51
这个题开始没想到全边的测试数据,不用记忆化搜索会 50个点800条边会搜索2*10^7次,然后改成记忆化搜索,暴力删边,昨天有点乱,结束了立马想到暴力删边哪里没解决,生气
Description
WNJXYK and DIDIDI is playing a game. DIDIDI draws a directed graph G on the paper which contains n points, m directed edges and no cycle. WNJXYK starts from point 1. For every round, WNJXYK will randomly select one of the directed edges which start from current point with equal possibility, and go to next point by this edge. And the game will continue until such edge doesn’t exist. DIDIDI will place the treasure on point n, if WNJXYK go through this point, he can get the treasure. WNJXYK have one chance to delete one edge(he can also don’t choose to delete), that he can increase the probability of getting a treasure. Your assignment is to calculate the probability of WNJXYK getting the treasure in the optimal condition.
Input
The first line of input contains a positive integer T telling you there are T test cases followed.
For each test case, the first line contains two integers n , m, indicating the number of points ,the number of edges.
Then the following are m lines, each of which contains two integers x and y, indicating there is a edge from x to y.
It is guaranteed that there does not exist multiple edge.
Output
For each test case, print a line “Case #x: y”, where x is the case number (starting from 1) and y is the probability of he getting the treasure. (round to six decimal places).
Sample Input
24 41 21 31 42 34 51 21 31 42 32 4
Sample Output
Case #1: 0.500000
Case #2: 0.750000
#include<bits/stdc++.h>using namespace std;typedef long long ll;vector<int>g[53];vector<bool>g2[53],vis[53];bool vis1[53];double ans,dp[53],maxn;int n,flag=0;double dfs(int u,double k){ if(vis1[u])return dp[u]*k; vis1[u]=1; double tmp=0; if(u==n) { dp[u]=1; return k; } if(g[u].size()==0) { dp[u]=0; return 0; } double t1=0; for(int i=0; i<g[u].size(); i++) { if(vis[u][i]==0)continue; t1=dfs(g[u][i],k/(u==flag?g[u].size()-1:g[u].size())); if(t1>0)g2[u][i]=1; tmp+=t1; } dp[u]=tmp/k; return tmp;}int main(){ freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); int t,cnt=1,m,x,y; scanf("%d",&t); while(t--) { maxn=0; scanf("%d%d",&n,&m); memset(vis1,0,sizeof(vis1)); for(int i=1; i<=n; i++)g[i].clear(),g2[i].clear(),dp[i]=0,vis[i].clear(); for(int i=1; i<=m; i++) { scanf("%d%d",&x,&y); if(x==n)continue; g[x].push_back(y); vis[x].push_back(1); g2[x].push_back(0); } maxn=max(ans,dfs(1,1)); // printf("%.6f\n",maxn); for(int i=1; i<n; i++) { for(int j=0; j<g[i].size(); j++) { if(g2[i][j]==0) { memset(vis1,0,sizeof(vis1)); vis[i][j]=0; flag=i; memset(dp,0,sizeof(dp)); // cout<<i<<" "<<j<<" "<<dfs(1,1)<<endl; maxn=max(maxn,dfs(1,1)); vis[i][j]=1; flag=0; } } } printf("Case #%d: %.6f\n",cnt++,maxn); } return 0;}
- UPC 1053 Mysterious Treasure (记忆化搜索)
- UPC 1053 Mysterious Treasure (记忆化搜索)
- Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索
- codeforces 505C. Mr. Kitayuta, the Treasure Hunter (记忆化搜索)
- Codeforces 505C Mr. Kitayuta, the Treasure Hunter【记忆化搜索+分块数据处理】好题
- 【CodeForces506A】【DP】【记忆化搜索】Mr. Kitayuta, the Treasure Hunter 题解
- upc 3795 食物链(邻接表+记忆dfs)
- hdu_p1078(记忆化搜索)
- WOJ(记忆化搜索)
- 滑雪 (记忆化搜索)
- hihocoder1037(记忆化搜索)
- hdu1078 (记忆化搜索)
- HDU3555Bomb(记忆化搜索)
- uva10626(记忆化搜索)
- *hdu1078(记忆化搜索)
- 滑雪(记忆化搜索)
- poj1088(记忆化搜索)
- hdu1069(*记忆化搜索)
- STM32F2位带操作
- 组建AR、VR和MR的硬件共享实验室,你认为可能吗?
- 有时用JSPsmartupload获取前台数据时,获取不到的原因
- 基于ssm完成mysql数据库数据导出到外部EXCEL表格
- 利用栈实现四则运算
- UPC 1053 Mysterious Treasure (记忆化搜索)
- AS下生成AIDL
- Teamviewer12 设置局域网远程
- Hadoop应用架构-OReilly 2015-读书笔记
- JBoss7/WildFly配置PostgreSQL数据源
- 三极管用于开关的电路设计
- Spring 加载数据库连接驱动的时候出现找不到驱动类解决办法。 com.mchange.v2.c3p0.DriverManagerDataSource ensureDriverLoaded
- 程序员必须掌握的8种排序算法(五):冒泡排序
- 绿卡在等你亚马逊北美招聘中国工程师