UVa 10417 - Gift Exchanging

分析和思路可以看我的一篇总结性文章 UVa 10417



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <vector>
#include <deque>
#include <algorithm>
#include <string>
using namespace std;


typedef long double ld;

ld res[5];
int n , book[5];
ld p[15][5];

void dfs(int d , double s , int wh)
{
	if( d==n+1 ) { res[wh]+= s; return ; }
	
	for(int i=0;i<5;i++) if(book[i] && p[d][i])
	{
		book[i]--;
		if(d==1) wh = i;
		dfs(d+1 , s*p[d][i] , wh);
		book[i]++;
	}
}


int main()
{
	freopen("in" , "r" , stdin);
	int t;
	cin>>t;
	
	while(t-- )
	{
		cin>>n;
		
		for(int i=0;i<5;i++) cin>>book[i] , res[i] = 0;
		for(int i=1;i<=n;i++) for(int j=0;j<5;j++) cin>> p[i][j];
		
		dfs(1 , 1 , -1);
		ld Max = 0 ,sum = 0;
		for(int i=0;i<5;i++)if(book[i])  sum+= res[i] , res[i] /= (double)book[i];
		for(int i=0;i<5;i++) Max = max(Max , res[i]);
		for(int i=0;i<5;i++) if(fabs(res[i] - Max)<1e-10) { printf("%d %.3Lf\n",i+1 , Max/sum); break; }
	}
	
	
	
	
	return 0;
}

你可能感兴趣的:(概率,uva)