【二分图|最大匹配】POJ-3041 Asteroids(匈牙利算法dfs、bfs版)

本文介绍了一种算法,用于解决在给定的网格中,通过射击行或列来消除所有小行星所需的最少射击次数的问题。提供了两种实现方法:一种使用深度优先搜索(DFS),另一种使用宽度优先搜索(BFS)。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Asteroids
Time Limit: 1000MS Memory Limit: 65536K
   

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
————————————————————寝る前の分割線————————————————————
思路:水题不解释,注意行列编号从1开始。
dfs版代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 555, M = 11111;
int tot, head[N], cou[N], n, m;
bool vis[N];
struct Edge {
	int v, next;
	Edge(){}
	Edge(int _v, int _next):
		v(_v), next(_next){}
}edge[M];

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v)
{
	edge[tot] = Edge(v, head[u]);
	head[u] = tot++;
}

int dfs(int u)
{
	for(int i = head[u]; ~i; i = edge[i].next) {
		int v = edge[i].v;
		if(!vis[v]) {
			vis[v] = 1;
			if(cou[v] == -1 || dfs(cou[v])) {
				cou[v] = u;
				return 1;
			}
		}
	}
	return 0;
}

int match()
{
	int ret = 0;
	memset(cou, -1, sizeof(cou));
	for(int i = 1; i <= n; i++) {
		memset(vis, 0, sizeof(vis));
		ret += dfs(i);
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	while(~scanf("%d%d", &n, &m)) {
		int u, v;
		init();
		for(int i = 0; i < m; i++) {
			scanf("%d%d", &u, &v);
			add(u, v);
		}
		int ans = match();
		printf("%d\n", ans);
	}
	return 0;
}

bfs版代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 555, M = 11111;
int tot, head[N], mx[N], my[N], pre[N], n, m;
int vis[N];
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];

void init()
{
    tot = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

bool bfs(int st)
{
    queue <int> q;
    q.push(st);
    pre[st] = -1;
    bool flag = false;
    while(!q.empty() && !flag) {
        int u = q.front(); q.pop();
        for(int i = head[u]; ~i && !flag; i = e[i].next) {
            int v = e[i].v;
            if(vis[v] != st) {
                vis[v] = st;
                q.push(my[v]);
                if(~my[v]) pre[my[v]] = u;
                else {//找到增广路
                    int a = u, b = v;
                    flag = true;
                    while(~a) {
                        int t = mx[a];
                        mx[a] = b;
                        my[b] = a;
                        a = pre[a];
                        b = t;
                    }
                }
            }
        }
    }
    return mx[st] != -1;
}

int hungary()
{
    int ret = 0;
    memset(mx, -1, sizeof(mx));
    memset(my, -1, sizeof(my));
    memset(vis, -1, sizeof(vis));
    for(int i = 1; i <= n; i++) {//number from 1
        if(mx[i] == -1) {
            if(bfs(i)) ret++;
        }
    }
    return ret;
}

int main()
{
#ifdef J_Sure
//  freopen("000.in", "r", stdin);
//  freopen(".out", "w", stdout);
#endif
    while(~scanf("%d%d", &n, &m)) {
        int u, v;
        init();
        for(int i = 0; i < m; i++) {
            scanf("%d%d", &u, &v);
            add(u, v);
        }
        int ans = hungary();
        printf("%d\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值