2016 多校1 1005 二分图最大匹配

探讨如何通过匈牙利算法解决一个宝石项链问题,该问题要求制作的项链阴阳能量交替排列,并尽量减少某些宝石变得暗淡的情况。通过构建二分图并使用匈牙利算法求解最大匹配,来最小化暗淡宝石的数量。

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(0≤N≤9),M(0≤M≤N∗N), 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

8!暴力枚举阴的部分建图后,转化为二分图最大匹配问题。匈牙利算法跑一遍即可。
之所以是8!而不是9!是因为环的对称性,可以直接set第一个是1。

代码:

#include <bits/stdc++.h>
using namespace std;
int a[15];
int n, m;
int Max;
bool vis[15];
bool vis2[15];
int linker[15];
bool mp[15][15];
vector<int>g[15];
struct match{
    int x, y;
};
//map <match, int>mp;
bool operator<(match const&a, match const&b)
{
    if(a.x!=b.x)return a.x<b.x;
    else return a.y<b.y;
}

bool dfs2(int u)
{
    for(int v=0;v<g[u].size();v++)
    {
        int tmp=g[u][v];
        if(!vis2[tmp])
        {
            vis2[tmp]=true;
            if(linker[tmp]==-1||dfs2(linker[tmp]))
            {
                linker[tmp]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    for(int i=1;i<=n;i++)g[i].clear();
    int x1, x2, y1, y2;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            x1=i;x2=i;
            y1=a[j];
            if(j==n)y2=a[1];
            else y2=a[j+1];
            if(!mp[x1][y1]&&!mp[x2][y2])g[i].push_back(j);
        }
    }
    //printf("pic is:\n");
    /*for(int i=1;i<=n;i++)
    {
        for(int j=0;j<g[i].size();j++)
            printf("%d ", g[i][j]);
        printf("\n");
    }*/
    int res=0;
    memset(linker, -1, sizeof(linker));
    for(int u=1;u<=n;u++)
    {
        memset(vis2, false, sizeof(vis2));
        if(dfs2(u))res++;
    }
    return res;
}

void dfs(int flor)
{
    if(flor==n+1)
    {
        int res=hungary();
        Max=max(Max, res);
        //for(int i=1;i<=n;i++)
        //    printf("%d%c", a[i],i==n?'\n':' ');
        return;
    }
    for(int i=2;i<=n;i++)
    {
        if(!vis[i])
        {
            vis[i]=true;
            a[flor]=i;
            dfs(flor+1);
            vis[i]=false;
        }
    }
    return;
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        //mp.clear();
        memset(mp, 0, sizeof(mp));
        int x, y;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d", &x, &y);
            //match tmp;
            //tmp.x=x;tmp.y=y;
            mp[x][y]=true;
        }
        Max=0;
        memset(vis, false, sizeof(vis));
        a[1]=1;
        dfs(2);
        printf("%d\n",n-Max);
    }
    return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值