poj2288

注意:确保前2个状态之间是连接的。到不了的状态要跳过。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
//#include <unordered_set>
#define mkp make_pair
#define err cout<<"here"<<endl
using namespace std;
const double EPS=1e-12;
typedef long long lon;
typedef unsigned long long ull;
typedef map<ull,int>::iterator IT;
const lon SZ=13,SSZ=3010,APB=4,mod=100000,one=97;
const lon INF=0x7FFFFFFF;
lon n,m,arr[SZ],dp[1<<SZ][SZ][SZ];
lon num[1<<13][SZ][SZ];
struct nd{
    lon to,wt;
    nd(lon a=0,lon b=0):to(a),wt(b){}
};
bool mp[SZ][SZ];

void dfs(lon sta,lon x,lon y)
{
    if(__builtin_popcount(sta)==2)
    {
        num[sta][x][y]=1;
        dp[sta][x][y]=arr[x]+arr[y]+arr[x]*arr[y];
        return;
    }
    else if(dp[sta][x][y]!=-1)return;
    else
    {
        dp[sta][x][y]=0;
        lon nsta=sta^(1<<x);
        for(lon i=0;i<n;++i)
        {
            if((nsta&(1<<i))&&i!=y&&mp[i][y])
            {
                dfs(nsta,y,i);
                lon tmp=dp[nsta][y][i];
                if(tmp==0||tmp==-1)continue;
                tmp+=arr[x]+arr[x]*arr[y];
                if(mp[x][i])tmp+=arr[x]*arr[y]*arr[i];
                if(tmp>dp[sta][x][y])
                {
                    dp[sta][x][y]=tmp;
                    num[sta][x][y]=num[nsta][y][i];
                }
                else if(tmp==dp[sta][x][y])
                {
                    num[sta][x][y]+=num[nsta][y][i];
                }
            }
        }
    }
}

void init()
{
    cin>>n>>m;
    memset(mp,0,sizeof(mp));
    memset(dp,-1,sizeof(dp));
    lon sum=0;
    for(lon i=0;i<n;++i)cin>>arr[i],sum+=arr[i];
    for(lon i=1;i<=m;++i)
    {
        lon a,b;
        cin>>a>>b;
        --a,--b;
        mp[a][b]=1;
        mp[b][a]=1;
    }
    lon sta=(1<<n)-1;
    if(n==1)cout<<sum<<" "<<1<<endl;
    if(n==2)
    {
        if(m==0)cout<<0<<" "<<0<<endl;
        else 
        cout<<sum+arr[0]*arr[1]<<" "<<1<<endl;
    }
    if(n<=2)return;
    lon res1=0,res2=0;
    //cout<<sta<<endl;
    for(lon i=0;i<n;++i)
    {
        if(sta&(1<<i))
        for(lon j=0;j<n;++j)
        {
            if(sta&(1<<j))
            if(j!=i&&mp[i][j])
            {
                for(lon k=0;k<n;++k)
                {//cout<<i<<" "<<j<<" "<<k<<endl;
                    if(sta&(1<<k))
                    if(k!=i&&k!=j&&mp[j][k])
                    {
                        //cout<<i<<" "<<j<<" "<<k<<endl;
                        lon nsta=sta^(1<<i);
                        dfs(nsta,j,k);
                        lon cur=dp[nsta][j][k];
                        if(cur==0||cur==-1)continue;
                        cur+=arr[i]+arr[i]*arr[j];
                        if(mp[i][k])cur+=arr[i]*arr[j]*arr[k];
//                        if(cur==152)
//                        {
//                            cout<<"i: "<<i<<" "<<j<<" "<<k<<endl;
//                        }
                        if(cur>res1)
                        {
                            res1=cur;
                            res2=num[nsta][j][k];
                        }
                        else if(cur==res1)
                        {
                            res2+=num[nsta][j][k];
                        }
                    }
                }
            }
        }
    }
//    for(int i=0;i<=sta;++i)
//    {
//        if(__builtin_popcount(i)>2)
//        {
//            for(int j=0;j<n;++j)
//            {
//                if(i&(1<<j))
//                {
//                    for(int k=0;k<n;++k)
//                    {
//                        if((i&(1<<k))&&mp[j][k])
//                        {
//                            cout<<i<<" "<<j<<" "<<k<<" "<<dp[i][j][k]<<endl;
//                        }
//                    }
//                }
//            }
//        }
//    }
    if(res1==0)res2=0;
    cout<<res1<<" "<<res2/2<<endl;
}

void work()
{
    
}

void release()
{    
    
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin);
    lon casenum;
    cin>>casenum;
    //cout<<casenum<<endl;
    for(lon time=1;time<=casenum;++time)
    //for(lon time=1;cin>>n,n;++time)
    {
        init();
        work();
        release();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/10879486.html
今日推荐