zoukankan      html  css  js  c++  java
  • hdu 3046 Pleasant sheep and big big wolf 最小割

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3046

    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.

    题目描述:在一个单位方格边长为1的矩阵中藏着灰太狼和它的同伴,等待着喜羊羊和它的同伴,为了不让喜羊羊和同伴被抓住,我们可以在矩形草坪中设置单位长度为1的栅栏,求最短的栅栏长度。

    算法分析:最小割模型。新增源点和汇点分别为from和to,源点到喜羊羊和同伴连边,权值为inf,每只狼到汇点连边,权值为inf,然后每个方格到相邻格子连边,权值为1即可。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<cmath>
      6 #include<algorithm>
      7 #include<queue>
      8 #define inf 0x7fffffff
      9 using namespace std;
     10 const int maxn=200*200+10;
     11 int n,m;
     12 struct node
     13 {
     14     int u,flow;
     15     int next;
     16 }edge[maxn*4];
     17 int head[maxn],edgenum;
     18 int from,to;
     19 
     20 void add(int u,int v,int flow)
     21 {
     22     edge[edgenum].u=v ;edge[edgenum].flow=flow;
     23     edge[edgenum].next=head[u];
     24     head[u]=edgenum++;
     25 
     26     edge[edgenum].u=u ;edge[edgenum].flow=0;
     27     edge[edgenum].next=head[v];
     28     head[v]=edgenum++;
     29 }
     30 
     31 int d[maxn];
     32 int bfs()
     33 {
     34     memset(d,0,sizeof(d));
     35     d[from]=1;
     36     queue<int> Q;
     37     Q.push(from);
     38     while (!Q.empty())
     39     {
     40         int u=Q.front() ;Q.pop() ;
     41         for (int i=head[u] ;i!=-1 ;i=edge[i].next)
     42         {
     43             int v=edge[i].u;
     44             if (!d[v] && edge[i].flow>0)
     45             {
     46                 d[v]=d[u]+1;
     47                 Q.push(v);
     48                 if (v==to) return 1;
     49             }
     50         }
     51     }
     52     return 0;
     53 }
     54 
     55 int dfs(int u,int flow)
     56 {
     57     if (u==to || flow==0) return flow;
     58     int cap=flow;
     59     for (int i=head[u] ;i!=-1 ;i=edge[i].next)
     60     {
     61         int v=edge[i].u;
     62         if (d[v]==d[u]+1 && edge[i].flow>0)
     63         {
     64             int x=dfs(v,min(cap,edge[i].flow));
     65             cap -= x;
     66             edge[i].flow -= x;
     67             edge[i^1].flow += x;
     68             if (cap==0) return flow;
     69         }
     70     }
     71     return flow-cap;
     72 }
     73 int dinic()
     74 {
     75     int sum=0;
     76     while (bfs()) sum += dfs(from,inf);
     77     return sum;
     78 }
     79 int main()
     80 {
     81     int ncase=1;
     82     while (scanf("%d%d",&n,&m)!=EOF)
     83     {
     84         memset(head,-1,sizeof(head));
     85         edgenum=0;
     86         from=n*m+1;
     87         to=from+1;
     88         int a;
     89         for (int i=1 ;i<=n ;i++)
     90         {
     91             for (int j=1 ;j<=m ;j++)
     92             {
     93                 scanf("%d",&a);
     94                 if (a==1)
     95                 {
     96                     add(from,(i-1)*m+j,inf);
     97                 }
     98                 else if (a==2)
     99                 {
    100                     add((i-1)*m+j,to,inf);
    101                 }
    102                 if (i-1>=1) add((i-1)*m+j,(i-2)*m+j,1);
    103                 if (i+1<=n) add((i-1)*m+j,i*m+j,1);
    104                 if (j-1>=1) add((i-1)*m+j,(i-1)*m+j-1,1);
    105                 if (j+1<=m) add((i-1)*m+j,(i-1)*m+j+1,1);
    106             }
    107         }
    108         printf("Case %d:
    %d
    ",ncase++,dinic());
    109     }
    110     return 0;
    111 }
  • 相关阅读:
    基于Apache+php+mysql的许愿墙网站的搭建
    关于php留言本网站的搭建
    httpd服务的安装、配置
    linux下面桌面的安装
    时间同步ntp服务的安装与配置
    通过挂载系统光盘搭建本地yum仓库的方法
    linux系统root用户忘记密码的重置方法
    linux系统的初化始配置
    Linux下通过PXE服务器安装Linux系统
    Linux设置RSA密钥登录
  • 原文地址:https://www.cnblogs.com/huangxf/p/4301352.html
Copyright © 2011-2022 走看看