poj 3041 Asteroids(java + 匈牙利算法)
来源:互联网 发布:广州seo顾问 编辑:程序博客网 时间:2024/06/05 11:17
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);
}
}
阅读全文
0 0
- poj 3041 Asteroids(java + 匈牙利算法)
- 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(匈牙利算法)
- POJ 3041 Asteroids(匈牙利算法)
- POJ 3041 Asteroids(匈牙利算法)
- poj 3041 Asteroids【二分图】【匈牙利算法】
- POJ:3041-Asteroids(匈牙利算法模板)
- Asteroids(POJ 匈牙利算法)
- POJ 3041 Asteroids 二分图匹配 匈牙利算法
- POJ 3041 Asteroids 匈牙利算法/最小点覆盖
- poj 1258 Agri-Net( java prim)
- poj 1789 Truck History(java + prim)
- poj 1094 Sorting It All Out(java + 拓扑排序)
- poj 3020 Antenna Placement(java + 匈牙利算法)
- Java项目经验——程序员成长的钥匙
- poj 3041 Asteroids(java + 匈牙利算法)
- int main(int argc,char* argv[])详
- matlab 记一次debug的心路历程以及NaN问题的解决办法
- 关于物联网的一点畅想
- restrict关键字
- 常有关pdf、360浏览器、Chrome浏览器和win7保护眼睛的颜色设置方法总结
- 基于UDP的新一代多路传输协议—QUIC(一)
- 基于UDP的新一代多路传输协议—QUIC(二)
- Hive数据类型