Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:直接暴力枚举,状态数 6!*5!==720*120==O(7*10^4)

Posted by tasty at 2012-08-23 23:30:35 on Problem 2743
In Reply To:直接暴力枚举,状态数 6!*5!==720*120==O(7*10^4) Posted by:tasty at 2012-08-23 23:29:28
> rt
贴代码
Source Code

Problem: 2743  User: tasty 
Memory: 1292K  Time: 16MS 
Language: C++  Result: Accepted 

Source Code 
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
double r,w[10],value[64],ans;
int n;
struct pp
{
     double l1,l2;
};
pp adj[64][720*120];
bool vis[64];
int num[64];
void dfs(int state)
{
     int i,j,s,p,q;
     double l1,l2;
     bool have=false;
     if(vis[state])
        return;
     vis[state]=true;
     for(i=(state&(state-1));i>0;i=((i-1)&state))
     {
         j=state^i;
         dfs(i);
         dfs(j);
         for(s=0;s<num[i];s++)
            for(p=0;p<num[j];p++)
            {
                l1=value[j]/(value[i]+value[j]);
                l2=value[i]/(value[i]+value[j]);
                adj[state][num[state]].l1=max(adj[i][s].l1+l1,adj[j][p].l1-l2);
                adj[state][num[state]].l2=max(adj[i][s].l2-l1,adj[j][p].l2+l2);
                if(adj[state][num[state]].l1+adj[state][num[state]].l2<=r)
                    num[state]++;
            }
         have=true;
     }
     if(!have)
     {
         adj[state][num[state]].l1=0;
         adj[state][num[state]++].l2=0;
     }
}
int main()
{
    int t,i,j,s,p,q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%d",&r,&n);
        for(i=0;i<n;i++)
            scanf("%lf",&w[i]);
        for(i=0;i<(1<<n);i++)
        {
             value[i]=0;
             for(j=0;j<n;j++) 
             {
                 if(i&(1<<j))
                    value[i]+=w[j];                 
             }
        }
        memset(num,0,sizeof(num));
        memset(vis,false,sizeof(vis));
        dfs((1<<n)-1);
        ans=-1;
        for(i=0;i<num[(1<<n)-1];i++)
        {
            if(adj[(1<<n)-1][i].l1+adj[(1<<n)-1][i].l2>ans)
               ans=adj[(1<<n)-1][i].l1+adj[(1<<n)-1][i].l2;
        }
        printf("%.20f\n",ans);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator