hdu 5013 City Tour


#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\len"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\len"
#define in64 "%lld"
#endif
/************ for topcoder by zz1215 *******************/
#define foreach(c,itr)  for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)
#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a,b)      for( int i = (a) ; i >= (b) ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define pb              push_back
#define pf              push_front
#define X               first
#define Y               second
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define MC(a,b)			memcpy(a,b,sizeof(b))
#define MAX(a,b)        ((a)>(b)?(a):(b))
#define MIN(a,b)        ((a)<(b)?(a):(b))
#define read            freopen("out.txt","r",stdin)
#define write           freopen("out2.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-11;
const double pi = acos(-1.0);
const int maxn = 16;

int n, m;
double dp[1 << maxn][17];
double ch[1 << maxn][17];
double ey[1 << maxn][17];
double go[1 << maxn][17];
double IIp[1 << maxn];
double p[17];
double px[17];
double pto[17][17];
double pn[17][17];
double h[20][20];
int have[1 << maxn];
int lg2[1 << maxn];

int sgn(double x)
{
	if (x>1e-20) return 1;
	if (x<-1e-20) return -1;
	return 0;
}

double start(){
	for (int i = 0; i < n; i++){
		if (sgn(p[i] - 1) == 0) p[i] -= 1e-10;
		px[i] = p[i] / (1.0 - p[i]);
	}
	for (int now = 0; now < (1 << n); now++){
		IIp[now] = 1.0;
		for (int i = 0; i < n; i++){
			if (now&(1 << i)){
				IIp[now] *= 1.0 - p[i];
			}
		}
	}
	for (int now = 0; now < (1 << n); now++){
		dp[now][0] = 1;
	}
	for (int step = 1; step <= n; step++){
		for (int now = 0; now < (1 << n); now++){
			if (!now){
				dp[now][step] = 0;
			}
			else{
				dp[now][step] = dp[now - (now&(-now))][step - 1] * px[lg2[now&(-now)]] + dp[now - (now&(-now))][step];
			}
		}
	}
	for (int now = 0; now < (1 << n); now++){
		for (int i = 0; i < n; i++){
			ch[now][i] = 0;
			if (now & (1 << i)){
				int rest = now - (1 << i);
				for (int cnt = 0; cnt < n; cnt++){
					ch[now][i] += px[i] * dp[rest][cnt] * (cnt + 1.0 + have[now]);
				}
				ch[now][i] /= have[now];
				ch[now][i] *= IIp[now];
			}
		}
	}
	for (int now = 0; now < (1 << n); now++){
		for (int pos = 1; pos < m; pos++){
			ey[now][pos] = 0;
			for (int i = 0; i < n; i++){
				if (now&(1 << i)){
					ey[now][pos] += h[i][pos] * ch[now][i];
				}
			}
		}
	}
	for (int i = 0; i < n; i++){
		pto[i][0] = 1.0;
		for (int j = 1; j < m; j++){
			pto[i][j] = 1.0;
			for (int k = 1; k <= j; k++){
				pto[i][j] *= p[i];
			}
			pn[i][j] = pto[i][j] / (1.0 - pto[i][j]);
		}
	}
	for (int now = 0; now < (1 << n); now++){
		go[now][0] = 0;
	}
	go[(1<<n)-1][0] = 1.0;
	for (int pos = 1; pos < m; pos++){
		go[0][pos] = 1.0;
		for (int i = 0; i < n; i++){
			go[0][pos] *= 1.0 - pto[i][pos];
		}
		for (int now = 1; now < (1 << n); now++){
			go[now][pos] = go[now - (now&(-now))][pos] * pn[lg2[now&(-now)]][pos];
		}
	}
	double ans = 0.0;
	for (int i = 0; i < n; i++){
		ans += h[i][0];
	}
	for (int pos = 1; pos < m; pos++){
		for (int now = 0; now < (1 << n); now++){
			ans += go[now][pos - 1] * ey[now][pos];
		}
	}
	return ans;
}

int main(){
	for (int i = 0; i < maxn; i++){
		lg2[1 << i] = i;
	}
	for (int now = 0; now < (1 << maxn); now++){
		have[now] = 0;
		for (int i = 0; i < maxn; i++){
			if (now&(1 << i)) {
				have[now]++;
			}
		}
	}
	while (cin >> n>> m){
		for (int i = 0; i < n; i++){
			cin >> p[i];
		}
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				cin >> h[i][j];
			}
		}
		printf("%.10lf\n", start());
	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值