poj 3041 Asteroids(java + 匈牙利算法)

package 匈牙利算法;

import java.util.Scanner;

/**问题请参考http://poj.org/problem?id=3041
 * @author rayli

 * @date:2014-7-31 下午3:03:44
 * @解题思路:请参考 :http://rayliwong23.blog.163.com/blog/static/205570134201463125747651/
 *
 */
public class Asteroids
{
    static boolean map[][];
    boolean vist[];
    int link[] = new int[401];
    
    boolean dfs(int x, int n)
    {
        for(int y=1; y<=n; y++)
        {
            if(map[x][y] && !vist[y])
            {
                vist[y] = true;
                
                if(link[y] == 0 || dfs(link[y], n))
                {
                    link[y] = x;//刚开始数据是从0行0列处理数据的,但是在这行当x=0出现了错误,相当于把link[y]处理两回。
                    return true;
                }
            }
        }
        return false;
    }
    
    int algorithm(int n)
    {//匈牙利算法
        int M = 0;
        for(int i=1; i<=n; i++)
        {
            vist = new boolean[n+1];
            
            if(dfs(i, n))
                M++;
        }
        return M;
    }
    
    void output(int ans)
    {//输出为无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2
        System.out.println(ans);
    }
    
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();//N行N列
        int num = cin.nextInt();//结点数
        map = new boolean [n+1][n+1];
    
        while(num --> 0)
        {
            int r = cin.nextInt();
            int c = cin.nextInt();
            
            map[r][c] = true;
        }
            
        Asteroids a = new Asteroids();
        int ans = a.algorithm(n);
        a.output(ans);
        
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值