UVA 12549 Sentry Robots

这道题挺像hdu 5093 Battle ships的,不过那道题是要求最多放置的点数,而这道题是要求最小点覆盖。

一个重要的位置有(x,y)两个坐标,而要守住这个这个位置就是相当于连了一条边x到y的边。

选了一个(x,y)就相当于选了所有相同的x的边或者所有相同的y的边。

当所有的x或y被选完的时候就完成了看守。相当于是割断了x和y,所以就是最小割,对于容量为1的模型可以用匈牙利算法。

有了障碍以后只要把障碍两边的分开考虑就行了,拆一下点。

数组开大点不要RE啦

#include<bits/stdc++.h>
using namespace std;

const int N = 5002;
const int maxn = 101;
char g[maxn][maxn];
int Yid[maxn][maxn],Y_cnt;
int match[N];
int dfsT[N],dfsTime;
vector<int> G[N];
#define PB push_back
bool dfs(int u)
{
    for(int i = 0; i < G[u].size(); i++){
        int v = G[u][i];
        if(dfsT[v] != dfsTime){
            dfsT[v] = dfsTime;
            if(!~match[v] || dfs(match[v])){
                match[v] = u; return true;
            }
        }
    }
    return false;
}

int MaxMatch()
{
    int ret = 0;
    memset(match,-1,sizeof(match));
    memset(dfsT,0,sizeof(dfsT));
    dfsTime = 0;
    for(int i = 0; i < Y_cnt; i++){
        dfsTime++;
        if(dfs(i)) ret++;
    }
    return ret;
}

int main()
{
   // freopen("in.txt","r",stdin);
    int C; scanf("%d",&C);
    while(C--){
        int Y, X, P; scanf("%d%d%d",&Y,&X,&P);
        memset(g,0,sizeof(g));

        while(P--) {
            int r,c;scanf("%d%d",&r,&c);
            g[r][c] = ‘*‘;
        }
        scanf("%d",&P);
        while(P--){
            int r,c;scanf("%d%d",&r,&c);
            g[r][c] = ‘#‘;
        }
        Y_cnt = 0;
        for(int i = 1; i <= Y;i++){
            bool flag = false;
            for(int j = 1; j <= X; j++){
                if(g[i][j] == ‘*‘) flag = true,Yid[i][j] = Y_cnt;
                else if(g[i][j] == ‘#‘ && flag) Y_cnt++;
            }
            if(flag) Y_cnt++;
        }
        for(int i = 0; i < Y_cnt; i++) G[i].clear();
        int X_cnt = 0;
        for(int j = 1; j <= X; j++){
            bool flag = false;
            for(int i = 1; i <= Y; i++){
                if(g[i][j] == ‘*‘) {
                    flag = true;
                    G[Yid[i][j]].PB(X_cnt);
                }else if(g[i][j] == ‘#‘&&flag){
                    X_cnt++;
                }
            }
            if(flag) X_cnt++;
        }
        printf("%d\n",MaxMatch());
    }
    return 0;
}
时间: 2024-10-25 05:55:00

UVA 12549 Sentry Robots的相关文章

12549 - Sentry Robots (二分图匹配)

该题和HDU 5093 如出一辙 传送门  .即求解二分图最大匹配数 = 最小点集覆盖 . 该题要求用尽量少的机器人看守所有重要的点,并且障碍物会阻隔机器人的看守范围  . 我们不妨将行列分开,按照行和列的最大看守范围编号,这样得到的就是最大匹配数 . 由于要求看守所有重要的点,所以这样可以有效去重,进行了最大匹配之后还可以保证一定看守了所有的点 . 建好图之后套Dinic模板就行了 . 该题有点逆着来的意思 ,请读者仔细品味 . 细节参见代码: #include<bits/stdc++.h>

uva 12549

12549 - Sentry Robots Time limit: 1.000 seconds We need to guard a set of points of interest using sentry robots that cannot move or turn. We can position a sentry at any position facing eithernorth, south, east or west. Once a sentry is settled, it

UVa12549 Sentry Robots (二分图最大匹配,最小点集覆盖)

题意:http://vjudge.net/problem/UVA-12549 分析: 一个重要位置有(x,y)两个坐标,而要守住这个重要位置就相当于连一条x到y的弧.选了一个重要位置(x,y)放置机器人相当于选了所有x相同的弧或者y相同的弧.当所有的x或者y被选完的时候就完成了看守.具体来说二分图两列分别表示用编号代表行和列,从源点s向行连一条容量为1的弧,从列向汇点t连一条容量为1的弧,再对于每个重要位置(x,y),连一条从x指向y容量为1的弧,跑一次s-t的最大流,最大流出现是当源点s连出去

Uva 10599 - Robots(II) (dp + 记录路径)

Problem K Robots(II) Time Limit 1 Second Your company provides robots that can be used to pick up litter from fields after sporting events and concerts. Before robots are assigned to a job, an aerial photograph of the field is marked with a grid. Eac

UVA 11795

B Mega Man's Missions Input Standard Input Output Standard Output Mega Man is off to save the world again. His objective is to kill the Robots created by Dr. Wily whose motive is to conquer the world. In each mission, he will try to destroy a particu

UVA - 11795 Mega Man&#39;s Mission

Mega Man is off to save the world again. His objective is to kill the Robots created by Dr. Wily whose motive is to conquer the world. In each mission, he will try to destroy a particular Robot. Initially, Mega Man is equipped with a weapon, called t

UVA 562 Dividing coins --01背包的变形

01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c[102],d

UVA 10341 Solve It

Problem F Solve It Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB Solve the equation: p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0 where 0 <= x <= 1. Input Input consists of multiple test cases and te

UVA 11014 - Make a Crystal(容斥原理)

UVA 11014 - Make a Crystal 题目链接 题意:给定一个NxNxN的正方体,求出最多能选几个整数点.使得随意两点PQ不会使PQO共线. 思路:利用容斥原理,设f(k)为点(x, y, z)三点都为k的倍数的点的个数(要扣掉一个原点O).那么全部点就是f(1),之后要去除掉共线的,就是扣掉f(2), f(3), f(5)..f(n).n为素数.由于这些素数中包括了合数的情况,而且这些点必定与f(1)除去这些点以外的点共线,所以扣掉.可是扣掉后会扣掉一些反复的.比方f(6)在f