zoukankan      html  css  js  c++  java
  • 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:
    ",cas);
            printf("%d
    ",Maxflow());
        }
    }
    


     

  • 相关阅读:
    初试 spring web mvc
    读取网络数据缓存在本地 流程图
    servlet 过滤器实现 请求转发(跳转);跨域转发请求;tomcat 环境下。
    C# .net基于Http实现web server(web服务)
    微信公众平台开发
    Linux目录结构及作用
    MySQL事件调度器event的使用
    MySQL触发器trigger的使用
    存储过程的查、改、删
    MySQL游标的简单实践
  • 原文地址:https://www.cnblogs.com/vermouth/p/3710151.html
Copyright © 2011-2022 走看看