zoukankan      html  css  js  c++  java
  • HDU3046 最大流(最小割)

    Pleasant sheep and big big wolf

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 19 Accepted Submission(s): 14
     
    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
     

    题意:

    n*m的场地中,1表示羊,2表示狼,0表示空地,问建最少的篱笆能把狼和羊分离开。

    代码:

    //篱笆的长度是1,我们假设把狼放在S集合,羊放在T集合,求S,T的最小割就是答案。
    //狼连接源点,羊连接汇点,相邻的各点之间连无向边/。最小割=最大流。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<cmath>
    using namespace std;
    const int maxn=40009,inf=0x7fffffff;
    struct edge{
        int from,to,cap,flow;
        edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
    };
    struct dinic{
        int n,m,s,t;
        vector<edge>edges;
        vector<int>g[maxn];
        bool vis[maxn];
        int d[maxn];
        int cur[maxn];
        void init(int n){
            this->n=n;
            for(int i=0;i<n;i++) g[i].clear();
            edges.clear();
        }
        void addedge(int from,int to,int cap){
            edges.push_back(edge(from,to,cap,0));
            edges.push_back(edge(to,from,0,0));//反向弧
            m=edges.size();
            g[from].push_back(m-2);
            g[to].push_back(m-1);
        }
        bool bfs(){
            memset(vis,0,sizeof(vis));
            queue<int>q;
            q.push(s);
            d[s]=0;
            vis[s]=1;
            while(!q.empty()){
                int x=q.front();q.pop();
                for(int i=0;i<(int)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<(int)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 s,int t){
            this->s=s;this->t=t;
            int flow=0;
            while(bfs()){
                memset(cur,0,sizeof(cur));
                flow+=dfs(s,inf);
            }
            return flow;
        }
    }dc;
    int main()
    {
        int n,m,cas=0,mp[205][205];
        while(scanf("%d%d",&n,&m)==2){
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++) scanf("%d",&mp[i][j]);
            int s=0,t=n*m+1;
            dc.init(n*m+2);
            for(int i=1;i<=n;i++)
                for(int j=1;j<=m;j++){
                    int nu=(i-1)*m+j;
                    if(mp[i][j]==1) dc.addedge(s,nu,inf);
                    if(mp[i][j]==2) dc.addedge(nu,t,inf);
                    if(i>1) dc.addedge(nu,nu-m,1);
                    if(i<n) dc.addedge(nu,nu+m,1);
                    if(j>1) dc.addedge(nu,nu-1,1);
                    if(j<m) dc.addedge(nu,nu+1,1);
                }
            printf("Case %d:
    %d
    ",++cas,dc.maxflow(s,t));
        }
        return 0;
    }
  • 相关阅读:
    lamp
    mysql多实例部署
    mysql进阶
    rsync
    mysql基础
    httpd
    ftp
    高级命令之awk
    NFS
    网络进阶管理
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6375062.html
Copyright © 2011-2022 走看看