【DP】HDOJ 5045 Contest

简单DP。。。。

#include <iostream>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <algorithm>  
#include <cstring>  
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 1205
#define maxm 40005
//#define eps 1e-10
#define mod 10000007
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid  
#define rson o<<1 | 1, mid+1, R  
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
LL qpow(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base;base=base*base;b/=2;}return res;}
LL powmod(LL a, LL b){LL res=1,base=a;while(b){if(b%2)res=res*base%mod;base=base*base%mod;b/=2;}return res;}
void scanf(int &__x){__x=0;char __ch=getchar();while(__ch==' '||__ch=='\n')__ch=getchar();while(__ch>='0'&&__ch<='9')__x=__x*10+__ch-'0',__ch = getchar();}
LL gcd(LL _a, LL _b){if(!_b) return _a;else return gcd(_b, _a%_b);}
//head

double g[15][maxn];
double dp[maxn][maxn];
int n, m;

void read(void)
{
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++)
		for(int j = 0; j < m; j++)
			scanf("%lf", &g[i][j]);
}

void init(void)
{
	for(int i = 0; i <= m; i++)
		for(int j = 0; j < (1<<n); j++)
			dp[i][j] = -1;
}

void work(int _)
{
	dp[0][0] = 0;
	int tt = (1 << n) - 1;
	for(int i = 0; i < m; i++)
		for(int j = 0; j <= tt; j++) {
			if(dp[i][j] <= -0.5) continue;
			for(int k = 0; k < n; k++) {
				if(j & (1 << k)) continue;
				if((j | (1 << k)) != tt) dp[i+1][j | (1 << k)] = max(dp[i+1][j | (1 << k)], dp[i][j] + g[k][i]);
				else dp[i+1][0] = max(dp[i+1][0], dp[i][j] + g[k][i]);
			}
		}
	double ans = 0;
	for(int i = 0; i <= tt; i++) ans = max(dp[m][i], ans);
	printf("Case #%d: %.5f\n", _, ans);
}

int main(void)
{
	int _, __;
	while(scanf("%d", &_)!=EOF) {
		__ = 0;
		while(_--) {
			read();
			init();
			work(++__);
		}
	}
	return 0;
}


你可能感兴趣的:(HDU)