HDU - 5727(57/600)

SJX has 2*N magic gems. NN 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)N(0≤N≤9),M(0≤M≤N∗N), descripted as above.

Then MM lines followed, every line contains two integers X,YX,Y, indicates that magic gem XX with Yang energy will become somber adjacent with the magic gem YY 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

这个题数据量特别小
可以暴力

暴力阶乘阴珠子然后杨珠子不冲突就插进去和位置建边

跑一边匈牙利
多校的题时间空间要求的真苛刻….

然后学了一些next_permutation( , )
这两个参数代表着左闭右开的区间

#include<bits/stdc++.h>
using namespace std;
int use[121],vis[121],n,m,wz[100],wy[100];
int tu[30][30];
int mp[1100][1100];
int zhao(int gen)
{
    for(int a=1;a<=2*n;a++)
    {
        if(!tu[gen][a])continue;
        if(vis[a])continue;
        vis[a]=1;
        if(use[a]==-1||zhao(use[a]))
        {
            use[a]=gen;
            return 1;
        }
    }
    return 0;
}
int xiongyali()
{
    int fs=0;
    memset(use,-1,sizeof(use));
    for(int a=1;a<=2*n;a++)
    {
        memset(vis,0,sizeof(vis));
        if(zhao(a))fs++;
    }
    return fs;
}
int ty[50];
int main()
{
    while(cin>>n>>m)
    {
        if(n==0)  
        {  
            printf("0\n");  
            continue;  
        } 
        for(int a=1;a<=n;a++)ty[a]=a;
        memset(mp,0,sizeof(mp));
        for(int a=1;a<=m;a++)
        {
            scanf("%d%d",&wz[a],&wy[a]);
            mp[wz[a]][wy[a]]=1;
        }
        int jg=0x3f3f3f3f;
        memset(tu,0,sizeof(tu));
        do
        {
            for(int a=1;a<=n;a++)
            {
                for(int b=1;b<=n;b++)
                {
                    tu[a+n][b]=0;
                    if(b==n)
                    {
                        if(mp[a][ty[b]]||mp[a][ty[1]])continue;
                        tu[a+n][b]=1;
                        continue;
                    }
                    if(mp[a][ty[b]]||mp[a][ty[b+1]])continue;
                    tu[a+n][b]=1;
                }
            }
            jg=min(jg,n-xiongyali());
        }while(next_permutation(ty+2,ty+n+1));
        cout<<jg<<endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值