uva11600 - Masud Rana 状态压缩 期望
来源:互联网 发布:培训机构垃圾 知乎 编辑:程序博客网 时间:2024/05/21 10:31
Masud Rana, A Daring Spy Of BangladeshCounter Intelligence. He is in a new mission. There is a total n cities in Bangladesh.Each city is connected to all other by bidirectional roads. So there are totaln * (n-1) / 2 bidirectional roads. Many of the roads are under control of evilpowers. City a is safely reachable from city b, if there is a path from a to bcontaining only roads which are not under control of evil powers. There are mroads which are safe from evil powers. The mission of Masud Rana is to destroythe evil powers of some roads, and make sure that every city is safelyreachable from all other.
Masud Rana chose a new strategyfor this special mission. Every morning he selects a random city other than thecity he stays in at that moment, and visit that city by direct connecting road,in the time of his visit by the road he destroys all evil power of that road ifexists any, and makes that road safe. After reaching new city, he stays there till next morning.In the next morning he checks whether all cities are safely reachable from allothers. If he is already done his mission ends, otherwise he repeats samestrategy.
Let us number the cities by 1, 2, ... , n. Masud Rana is incity 1 when he starts his mission.
What is the expected number of days to finish the missionfor Masud Rana.
Input
Input will starts with an integerT(T ≤ 100) which denotes the number of test case. Each case starts withtwo integer N(N ≤ 1 ≤ 30) and M(0 ≤ M ≤ N*(N-1)/2).Each of the next lines contains two integers a and b (1 ≤ a, b ≤ N)which means road connecting city a and b is safe.
Output
You have to output the expectednumber of days required for Masud Rana. Print the case number followed by theoutput. Look at the sample in/out forexact format. Upto 1E-6 error in your output will be acceptable.
2
3 1
2 3
4 1
2 3
Case 1: 1.0
Case 2: 3.5
有N个城市,第一天在城市1,每天随机走到另一个城市,如果这两个城市之间的路上有妖怪就消灭妖怪,在平均情况下,需要多少天能消灭所有妖怪。
首先缩点,互相之间没有妖怪的城市都看成一个点i,cnt[i]表示这个点有多少个城市。DP(u,st)表示目前在u点的一个城市,已经经过了st状态的点,还平均需要多少天能消灭所有妖怪。设n为当前已走过st状态的点的个数,那么有(N-n)/(N-1)的概率走到当前没有经过的城市去,这是个几何分布,期望是1/p,所以平均要走(N-1)/(N-n)次才能走到没有经过的城市。在没访问过的点中,走到点i的概率为cnt[i]/(N-n),若走到i点,还平均需要DP(i,st|(1<<i))。所以DP(u,st)=(N-1)/(N-n)+{sum(cnt[i]/(N-n)*DP(i,st|(1<<i))),st&(1<<i)==0}。
#include<cstring>#include<cstdio>#include<iostream>#include<climits>#include<cmath>#include<algorithm>#include<queue>#include<map>#define INF 0x3f3f3f3f#define MAXN 32#define MAXM 500using namespace std;int T,N,M,P;int vis[MAXN],cnt[MAXN],w[MAXN][MAXN];map<int,double> d[MAXM];void DFS(int x){ vis[x]=1; cnt[P]++; for(int i=1;i<=N;i++) if(w[x][i]&&!vis[i]) DFS(i);}int num(int x){ int ret=0; for(int i=0;i<P;i++) if(x&(1<<i)) ret+=cnt[i]; return ret;}double DP(int u,int st){ if(d[u].count(st)) return d[u][st]; double &ans=d[u][st]; int n=num(st); if(n==N) return ans=0; ans=1.*(N-1)/(N-n); for(int i=0;i<P;i++) if(!(st&(1<<i))) ans+=DP(i,st|(1<<i))*cnt[i]/(N-n); return ans;}int main(){ freopen("in.txt","r",stdin); int cas=0; scanf("%d",&T); while(T--){ scanf("%d%d",&N,&M); memset(w,0,sizeof(w)); memset(vis,0,sizeof(vis)); memset(cnt,0,sizeof(cnt)); while(M--){ int u,v; scanf("%d%d",&u,&v); w[u][v]=w[v][u]=1; } P=0; for(int i=1;i<=N;i++) if(!vis[i]){ DFS(i); P++; } for(int i=0;i<N;i++) d[i].clear(); printf("Case %d: %.6lf\n",++cas,DP(0,1)); } return 0;}
- uva11600 - Masud Rana 状态压缩 期望
- UVA 11600 - Masud Rana(状态压缩DP+记忆化搜索)
- UVA 10600 Masud Rana
- UVA 11600 Masud Rana
- UVA-11600-Masud Rana
- uva 11600 - Masud Rana(记忆化搜索)
- Masud Rana - UVa 11600 dp 并查集
- UVA 11600 Masud Rana 并查集+状压概率dp
- BZOJ1076 状态压缩DP 期望DP
- 搜集卡片 状态压缩+数学期望
- hdu 4336 dp求期望(状态压缩)
- HDU-5045 Contest 状态压缩DP求期望
- hdu 4336 全期望公式+状态压缩+概率dp
- HDOJ4336Card Collector【概率dp求期望+状态压缩】
- BZOJ_P1076 [SCOI2008]奖励关(概率期望DP+状态压缩DP)
- hdu-4336 Card Collector状态压缩|期望dp】
- bzoj 1076: [SCOI2008]奖励关 期望dp+状态压缩
- 9.2机房欢乐赛 礼物 (状态压缩+期望dp)
- IOS中的Block在C++中的运用
- Call-time pass-by-reference has been removed
- Byte的取值范围0到255
- HDU 2602 Bone Collector (01背包)
- C#实现TreeView向XML的绝对转换类 .
- uva11600 - Masud Rana 状态压缩 期望
- hdu 1009 FatMouse' Trade (贪心)
- poj 3259 Wormholes 最短路
- 在windows,mac,linux下 git ssh key的生成
- Safe C++ 读书笔记:对基本数据类型进行封装,以编写更安全的C++代码
- openssl与cryptoAPI交互AES加密解密
- 【转】NSJSONSerialization解析JSON数据
- IIS网站配置
- 使用List、Set等求交集和并集