ZJOI2009 狼和羊的故事

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   art   

既然這題這麼水,我就不寫了……

挖掘柵欄的本質:只能建在相鄰兩個,且建好後使得狼和羊之間不存在通路。而割的定義是:使S集和T集不存在通路。而題目又要求建的柵欄最少,於是就是最小割問題了。

從源點向所有狼連一條∞的邊,從所有羊向匯點連一條∞的邊,這樣就能保證狼和羊都在不同的點集裡。然後再從狼到相鄰的羊和空地,空地到相鄰的空地和羊連一條流量為1的邊,最大流求最小割即可。

或者將所有點向四周連邊。。就是時間長了點 --hzwer

代碼:(來自hzwer)

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define inf 0x7fffffff 5 #define T 10001 6 using namespace std; 7 int head[10005],q[10005],h[10005]; 8 int cnt=1,ans,n,m; 9 int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0},mp[105][105];10 struct data{int to,next,v;}e[500001];11 void ins(int u,int v,int w)12 {e[++cnt].to=v;e[cnt].next=head[u];e[cnt].v=w;head[u]=cnt;}13 void insert(int u,int v,int w)14 {ins(u,v,w);ins(v,u,0);}15 bool bfs()16 {17      int t=0,w=1,i,now;18      memset(h,-1,sizeof(h));19      q[0]=0;h[0]=0;20      while(t<w)21      {22                now=q[t];t++;i=head[now];23                while(i)24                {25                        if(e[i].v&&h[e[i].to]==-1)26                        {27                                                  h[e[i].to]=h[now]+1;28                                                  q[w++]=e[i].to;29                                                  }30                        i=e[i].next;31                        }32                }33     return h[T]==-1? 0:1;34  }35 int dfs(int x,int f)36 {37     if(x==T)return f;38     int w,used=0,i;39     i=head[x];40     while(i)41     {42             if(e[i].v&&h[e[i].to]==h[x]+1)43             {44                                           w=f-used;45                                           w=dfs(e[i].to,min(w,e[i].v));46                                           e[i].v-=w;47                                           e[i^1].v+=w;48                                           used+=w;49                                           if(used==f)return f;50                                           }51             i=e[i].next;52             }53     if(!used)h[x]=-1;54     return used;55 }56 void dinic(){while(bfs())ans+=dfs(0,inf);}57 void ini()58 {59      scanf("%d%d",&n,&m);60      for(int i=1;i<=n;i++)61          for(int j=1;j<=m;j++)62              scanf("%d",&mp[i][j]);63      }64 void build()65 {66      for(int i=1;i<=n;i++)67          for(int j=1;j<=m;j++)68          {69              if(mp[i][j]==1)insert(0,(i-1)*m+j,inf);70              else if(mp[i][j]==2)insert((i-1)*m+j,T,inf);71              for(int k=0;k<4;k++)72              {73                      int nowx=i+xx[k],nowy=j+yy[k];74                      if(nowx<1||nowx>n||nowy<1||nowy>m||mp[i][j]==2)continue;75                      if(mp[i][j]!=1||mp[nowx][nowy]!=1)76                      insert((i-1)*m+j,(nowx-1)*m+nowy,1);77                      }78              }79      }80 int main()81 {82     ini();83     build();84     dinic();85     printf("%d",ans);86     return 0;87     }
View Code

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.