匈牙利算法求最大匹配数

bool 寻找从k出发的对应项出的可增广路
{
    while (从邻接表中列举k能关联到顶点j)
    {
        if (j不在增广路上)
        {
            把j加入增广路;
            if (j是未盖点 或者 从j的对应项出发有可增广路)
            {
                修改j的对应项为k;
                则从k的对应项出有可增广路,返回true;
            }
        }
    }
    则从k的对应项出没有可增广路,返回false;
}

void 匈牙利hungary()
{
    for i->1 to n
    {
        if (则从i的对应项出有可增广路)
            匹配数++;
    }
    输出 匹配数;
}

 

 

例子:

 

Asteroids

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 27   Accepted Submission(s) : 14
Problem Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. 

Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
 

 

Input
* Line 1: Two integers N and K, separated by a single space. 
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
 

 

Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
 

 

Sample Input
3 4 1 1 1 3 2 2 3 2
 

 

Sample Output
2
 
 
 
 

把方阵看做一个特殊的二分图(以行列分别作为两个顶点集V1、V2,其中| V1|=| V2|)

然后把每行x或者每列y看成一个点,而障碍物(x,y)可以看做连接x和y的边。按照这种思路构图后。问题就转化成为选择最少的一些点(x或y),使得从这些点与所有的边相邻,其实这就是最小点覆盖问题。

 

 

//转自優YoU http://user.qzone.qq.com/289065406/blog/1299322465

#include<iostream>  
using namespace std;  
  
int n,k;  //n矩阵规格,k星体数量  
int V1,V2;       //二分图顶点集  
 /*矩阵的行列分别属于二分图的两个顶点集V1、V2,其中行x∈V1,列y∈V2*/  
bool grid[501][501];  //存储数据方式:可达矩阵  
bool vis[501];     //记录V2的点每个点是否已被搜索过  
int link[501];     //记录 V2中的点y 在 V1中 所匹配的点x的编号  
int m;  //最大匹配数  
  
/*Hungary Algorithm*/  
  
bool dfs(int x)  
{  
    for(int y=1;y<=V2;y++)  
        if(grid[x][y] && !vis[y])  //x到y相邻(有边) 且 节点y未被搜索  
        {  
            vis[y]=true;   //标记节点y已被搜索  
            if(link[y]==0 || dfs(link[y])) //link[y]==0 : 如果y不属于前一个匹配M  
            {                               //find(link[y] : 如果被y匹配到的节点可以寻找到增广路  
                link[y]=x; //那么可以更新匹配M'(用M替代M')  
                return true;  //返回匹配成功的标志  
            }  
        }  
    return false;  //继续查找V1下一个x的邻接节点  
}  
  
void search(void)  
{  
    for(int x=1;x<=V1;x++)  
    {  
        memset(vis,false,sizeof(vis)); //清空上次搜索时的标记  
        if(dfs(x))  //从V1中的节点x开始寻找增广路  
            m++;  
    }  
    return;  
}  
  
int main(void)  
{  
    cin>>n>>k;  
    V1=V2=n;  
  
    int x,y;         //坐标(临时变量)  
    for(int i=1;i<=k;i++)  
    {  
        cin>>x>>y;  
        grid[x][y]=true;   //相邻节点标记  
    }  
  
    /*增广轨搜索*/  
  
    search();  
  
    /*Output*/  
  
    cout<<m<<endl;  
  
    return 0;  
}  

  

posted on 2013-11-23 17:43  平心静气  阅读(653)  评论(0)    收藏  举报

导航