• Oil Skimming HDU


    匈牙利

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    char mp[660][660];
    int g[660][660];
    int relate[660][660];
    int match[660];
    int vis[660];
    int k,n;
    int tmp;
    int dfs(int u){
        int i;
        for(i = 0; i < tmp; i++){
            if(g[u][i] && !vis[i]){
                vis[i] = 1;
                if(!match[i] || dfs(match[i])){
                    match[i] = u;
                    return 1;
                }
            }
        }
        return 0;
    }
     
    int hungry(){
        int i;
        int cnt = 0;
        memset(match,0,sizeof(match));
        for(i = 0; i < tmp; i++){
            memset(vis,0,sizeof(vis));
            if(dfs(i))
                cnt++;
        }
        return cnt;
    }
    int main(){
        cin >> k;
        int cas = 0;
        while(k--){
            cin >> n;
            getchar();
            memset(relate,0,sizeof(relate));
            memset(g,0,sizeof(g));
            int i,j;
            tmp = 0;
            for(i = 0; i < n; i++){
                cin >> mp[i];
                getchar();
                for(j = 0; j < n; j++){
                    if(mp[i][j] == '#')
                        relate[i][j] = tmp++;
                }
            }
            for(i = 0; i < n; i++){
                for(j = 0; j < n; j++){
                    if(mp[i][j] != '#') continue;
                    if(mp[i-1][j] == '#') g[relate[i][j]][relate[i-1][j]] = 1;
                    if(mp[i+1][j] == '#') g[relate[i][j]][relate[i+1][j]] = 1;
                    if(mp[i][j-1] == '#') g[relate[i][j]][relate[i][j-1]] = 1;
                    if(mp[i][j+1] == '#') g[relate[i][j]][relate[i][j+1]] = 1;
                }
            }
            int ans = hungry();
            printf("Case %d: %d
    ",++cas,ans/2);//除以2是因为每个点都作为起点看了一遍,即正反都看了一遍所以是真正答案的两倍
        }
        return 0;
    }
    

    HK

    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    typedef long long ll;
    const int N=600*600+10;
    const int M=600*600*2+10;
    const int INF=0x7f7f7f7f;
    const int DX[]={0,0,-1,1};
    const int DY[]={-1,1,0,0};
    int e[M],ne[M],h[M],idx;
    void addedge(int a,int b)
    {
    	e[idx]=b;
    	ne[idx]=h[a];
    	h[a]=idx++;
    }
    void init()
    {
        idx=0;
        memset(h,-1,sizeof(h));
    }
     
    int uN;
    int Mx[N],My[N];
    int dx[N],dy[N];
    int dis;
    bool used[N];
    bool SearchP()
    {
        queue<int> q;
        memset(dx,-1,sizeof(dx));
        memset(dy,-1,sizeof(dy));
        for(int i=1;i<=uN;i++)
            if(Mx[i]==-1)
            {
                q.push(i);
                dx[i]=0;
            }
        dis=INF;
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            if(dx[u]>dis) break;
            for(int i=h[u];i!=-1;i=ne[i])
            {
                int v=e[i];
                if(dy[v]==-1)
                {
                    dy[v]=dx[u]+1;
                    if(My[v]==-1) dis=dy[v];
                    else
                    {
                        dx[My[v]]=dy[v]+1;
                        q.push(My[v]);
                    }
                }
            }
        }
        return dis!=INF;
    }
    bool dfs(int u)
    {
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=e[i];
            if(!used[v]&&dy[v]==dx[u]+1)
            {
                used[v]=true;
                if(My[v]!=-1&&dy[v]==dis) continue;
                if(My[v]==-1||dfs(My[v]))
                {
                    My[v]=u;
                    Mx[u]=v;
                    return true;
                }
            }
        }
        return false;
    }
    int MaxMatch()
    {
        int res=0;
        memset(Mx,-1,sizeof(Mx));
        memset(My,-1,sizeof(My));
        while(SearchP())
        {
            memset(used,false,sizeof(used));
            for(int i=1;i<=uN;i++)
                if(Mx[i]==-1&&dfs(i)) res++;
        }
        return res;
    }
    char g[610][610];
    int id[610][610];
    int main()
    {
        int n,T,kase=0;
        scanf("%d",&T);
        while(T--)
        {
            init();
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
                scanf("%s",g[i]+1);
            memset(id,-1,sizeof(id));
            int cnt=1;
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(g[i][j]=='#'&&(i+j)%2==0)
                        id[i][j]=cnt++;
            uN=cnt-1;
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    if(g[i][j]=='#'&&(i+j)%2==1)
                    {
                        int t=cnt++;
                        for(int k=0;k<4;k++)
                        {
                            int ni=i+DX[k];
                            int nj=j+DY[k];
                            if(id[ni][nj]!=-1) addedge(id[ni][nj],t);
                        }
                    }
            printf("Case %d: %d
    ",++kase,MaxMatch());
        }
        return 0;
    }
    
    
  • 相关阅读:
    自定义文书思路
    传输
    Netty的组件和设计
    第一款Netty应用程序
    Netty异步和事件驱动
    初识MQTT
    TCP/IP协议分层模型
    uni原生插件的开发(安卓)
    uniapp离线打包记录
    日常问题处理:Linux通过设备名称如何查看硬盘SN
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12423139.html
Copyright © 2020-2023  润新知