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;
}
POJ3041 Asteroids(匈牙利算法)的更多相关文章
- poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配
/** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...
- Asteroids(匈牙利算法)
求最小点覆盖数,即最大匹配数,匈牙利算法. #include<stdio.h> #include<string.h> ][],vis[],linker[];//linker[] ...
- POJ 3041 Asteroids | 匈牙利算法模板
emmmmm 让你敲个匈牙利 #include<cstdio> #include<algorithm> #include<cstring> #define N 51 ...
- POJ 3041 Asteroids 匈牙利算法,最大流解法,行列为点 难度:1
http://poj.org/problem?id=3041 #include <cstdio> #include <cstring> #include <vector& ...
- POJ3041轰炸行星(匈牙利算法 最小覆盖点集)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25232 Accepted: 13625 Descr ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- POJ 3041 Asteroids(二分图 && 匈牙利算法 && 最小点覆盖)
嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...
- poj3020 建信号塔(匈牙利算法 最小覆盖边集)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10518 Accepted: 518 ...
随机推荐
- spring security认证
1 开发基于表单的认证 Spring security核心的功能 认证(你是谁?) 授权(你能干什么?) 攻击防护(防止伪造身份) spring security实现了默认的用户名+密码认证,默认用户 ...
- SpringMVC笔记:annotation注解式开发
一.配置式开发 在我们之前的学习中,springmvc使用配置式开发模式,需要在核心配置文件中springmvc.xml注册大量的bean来注入controller控制器,工作繁琐容易出错,下面我们学 ...
- WinForm实现Rabbitmq官网6个案例-RPC
获取源码 客户端代码: namespace RabbitMQDemo { public partial class RPC : Form { private readonly static RPC _ ...
- 实用的 Chrome Developers Tools
做前端的,都喜欢 chrome , 里面的开发工具也很实用,网上看到文章不错,部分摘来分享 ------------------------------- console.log,中包含一些格式化的指 ...
- [算法练习]Add Two Numbers
题目说明: You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- input标签添加上disable属性在ios端字体颜色不兼容的问题
input[disabled],input:disabled,input.disabled{ color: #3e3e3e; -webkit-text-fill-color: #3e3e3e; -we ...
- Java多线程学习笔记(一)
一 概述 一个进程只有一个至少会运行一个线程,Java中同样存在这样,在调用main方法的时候,线程又JVM所创建. package link.summer7c.test; public class ...
- TCP状态统计 - 脚本命令
一.netstat命令说明 netstat常见参数 -a (all)显示所有选项,默认不显示LISTEN相关 -t (tcp)仅显示tcp相关选项 -u (udp)仅显示udp相关选项 -n 拒绝显示 ...
- windows程序设置开机自动启动
//调用方法:设置开机启动 SetAutoRun(Process.GetCurrentProcess().ProcessName, true, Application.StartupPath + @& ...
- 使用RQShineLabel
使用RQShineLabel https://github.com/zipme/RQShineLabel 最终效果: 源码: // // RootViewController.m // UseText ...