Pleasant sheep and big big wolf 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): 3316    Accepted Submission(s): 1360


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

解析:

  求至少需要多少边使狼不能抓住羊,边嘛,肯定想到最小割,但一想最小割是删除边,那么就转化为把所有边都连上,求最小割叭

数组开小了 竟然T了一次  emm。。。

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + 10, INF = 0x7fffffff;
int n, m, s, t;
int head[maxn], cur[maxn], d[maxn], vis[maxn], cnt;
int nex[maxn << 1];
struct node
{
    int u, v, c;
}Node[maxn << 1];


void add_(int u, int v, int c)
{
    Node[cnt].u = u;
    Node[cnt].v = v;
    Node[cnt].c = c;
    nex[cnt] = head[u];
    head[u] = cnt++;
}

void add(int u, int v, int c)
{
    add_(u, v, c);
    add_(v, u, 0);
}

bool bfs()
{
    queue<int> Q;
    mem(d, 0);
    d[s] = 1;
    Q.push(s);
    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();
        for(int i = head[u]; i != -1; i = nex[i])
        {
            int v = Node[i].v;
            if(!d[v] && Node[i].c > 0)
            {
                d[v] = d[u] + 1;
                Q.push(v);
                if(v == t) return 1;
            }
        }
    }
    return d[t] != 0;
}

int dfs(int u, int cap)
{
    int ret = 0;
    if(u == t || cap == 0)
        return cap;
    for(int &i = cur[u]; i != -1; i = nex[i])
    {
        int v = Node[i].v;
        if(d[v] == d[u] + 1 && Node[i].c > 0)
        {
            int V = dfs(v, min(cap, Node[i].c));
            Node[i].c -= V;
            Node[i ^ 1].c += V;
            ret += V;
            cap -= V;
            if(cap == 0) break;
        }
    }
    if(cap > 0) d[u] = -1;
    return ret;
}

int Dinic(int u)
{
    int ans = 0;
    while(bfs())
    {
        memcpy(cur, head, sizeof(head));
        ans += dfs(u, INF);
    }
    return ans;
}

int main()
{
    int tmp, kase = 0;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        mem(head, -1);
        cnt = 0;
        s = 0, t = 80002;
        rep(i, 0, n)
        {
            rap(j, 1, m)
            {
                rd(tmp);
                if (tmp == 1)
                    add(i * m + j, t, INF);
                else if (tmp == 2)
                    add(s, i * m + j, INF);
                if(i != n - 1 && j != m)
                    add(i * m + j, (i + 1) * m + j, 1), add(i * m + j, i * m + j + 1, 1), add((i + 1) * m + j, i * m + j, 1), add(i * m + j + 1, i * m + j, 1);
                else if(i != n - 1 && j == m)
                    add(i * m + j, (i + 1) * m + j, 1), add((i + 1) * m + j, i * m + j, 1);
                else if (i == n - 1 && j != m)
                    add(i * m + j, i * m + j + 1, 1), add(i * m + j + 1, i * m + j, 1);
            }
        }
        printf("Case %d:\n", ++kase);
        pd(Dinic(s));
    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9960712.html
今日推荐