hdu 5727 Necklace (全排列+二分匹配)

本文介绍了一个经典的宝石项链问题,探讨如何将阴阳属性的宝石按特定规则串成项链,以最小化受规则限制的宝石数量。文章提供了一种通过全排列与最大二分匹配算法来解决该问题的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
题意:给你2*n颗宝石,其中n颗阳宝石,n颗阴宝石,让你阴阳间隔串在项链上(围成一个环),还给你了m对x,y,表示如果阳宝石x和阴宝石y挨在一起,阳宝石就会受影响,让你求出阳宝石受影响的最少数量。
暴力解法:我们可以用全排列先枚举阴宝石的所有放法,让后看每两个阴宝石中间放哪些阳宝石而不受影响,那么我们可以对每个空位编号从1-n,这个空位编号向对应阳宝石编号连边,注意最后一个阴宝石和第一个阴宝石也要算进去,然后跑最大二分匹配,求出未匹配点数就可以更新答案,如果矩阵存图会超时,换邻接表就行。
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
bool no[20][20];
int maze[20][20];
int a[20];
int n,m;
int cnt,p[20];
struct node
{
    int v,next;
}E[1000];
void add(int u,int v)
{
    E[cnt].v=v;
    E[cnt].next=p[u];
    p[u]=cnt++;
}
int fg[20];
int pp[20];
bool Find(int x)
{
    for(int i=p[x];i!=-1;i=E[i].next)
    {
        int y=E[i].v;
        if(!fg[y])
        {
            fg[y]=1;
            if(pp[y]==0||Find(pp[y]))
            {
                pp[y]=x;
                return 1;
            }
        }
    }
    return 0;
}
int solve()
{
    int ans=0;
    for(int i=1;i<=n;i++) pp[i]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++) fg[i]=0;
        if(Find(i)) ans++;
    }
    return ans;
}
int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
            no[i][j]=false;
        for(int i=1;i<=m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            no[y][x]=true;
        }
        if(m==0)
        {
            puts("0");
            continue;
        }
        for(int i=1;i<=n;i++) a[i]=i;
        int ans=20;
        do
        {
            cnt=0;
            for(int i=1;i<=n;i++) p[i]=-1;
            for(int i=1;i<n;i++)
                for(int j=1;j<=n;j++)
                    if(!no[a[i]][j]&&!no[a[i+1]][j]) add(i,j);
            for(int j=1;j<=n;j++)
                if(!no[a[1]][j]&&!no[a[n]][j]) add(n,j);
            ans=min(ans,n-solve());
            if(ans==0) break;
        }while(next_permutation(a+1,a+n+1));
        printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值