hdu 5727 Necklace 状压dp/二分图匹配

Necklace

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3061    Accepted Submission(s): 958


Problem Description
SJX has 2*N magic gems. N of them have Yin energy inside while others have Yang energy. SJX wants to make a necklace with these magic gems for his beloved BHB. To avoid making the necklace too Yin or too Yang, he must place these magic gems Yin after Yang and Yang after Yin, which means two adjacent gems must have different kind of energy. But he finds that some gems with Yang energy will become somber adjacent with some of the Yin gems and impact the value of the neckless. After trying multiple times, he finds out M rules of the gems. He wants to have a most valuable neckless which means the somber gems must be as less as possible. So he wonders how many gems with Yang energy will become somber if he make the necklace in the best way.
 

Input
  Multiple test cases.

  For each test case, the first line contains two integers N(0N9),M(0MNN) , descripted as above.

  Then M lines followed, every line contains two integers X,Y , indicates that magic gem X with Yang energy will become somber adjacent with the magic gem Y with Yin energy.
 

Output
One line per case, an integer indicates that how many gem will become somber at least.
 

Sample Input
  
  
2 1 1 1 3 4 1 1 1 2 1 3 2 1
 

Sample Output
  
  
1 1


一看数据就想到用状压dp做,很好想,大致就像这样:d[i][j]=min(d[i^(1<<j)][k]+mp[k][j]),但是有些细节考虑得很头疼。因为每个Yang energy的somber只能计算一次,所以需要记录当前Yang energy是否已经somber,还有一个就是第一个Yang energy的情况要特殊统计。详见代码:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;
const int N=(1<<18)+10;
const int INF=0x7fffff;
int n,m,mp[11][11],d[N][20];
int s[N][20];//记录当前状态下的当前 Yang energy 是否已经 somber
int z[N][20];//记录的当前状态下,第1个Yang energy 是否已经 somber

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(mp,0,sizeof(mp));
        memset(s,0,sizeof(s));
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x-1][y-1+n]=1;
        }
        if(n==0||m==0)//懒得处理细节,直接特判一下
        {
            printf("0\n");
            continue;
        }
        if(n==1&&m==1)//特判
        {
            printf("1\n");
            continue;
        }
        int ed=1<<(n+n);
        for(int i=0;i<ed;i++)
            for(int j=0;j<n*2;j++)
                d[i][j]=INF;
        d[1][0]=0;//因为是一个环,我们从哪里开始都一样,所以选择从第一个Yang energy开始考虑
        for(int i=0;i<ed;i++)
        {
            int sum=0;
            for(int j=0;j<n*2;j++)
            {
                if((i>>j)&1)    sum++;
            }
            if(sum&1)//处理 Yang energy
            {
                for(int j=0;j<n;j++)
                {
                    int kk=-1;
                    if((i>>j)&1)
                    {
                        for(int k=n;k<n+n;k++)
                            if((i>>k)&1)
                            {
                                int t=d[i^(1<<j)][k]+mp[j][k];
                                if(t<d[i][j])
                                {
                                    d[i][j]=t;
                                    kk=k;
                                }
                            }
                    }
                    if(kk!=-1)   s[i][j]=mp[j][kk];//判断当前 Yang energy 是否已经 somber
                    z[i][j]=z[i^(1<<j)][kk];//传递第1个Yang energy 是否已经 somber
                }
            }
            else//处理Yin energy
            {
                for(int j=n;j<n+n;j++)
                {
                    if((i>>j)&1)
                    {
                        int kk=-1;
                        for(int k=0;k<n;k++)
                            if((i>>k)&1)
                            {//懒得写判断语句,如果前一个Yang energy 已经 somber 就不需要再次统计
                                int a=d[i^(1<<j)][k],b=1-s[i^(1<<j)][k],c=1-z[i^(1<<j)][k];
                                if(sum==n*2)    d[i][j]=min(d[i][j],a+mp[k][j]*b+mp[0][j]*c);//处理最后一个Yin energy,考虑前一个还有第一个Yang energy
                                else
                                {
                                    if(a+mp[k][j]*b<d[i][j])
                                    {
                                        d[i][j]=a+mp[k][j]*b;
                                        kk=k;
                                    }
                                }
                            }
                        if(kk!=-1)  z[i][j]=z[i^(1<<j)][kk];//传递第一个Yang energy 的 somber 情况
                        if(sum==2)  z[i][j]=mp[0][j];//判断第一个Yang energy 的 somber 情况
                    }
                }
            }
        }
        int ans=d[ed-1][n];
        for(int i=n+1;i<n+n;i++)    ans=min(ans,d[ed-1][i]);
        printf("%d\n",ans);
    }
}


二分图匹配方法:枚举所有的Yin energy排列情况,因为是个环,所有只有2^8种情况,对每个Yang energy,如果插入一个位置不会somber,就建一条边,最后求其二分图匹配。最小值为min(n-hungry)。

#include <iostream>
#include<bits/stdc++.h>

using namespace std;
const int N=1<<9;
int head[N],cnt,n,v[N],pre[N],a[11],mp[22][22],m,ans;

struct node
{
    int v,next;
}e[N];

void add(int u,int v)
{
    e[cnt].v=v;
    e[cnt].next=head[u];
    head[u]=cnt++;
}

int dfs(int u)
{
    for(int i=head[u];i+1;i=e[i].next)
    {
        int c=e[i].v;
        if(!v[c])
        {
            v[c]=1;
            if(pre[c]==-1||dfs(pre[c]))
            {
                pre[c]=u;
                return 1;
            }
        }
    }
    return 0;
}

int hungry()
{
    int ans=0;
    memset(pre,-1,sizeof(pre));
    for(int i=1;i<=n;i++)
    {
        memset(v,0,sizeof(v));
        if(dfs(i))  ans++;
    }
    return ans;
}

void solve()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int pre=j,next=j+1;
            if(next==n+1)   next=1;
            if(!mp[i][a[pre]]&&!mp[i][a[next]]) add(j,i);
        }
    }
    ans=min(ans,n-hungry());
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(mp,0,sizeof(mp));
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x][y]=1;
        }
        if(n==0||m==0)
        {
            printf("0\n");
            continue;
        }
        for(int i=1;i<=n;i++)   a[i]=i;
        ans=n;
        do
        {
            solve();
        }while(next_permutation(a+1,a+n));
        printf("%d\n",ans);
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值