您的位置:首页 > 其它

hdoj 3046 Pleasant sheep and big big wolf 【入门最小割】

2014-08-25 19:41 423 查看
题目:hdoj 3046 Pleasant sheep and big big wolf

题意:一个矩阵,1表示羊,2表示狼,然后让把羊和狼隔开,问需要最小的栅栏、

分析:标准的最小割

最小割:一个图中,删去最小容量和的边使得从 s 到 t 没有路径。

最小割 = 最大流

建图:

首先编号

相邻点建边 1

s 到 羊 无穷

t 到 狼 无穷

(这个题目数据比较水)

求最大流即可:

AC代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define Del(a,b) memset(a,b,sizeof(a))
const int N = 43020;
const int inf = 0x3f3f3f3f;
int n,m;
struct Node
{
    int from,to,cap,flow;
};
vector<int> v
;
vector<Node> e;
int vis
;  //构建层次图
int cur
;
void add_Node(int from,int to,int cap)
{
    e.push_back((Node)
    {
        from,to,cap,0
    });
    e.push_back((Node)
    {
        to,from,0,0
    });
    int tmp=e.size();
    v[from].push_back(tmp-2);
    v[to].push_back(tmp-1);
}
bool bfs(int s,int t)
{
    Del(vis,-1);
    queue<int> q;
    q.push(s);
    vis[s] = 0;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0; i<v[x].size(); i++)
        {
            Node tmp = e[v[x][i]];
            if(vis[tmp.to]<0 && tmp.cap>tmp.flow)  //第二个条件保证
            {
                vis[tmp.to]=vis[x]+1;
                q.push(tmp.to);
            }
        }
    }
    if(vis[t]>0)
        return true;
    return false;
}
int dfs(int o,int f,int t)
{
    if(o==t || f==0)  //优化
        return f;
    int a = 0,ans=0;
    for(int &i=cur[o]; i<v[o].size(); i++) //注意前面 ’&‘,很重要的优化
    {
        Node &tmp = e[v[o][i]];
        if(vis[tmp.to]==(vis[o]+1) && (a = dfs(tmp.to,min(f,tmp.cap-tmp.flow),t))>0)
        {
            tmp.flow+=a;
            e[v[o][i]^1].flow-=a; //存图方式
            ans+=a;
            f-=a;
            if(f==0)  //注意优化
                break;
        }
    }
    return ans;  //优化
}

int dinci(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        Del(cur,0);
        int tm=dfs(s,inf,t);
        ans+=tm;
    }
    return ans;
}
int mp[220][220];
int solve(int i,int j)
{
    return i*m+j;
}
int main()
{
    //freopen("Input.txt","r",stdin);
    int cas=1;
    while(~scanf("%d%d",&n,&m))
    {
        int sum=0;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
                scanf("%d",&mp[i][j]);
        }
        //printf("Yes\n");
        int s=n*m+1,t=s+1;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(i>0)
                    add_Node(solve(i,j),solve(i-1,j),1);
                if(j>0)
                    add_Node(solve(i,j),solve(i,j-1),1);
                if(i<(n-1))
                    add_Node(solve(i,j),solve(i+1,j),1);
                if(j<(m-1))
                    add_Node(solve(i,j),solve(i,j+1),1);
                if(mp[i][j]==2)
                    add_Node(s,solve(i,j),inf);
                else if(mp[i][j]==1)
                    add_Node(solve(i,j),t,inf);
            }
        }
        printf("Case %d:\n",cas++);
        printf("%d\n",dinci(s,t));
        for(int i=0; i<=t; i++)
            v[i].clear();
        e.clear();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: