poj 3041 Asteroids(java + 匈牙利算法)
2017-08-19 14:03
465 查看
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);
}
}
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);
}
}
相关文章推荐
- POJ 3041 Asteroids 匈牙利算法
- POJ - 3041 Asteroids (匈牙利算法)
- POJ 3041 Asteroids (匈牙利算法最小点覆盖)
- POJ 3041 Asteroids (匈牙利算法)
- poj 3041 Asteroids 【匈牙利算法】
- POJ 3041 Asteroids 【匈牙利算法最小点覆盖】
- POJ 3041 Asteroids(匈牙利算法)
- POJ 3041 Asteroids 匈牙利算法 最小点覆盖
- POJ 3041 Asteroids 【匈牙利算法最小点覆盖】
- POJ:3041-Asteroids(匈牙利算法模板)
- poj3041——Asteroids(二分图,匈牙利算法)
- poj 3041 Asteroids 匈牙利算法
- (模板题)poj 3041 Asteroids(二分图的最大匹配匈牙利算法)
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- poj_3041 Asteroids(匈牙利算法+最小点覆盖)
- POJ 3041 Asteroids (匈牙利算法)
- POJ 3041 Asteroids 匈牙利算法
- 【POJ 3041 】Asteroids (匈牙利算法最小点覆盖&二分图)
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)