zoukankan      html  css  js  c++  java
  • HDU5727 Necklace(二分图匹配)

    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
    Sample Input
    2 1
    1 1
    3 4
    1 1
    1 2
    1 3
    2 1
     
    
    Sample Output
    1
    1

    题意:

      有2n个珠子,分成阴阳两极,每极各n个。用这2n个珠子做成一个项链,使得相邻两个珠子的极性是不一样的,有一些阳性的珠子会被一些阴性的珠子所削弱,在它们它们相邻的情况下。

      给你m个关系[x,y]表示阳性珠子x会被阴性珠子y在相邻情况下所削弱。问你最少有多少个阳性被削弱。

    思路:

      固定阳珠,然后对阴珠进行全排列,枚举每一种情况,对于一种排列, 给每一个位置对应的不会褪色的阳珠建边, 做出二维数组,跑出的最大匹配就是最多的不褪色阳珠子个数。我们通过匈牙利算法算出最大匹配sum,然后算出n-sum,对于每种排列取最小值就得到了我们想要的答案,注意特判下n=0的情况。

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    #define MAX 0x3f3f3f3f
    
    int map[100][100];//map数组存的是阴阳珠是否消退,如果map[1][1]=1表示1号阳珠和1号阴珠能消退
    int a[100];//用来存储排列的方式
    int g[100][100];//数组存的是排列后的阴阳珠能否消退
    
    int linker[100];//用来标记
    int flag[100];//用来标记
    
    int n;//n对珠
    
    bool dfs(int u)
    {
        for(int v=1; v<=n; v++)
        {
            if(g[u][v]&&!flag[v])
            {
                flag[v]=true;
                if(linker[v]==-1||dfs(linker[v]))
                {
                    linker[v]=u;
                    return true;
                }
            }
        }
        return false;
    }
    
    int hungary()
    {
        int res=0;
        memset(linker,-1,sizeof linker);
        for(int u=1; u<=n; u++)
        {
            memset(flag,false,sizeof(flag));
            if(dfs(u))
                res++;
        }
        return res;
    }
    
    int main()
    {
        int m;
        while(~scanf("%d%d",&n,&m))//有n对宝石,即n个阳,n个阴
        {
            if(n==0)//如果0对,直接输出0
            {
                cout<<0<<endl;
                continue;
            }
            memset(map,0,sizeof(map));//开始初始化为0,表示都不消退
            for(int i=1; i<=m; i++)
            {
                int a,b;
                cin>>a>>b;
                map[a][b]=1;
            }//更新map数组,能消退的标记为1
            int ans=MAX;
            for(int i=1; i<=n; i++)
                a[i]=i;//初始化a[i]数组为本身,一会用于全排列
            do
            {
                for(int i=1; i<=n; i++)//固定阳珠,阴珠全排列
                {
                    for(int j=1; j<=n; j++)
                    {
                        g[i][j]=0;
                        //如果是第三个,则特判(因为是环形,所以第三个应该检查是否与第3个排列数的阳珠和第1个排列数的阳珠消退)
                        if(j==n)
                        {
                            //如果3号阳珠既不与第3个阴珠消退也不和第1个阴珠消退,标记为1,注意是排列之后的第一个和第三个
                            if(!map[i][a[j]]&&!map[i][1])
                                g[i][j]=1;
                        }
                        //如果阳珠不与相邻的两个阴珠消退,则标记为1
                        else if(!map[i][a[j]]&&!map[i][a[j+1]])
                        {
                            g[i][j]=1;
                        }
                    }
                }
                int num=hungary();//求出在此排列顺序中最大的匹配数
                ans=min(ans,n-num);
            }
            while(next_permutation(a+2,a+n+1)); //全排列,排列出来的存在a数组中
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    typescript基础语法--变量/函数/指令/类
    java获取post请求头部字符串
    spring MVC 3.2中@ResponseBody(Post接口)返回乱码的完美解决方案
    java+js正则表达式获取URL(带端口)域名
    jquery的ajax提交时加载处理方法
    js截取+全部替换+字符串
    Filter过滤器除去部分URL链接
    Eclipse常用快捷键
    CAS+Tomcat SSL第三方数据证书导入(jks)
    CAS客户端和服务器配置https证书
  • 原文地址:https://www.cnblogs.com/aiguona/p/7230476.html
Copyright © 2011-2022 走看看