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;}



0 0
原创粉丝点击