poj 3020 二分圖路徑覆蓋問題

題意:

一個矩形中,有N個城市’*’,現在這n個城市都要覆蓋無線,若放置一個基站,那麼它至多可以覆蓋相鄰的兩個城市。

問至少放置多少個基站才能使得所有的城市都覆蓋無線?

有向圖的最小路徑覆蓋=頂點數-最大二分匹配

無向圖的最小路徑覆蓋=頂點數-最大二分匹配/2

證明見這兩篇博客:

https://blog.csdn.net/lyy289065406/article/details/6647040

https://blog.csdn.net/fouzhe/article/details/52039197

ac代碼:

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <cstdio>
#include <bitset>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int go[4][2]= {0,1,0,-1,1,0,-1,0};

const int maxn=401;
int g[maxn][maxn];
int linker[maxn];
bool used[maxn];
int un,vn;

struct node
{
    char type;
    int id;
} mp[maxn][maxn];

int dfs(int u)
{
    for(int v=1;v<=vn;v++)
    {
        if(g[u][v]&&!used[v])
        {
            used[v]=true;
            if(linker[v]==-1||dfs(linker[v]))
            {
                linker[v]=u;
                return true;
            }
        }
    }
    return false;
}

int hungary()
{
    int res=0;
    memset(linker,-1,sizeof(linker));
    for(int u=1;u<=un;u++)
    {
        memset(used,false,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        memset(mp,0,sizeof(mp));
        memset(g,0,sizeof(g));
        memset(linker,0,sizeof(linker));
        memset(used,0,sizeof(used));
        int h,w;
        cin>>h>>w;
        int next=0;
        for(int i=1; i<=h; i++)
        {
            for(int j=1; j<=w; j++)
            {
                cin>>mp[i][j].type;
                if(mp[i][j].type=='*')
                {
                    next++;
                    mp[i][j].id=next;
                }
                else
                {
                    mp[i][j].id=0;
                }
            }
        }
        for(int i=1; i<=h; i++)
        {
            for(int j=1; j<=w; j++)
            {
                for(int k=0; k<4; k++)
                {
                    int x=i+go[k][0];
                    int y=j+go[k][1];
                    //if(x<1||x>h||y<1||y>w) continue;
                    if(mp[x][y].type=='*')
                    {
                        int t1=mp[i][j].id;
                        int t2=mp[x][y].id;
                        //if(g[t1][t2]||g[t2][t1]) continue;
                        g[t1][t2]=1;
                    }
                    /*else
                    {
                        int t1=mp[i][j].id;
                        g[t1][t1]=1;
                    }*/
                }
            }
        }
        /*for(int i=1;i<=next;i++)
        {
            for(int j=1;j<=next;j++)
            {
                if(g[i][j])
                {
                    cout<<i<<' '<<j<<endl;
                }
            }
        }*/
        un=next;
        vn=next;
        printf("%d\n",next-hungary()/2);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章