POJ3041 Asteroids(匈牙利算法)

嘟嘟嘟

虽然我已经会网络流了,但是还是学了一个匈牙利算法。

——就跟我会线段树,但还是学了树状数组一样。

其实匈牙利算法挺暴力的。简单来说就是先贪心匹配,然后如果左部点\(i\)匹配不上了,就尝试更改前面已经匹配好的点,腾出地给他匹配。

因此对于每一个点跑一遍匈牙利算法,如果这个点匹配成功,总匹配数就加1。

感觉没啥好讲的。

关于这道题怎么做,看我这篇博客吧。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(‘ ‘)
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 505;
const int maxe = 1e4 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ‘ ‘;
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - ‘0‘, ch = getchar();
  if(last == ‘-‘) ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar(‘-‘);
  if(x >= 10) write(x / 10);
  putchar(x % 10 + ‘0‘);
}

int n, k;
struct Edge
{
  int nxt, to;
}e[maxe];
int head[maxn], ecnt = -1;
void addEdge(int x, int y)
{
  e[++ecnt] = (Edge){head[x], y};
  head[x] = ecnt;
}

int fa[maxn], vis[maxn], vcnt = 0;
//fa:下标是右部点,表示右部点i和左部点fa[i]匹配上了
//vis:表示匹配点i的时候这个点是不是i要匹配的
//因此每一次dfs前应该清空,为了降低复杂度,改为累加标记
bool dfs(int now)
{
  for(int i = head[now], v; i != -1; i = e[i].nxt)
    {
      if(vis[v = e[i].to] != vcnt)
    {
      vis[v] = vcnt;
      if(!fa[v] || dfs(fa[v])) {fa[v] = now; return 1;}
    }
    }
  return 0;
}

int main()
{
  Mem(head, -1);
  n = read(); k = read();
  for(int i = 1; i <= k; ++i)
    {
      int x = read(), y = read();
      addEdge(x, y);
    }
  int ans = 0;
  for(int i = 1; i <= n; ++i)
    {
      ++vcnt;
      if(dfs(i)) ans++;
    }
  write(ans), enter;
  return 0;
}

原文地址:https://www.cnblogs.com/mrclr/p/10015661.html

时间: 2024-08-30 11:29:51

POJ3041 Asteroids(匈牙利算法)的相关文章

POJ 3041 Asteroids (匈牙利算法)

Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14388 Accepted: 7828 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 astero

POJ 3014:Asteroids(二分匹配,匈牙利算法)

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14399   Accepted: 7836 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 as

二分图匹配(匈牙利算法) POJ 3041 Asteroids

题目传送门 1 /* 2 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 3 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 4 趣味入门:http://blog.csdn.net/dark_scope/article/details/8880547 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include

POJ #3041 Asteroids 3041 二分图最小覆盖 最大匹配 匈牙利算法

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

poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利用这个武器摧毁所有的小行星最少需要几发光束. 主要是构图,将每一行当成一个点,构成集合1,每一列也当成一个点,构成集合2,每一个障碍物的位置坐标将集合1和集合2的点连接起来,也就是将每一个障碍物作为连接节点的边,这样可以得出本题是一个最小点覆盖的问题==二分图的最大匹配. 就可以通过匈牙利算法求解.

POJ 3041 Asteroids(二分图 &amp;&amp; 匈牙利算法 &amp;&amp; 最小点覆盖)

嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边,而我们要做的就是要找尽量少的点把所有的边覆盖,即为最小点覆盖问题,根据König定理:最小覆盖点数=最大匹配数,所以就可以用匈牙利算法求最大匹配了. AC代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream&

POJ3041 Asteroids

Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20458   Accepted: 11098 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 a

匈牙利算法(二分图)

                                                                                                            ---------------------------------------------------------------------题材大多来自网络,本篇由神犇整理 基本概念—二分图 二分图:是图论中的一种特殊模型.若能将无向图G=(V,E)的顶点V划分为两个交集为空的顶点集,

匈牙利算法

(一)首先明确匈牙利算法是干嘛滴? 匈牙利算法是解决二部图最大匹配问题滴. (二)算法的核心思想:不断寻找增广路径,每找到一条增广路径,就通过异或操作使匹配边数加一,直到找不到增广路径,算法结束. (三)算法的基本步骤: (1)任取二部图G(X,Y)的匹配M,若M饱和X,则停止.若M不能饱和X,则取X的未标记的M非饱和点x.(标记的点表示经过此点不存在增广路)令S={x},T= ?.(T集合中的点表示N(S)中已经加入增广路的点)(当不存非饱和点或者所有非饱和点都被标记,算法结束) (2)若N(