zoukankan      html  css  js  c++  java
  • [BZOJ 1735] Muddy Fields

    [题目链接]

             https://www.lydsy.com/JudgeOnline/problem.php?id=1735

    [算法]

             二分图最小覆盖

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 1010
    
    struct edge
    {
            int to,nxt;
    } e[MAXN * MAXN];
    
    int i,j,n,m,ans,cntx,cnty,tot;
    char mp[MAXN][MAXN];
    int x[MAXN][MAXN],y[MAXN][MAXN];
    int match[MAXN],head[MAXN];
    bool visited[MAXN];
    
    inline void addedge(int u,int v)
    {
            tot++;
            e[tot] = (edge){v,head[u]};
            head[u] = tot;
    }
    inline bool hungary(int u)
    {
            int i,v;
            for (i = head[u]; i; i = e[i].nxt)
            {
                    v = e[i].to;
                    if (!visited[v])
                    {
                            visited[v] = true;
                            if (!match[v] || hungary(match[v]))
                            {
                                    match[v] = u;
                                    return true;
                            }
                    }
            }
            return false;
    }
    
    int main() 
    {
            
            scanf("%d%d",&n,&m);
            for (i = 1; i <= n; i++) scanf("%s",mp[i] + 1);
            for (i = 1; i <= n; i++)
            {
                    for (j = 1; j <= m; j++)
                    {
                            if (mp[i][j] == '*')
                            {
                                    x[i][j] = ++cntx;
                                    while (j < m && mp[i][j + 1] == '*')
                                    {
                                            j++;
                                            x[i][j] = cntx;        
                                    }    
                            }                
                    }
            }
            for (j = 1; j <= m; j++)
            {
                    for (i = 1; i <= n; i++)
                    {
                            if (mp[i][j] == '*')
                            {
                                    y[i][j] = ++cnty;
                                    while (i < n && mp[i + 1][j] == '*')
                                    {
                                            i++;
                                            y[i][j] = cnty;
                                    }
                            }
                    }
            }
            for (i = 1; i <= n; i++)
            {
                    for (j = 1; j <= m; j++)
                    {
                            if (mp[i][j] == '*')
                                    addedge(x[i][j],y[i][j]);        
                    } 
            } 
            for (i = 1; i <= cntx; i++)
            {
                    memset(visited,false,sizeof(visited));
                    if (hungary(i)) ans++;
            }
            printf("%d
    ",ans);
            
            return 0;
        
    }
  • 相关阅读:
    WinForm 窗体应用程序(初步)之一
    ADO.NET
    面向对象思想
    数据库原理
    HTML学习总结
    c# 学习心得(2)
    c# 学习心得(1)
    《大话数据结构》读书笔记(2)
    《大话数据结构》读书笔记(1)
    ASP.NET Core学习总结(3)
  • 原文地址:https://www.cnblogs.com/evenbao/p/9408170.html
Copyright © 2011-2022 走看看