码迷,mamicode.com
首页 > 其他好文 > 详细

Pleasant sheep and big big wolf (hdu 3046 最小割)

时间:2015-07-12 11:25:02      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:pleasant sheep and b   hdu 3046   最小割   

Pleasant sheep and big big wolf

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2371    Accepted Submission(s): 988


Problem Description
In ZJNU, there is a well-known prairie. And it attracts pleasant sheep and his companions to have a holiday. Big big wolf and his families know about this, and quietly hid in the big lawn. As ZJNU ACM/ICPC team, we have an obligation to protect pleasant sheep and his companions to free from being disturbed by big big wolf. We decided to build a number of unit fence whose length is 1. Any wolf and sheep can not cross the fence. Of course, one grid can only contain an animal.
Now, we ask to place the minimum fences to let pleasant sheep and his Companions to free from being disturbed by big big wolf and his companions.
技术分享

 

Input
There are many cases.
For every case:

N and M(N,M<=200)
then N*M matrix:
0 is empty, and 1 is pleasant sheep and his companions, 2 is big big wolf and his companions.
 

Output
For every case:

First line output “Case p:”, p is the p-th case;
The second line is the answer.
 

Sample Input
4 6 1 0 0 1 0 0 0 1 1 0 0 0 2 0 0 0 0 0 0 2 0 1 1 0
 

Sample Output
Case 1: 4
 

Source
 

Recommend
gaojie   |   We have carefully selected several similar problems for you:  1569 1565 3049 3045 1733 
 


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

思路:要把羊和狼隔开,就是去最小割把他们分开,每个格子和周围四个格子建流量为1的边,s向狼建INF的边,羊向t建INF的边。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

const int MAXM = 200010;
const int MAXN = 43000;

int n,m,N;
int mp[205][205];

struct Edge
{
    int to,next,cap,flow;
}edge[MAXM];

int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];

void init(int n)
{
    tol=0;N=n;
    memset(head,-1,sizeof(head));
}

//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
    edge[tol].to=v; edge[tol].cap=w; edge[tol].next=head[u];
    edge[tol].flow=0; head[u]=tol++;
    edge[tol].to=u; edge[tol].cap=rw; edge[tol].next=head[v];
    edge[tol].flow=0; head[v]=tol++;
}

//输入参数:起点,终点,点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    int u=start;
    pre[u]=-1;
    gap[0]=N;
    int ans=0;
    while (dep[start]<N)
    {
        if (u==end)
        {
            int Min=INF;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if (Min>edge[i].cap-edge[i].flow)
                    Min=edge[i].cap-edge[i].flow;
            for (int i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u=start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        int v;
        for (int i=cur[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if (edge[i].cap-edge[i].flow && dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if (flag)
        {
            u=v;
            continue;
        }
        int Min=N;
        for (int i=head[u];i!=-1;i=edge[i].next)
            if (edge[i].cap-edge[i].flow && dep[edge[i].to]<Min)
            {
                Min=dep[edge[i].to];
                cur[u]=i;
            }
        gap[dep[u]]--;
        if (!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if (u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
    int i,j,cas=0;
    while (~sff(n,m))
    {
        for (i=0;i<n;i++)
            for (j=0;j<m;j++)
                sf(mp[i][j]);
        init(n*m+2);
        int s=n*m,t=s+1;
        for (i=0;i<n;i++)
        {
            for (j=0;j<m;j++)
            {
                if (i>0)
                    addedge(i*m+j,(i-1)*m+j,1);
                if (j>0)
                    addedge(i*m+j,i*m+j-1,1);
                if (i<(n-1))
                    addedge(i*m+j,(i+1)*m+j,1);
                if (j<(m-1))
                    addedge(i*m+j,i*m+j+1,1);
                if (mp[i][j]==1)
                    addedge(i*m+j,t,INF);
                if (mp[i][j]==2)
                    addedge(s,i*m+j,INF);
            }
        }
        pf("Case %d:\n",++cas);
        pf("%d\n",sap(s,t,N));
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

Pleasant sheep and big big wolf (hdu 3046 最小割)

标签:pleasant sheep and b   hdu 3046   最小割   

原文地址:http://blog.csdn.net/u014422052/article/details/46848179

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!