题意:给定一个 n * m 的矩阵,有一些格子有目标,每次可以消灭一行或者一列,问你最少要几次才能完成。

析:把 行看成 X,把列看成是 Y,每个目标都连一条线,那么就是一个二分图的最小覆盖数,这个答案就是二分图的最大匹配,在输出解的时候,就是从匈牙利树上,从X的未盖点出发,然后标记X和Y,最后X中未标记的和Y标记的就是答案。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 2000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct Edge{
int to, next;
};
Edge edge[maxn*maxn/4];
int head[maxn], cnt;
int match[maxn];
bool used[maxn];
bool visx[maxn], visy[maxn]; void addEdge(int u, int v){
edge[cnt].to = v;
edge[cnt].next = head[u];
head[u] = cnt++;
} bool dfs(int u){
used[u] = true;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to, w = match[v];
if(w < 0 || !used[w] && dfs(w)){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} void dfs1(int u){
visx[u] = 1;
for(int i = head[u]; ~i; i = edge[i].next){
int v = edge[i].to;
if(visy[v]) continue;
visy[v] = 1;
dfs1(match[v]);
}
} int main(){
int r, c;
while(scanf("%d %d %d", &r, &c, &m) == 3 && r+c+m){
ms(head, -1); cnt = 0;
for(int i = 0; i < m; ++i){
int u, v;
scanf("%d %d", &u, &v);
--u, --v;
addEdge(u, v + r);
}
n = r + c;
ms(match, -1);
int ans = 0;
for(int i = 0; i < n; ++i) if(match[i] < 0){
ms(used, 0); if(dfs(i)) ++ans;
}
printf("%d", ans);
ms(visx, 0); ms(visy, 0);
ms(used, 0);
for(int i = 0; i < r; ++i) if(match[i] != -1) used[i] = 1;
for(int i = 0; i < r; ++i) if(!used[i]) dfs1(i);
for(int i = 0; i < r; ++i) if(!visx[i]) printf(" r%d", i + 1);
for(int i = r; i < n; ++i) if(visy[i]) printf(" c%d", i + 1 - r);
printf("\n");
}
return 0;
}

  

UVa 11419 SAM I AM (最小覆盖数)的更多相关文章

  1. UVA 11419 SAM I AM (最小点覆盖,匈牙利算法)

    题意:给一个r*c的矩阵,某些格子中可能有一些怪物,可以在一行或一列防止一枚大炮,大炮会扫光整行/列的怪,问最少需要多少炮?输出炮的位置. 思路: 先每行和列都放一个炮,把炮当成点,把怪当成边,一边连 ...

  2. UVA 11419 SAM I AM(最大二分匹配&最小点覆盖:König定理)

    题意:在方格图上打小怪,每次可以清除一整行或一整列的小怪,问最少的步数是多少,又应该在哪些位置操作(对输出顺序没有要求). 分析:最小覆盖问题 这是一种在方格图上建立的模型:令S集表示“行”,T集表示 ...

  3. Uva - 11419 - SAM I AM

    题意:一个矩形——R*C的网格,在某些位置上有石头,在网格外开一炮可以打掉该行或者该列的石头,求打掉这些石头最少需要多少门大炮,位置分别设在哪行哪列(0<R<1001, 0 < C ...

  4. 训练指南 UVA - 11419(二分图最小覆盖数)

    layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...

  5. SAM I AM UVA - 11419 最小点集覆盖 要输出具体覆盖的行和列。

    /** 题目:SAM I AM UVA - 11419 链接:https://vjudge.net/problem/UVA-11419 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一 ...

  6. POJ 3041 Asteroids 最小覆盖数

    http://poj.org/problem?id=3041 题目大意: 一辆宇宙飞船在一个小行星带中,你知道,这很危险.他有一种武器,可以清除掉一行或一列的小行星.问把小行星全部清除最少的武器使用次 ...

  7. UVa 11419 我是SAM(最小点覆盖+路径输出)

    https://vjudge.net/problem/UVA-11419 题意:一个网格里面有一些目标,可以从某一行,某一列发射一发子弹,可以打掉它:求最少的子弹,和在哪里打? 思路: 每个点的x坐标 ...

  8. SAM I AM UVA - 11419(最小顶点覆盖+输出一组解)

    就是棋盘问题输出一组解 https://blog.csdn.net/llx523113241/article/details/47759745 http://www.matrix67.com/blog ...

  9. Uva 11419 我是SAM

    题目链接:https://vjudge.net/problem/UVA-11419 题意:一个网格里面有一些目标,可以从某一行,某一列发射一发子弹,可以打穿: 求最少的子弹,和在哪里打? 分析: 听说 ...

随机推荐

  1. 【转】UBUNTU 下GIT的安装

    原文网址:http://www.cnblogs.com/perseus/archive/2012/01/06/2314069.html linux下软件的安装方式有多种,最简单的莫过于从软件中心直接安 ...

  2. 移植SDL2.2问题及解决方法

    项目需要ffmpeg+SDL播放视频,所以不得不移植SDL 根据 <移植SDL最新版本>http://blog.csdn.net/flyyang123456789/article/deta ...

  3. LogHelp 日记分天记录,只记30天日记

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  4. 机器学习The Learning Problem——coursera简要总结

    1.人类及动物的学习模式:观察->学习->技能 机器学习的模式:data->ML(机器学习)->skill 2.那什么是skill:技能是某种表现方法的增进   eg:stac ...

  5. vmware克隆linux网络配置

    一.配置Linux网络 在安装Linux的时候,一定要保证你的物理网络的IP是手动设置的,要不然会在Linux设置IP连通网络的时候会报network is unreachable 并且怎么也找不到问 ...

  6. Azkaban 使用问题及解决

    什么是Azkaban Azkaban是一款基于Java编写的任务调度系统 任务调度:有四个任务脚A.B.C.D,其中任务A与任务B可以并行运行,然后任务C依赖任务A和任务B的运行结果,任务D依赖任务C ...

  7. windows mysql默认配置文件

    查询配置目录 select @@basedir; 查询数据目录 select @@datadir; 查询数据库编码 show variables like 'char% my.ini [mysql] ...

  8. jmeter 目录内容分布

    /bin 目录(常用文件介绍) examples:目录下包含Jmeter使用实例 ApacheJMeter.jar:JMeter源码包 jmeter.bat:windows下启动文件 jmeter.s ...

  9. C++ 构造函数_初始化列表

    构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式.例如: class Student { public: //构造函数初始化列表 Stude ...

  10. PAT 甲级 1011 World Cup Betting (20)(20 分)(水题,不用特别在乎精度)

    1011 World Cup Betting (20)(20 分) With the 2010 FIFA World Cup running, football fans the world over ...