hdu 3046 Pleasant sheep and big big wolf 网络流

原图中相邻点两两连边,容量为1,源点连接狼的节点,容量为INF,羊的节点连接汇点,容量为INF,这样求最小割就可以了。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
#define maxn 50000
#define INF 100000
struct Edge
{
    int from, to, cap, flow;
};

int s, t;
vector<Edge> edges;    // 边数的两倍
vector<int> G[maxn];   // 邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[maxn];        // BFS使用
int d[maxn];           // 从起点到i的距离
int cur[maxn];         // 当前弧指针
int map[210][210];
int sum[210][210];
int min(int a,int b)
{
    if(a<b) return a;
    else return b;
}
void AddEdge(int from, int to, int cap)
{
    int len;
    Edge temp;
    temp.from=from;temp.to=to;temp.cap=cap;temp.flow=0;
    edges.push_back(temp);
    temp.from=to;temp.to=from;temp.cap=0;temp.flow=0;
    edges.push_back(temp);
    len = edges.size();
    G[from].push_back(len-2);
    G[to].push_back(len-1);
}
bool BFS()
{
    memset(vis, 0, sizeof(vis));
    queue<int> Q;
    Q.push(s);
    vis[s] = 1;
    d[s] = 0;
    while(!Q.empty())
    {
        int x = Q.front(); Q.pop();
        for(int i = 0; i < G[x].size(); i++)
        {
            Edge& e = edges[G[x][i]];
            if(!vis[e.to] && e.cap > e.flow)
            {
                vis[e.to] = 1;
                d[e.to] = d[x] + 1;
                Q.push(e.to);
            }
        }
    }
    return vis[t];
}
int DFS(int x, int a)
{
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int& i = cur[x]; i < G[x].size(); i++)
    {
        Edge& e = edges[G[x][i]];
        if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap-e.flow))) > 0)
        {
            e.flow += f;
            edges[G[x][i]^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0) break;
        }
    }
    return flow;
}
int Maxflow()
{
    int flow = 0;
    while(BFS())
    {
        memset(cur, 0, sizeof(cur));
        flow += DFS(s, INF);
    }
    return flow;
}
int main()
{
    int row,col;
    int i,j;
    int cas=0;
    while(scanf("%d%d",&row,&col)!=EOF)
    {
        cas++;
        int tot=0;
        memset(map,0,sizeof(0));
        memset(sum,0,sizeof(0));
        s=0;t=row*col+1;
        for(i=0;i<=t+10;i++) G[i].clear();
        for(i=1;i<=row;i++)
            for(j=1;j<=col;j++)
            {
                tot++;
                sum[i][j]=tot;
            }
        for(i=1;i<=row;i++)
            for(j=1;j<=col;j++)
            {
                scanf("%d",&map[i][j]);
                if(j+1<=col)
                {
                    AddEdge(sum[i][j],sum[i][j+1],1);
                    AddEdge(sum[i][j+1],sum[i][j],1);
                }
                if(i+1<=row)
                {
                    AddEdge(sum[i][j],sum[i+1][j],1);
                    AddEdge(sum[i+1][j],sum[i][j],1);
                }
                if(map[i][j]==1)
                    AddEdge(sum[i][j],t,INF);
                else if(map[i][j]==2)
                    AddEdge(s,sum[i][j],INF);
            }
        printf("Case %d:\n",cas);
        printf("%d\n",Maxflow());
    }
}


 

 

 

posted @ 2014-03-27 12:52  贝尔摩德  阅读(159)  评论(0编辑  收藏  举报