[bzoj2502]清理雪道[上下界网络流]

bzoj状态里有两种,一种时间是个位数,一种是四位数,我就是四位数的那种,,,估计都是看了hzwer..

  1 #include <bits/stdc++.h>
  2 
  3 #define    INF    0x3f3f3f3f
  4 
  5 using namespace std;
  6 
  7 template<const int _n,const int _m>
  8 struct Edge
  9 {
 10     struct Edge_base { int    to,next,w; }e[_m]; int    cnt,p[_n];
 11     Edge() { clear(); }
 12     void insert(const int x,const int y,const int z)
 13     { e[++cnt].to=y; e[cnt].next=p[x]; e[cnt].w=z; p[x]=cnt; return ; }
 14     int    start(const int x) { return p[x]; }
 15     void    clear() { cnt=1;memset(p,0,sizeof(p)); }
 16     void    Link(const int x,const int y,const int z)
 17     { insert(x,y,z); insert(y,x,0); }
 18     Edge_base&    operator[](const int x) { return e[x]; }
 19 };
 20 
 21 Edge<31000,510000>e;
 22 int    n,cnt,Flow,level[31000];
 23 int    SS,SSS,TTT,cur[31000],In[31000];
 24 
 25 bool    Bfs(const int S)
 26 {
 27     int    i,t;
 28     queue<int>    Q;
 29     memset(level,0,sizeof(level));
 30     level[S]=1;
 31     Q.push(S);
 32     while(!Q.empty())
 33     {
 34         t=Q.front();Q.pop();
 35         for(i=e.start(t);i;i=e[i].next)
 36         {
 37             if(!level[e[i].to] && e[i].w)
 38             {
 39                 level[e[i].to]=level[t]+1;
 40                 Q.push(e[i].to);
 41             }
 42         }
 43     }
 44     return level[TTT];
 45 }
 46 
 47 int    Dfs(const int S,const int bk)
 48 {
 49     if(S==TTT)return bk;
 50     int    rest=bk;
 51     for(int &i=cur[S];i;i=e[i].next)
 52     {
 53         if(level[e[i].to]==level[S]+1 && e[i].w)
 54         {
 55             int flow=Dfs(e[i].to,min(e[i].w,rest));
 56             e[i].w-=flow;
 57             e[i^1].w+=flow;
 58             if((rest-=flow)<=0)break;
 59         }
 60     }
 61     if(bk==rest)level[S]=0;
 62     return bk-rest;
 63 }
 64 
 65 void    Dinic()
 66 {
 67     while(Bfs(SSS))
 68     {
 69         memcpy(cur,e.p,sizeof(cur));
 70         Flow+=Dfs(SSS,INF);
 71     }
 72     return ;
 73 }
 74 
 75 int main()
 76 {
 77     int    i,j,t,y;
 78 
 79     scanf("%d",&n);
 80     for(i=1;i<=n;++i)
 81     {
 82         scanf("%d",&t);
 83         for(j=1;j<=t;++j)
 84         {
 85             scanf("%d",&y);
 86             cnt++;
 87             e.Link(i,n+(cnt<<1)-1,INF);
 88             e.Link(n+(cnt<<1),y,INF);
 89         }
 90     }
 91 
 92     SS=n+cnt+cnt+1,SSS=SS+1,TTT=SSS+1;
 93 
 94     for(i=1;i<=cnt;++i)
 95         e.Link(n+i+i-1,n+i+i,INF),In[n+i+i-1]--,In[n+i+i]++;
 96 
 97     for(i=n+1;i<=n+cnt+cnt;++i)
 98         if(In[i]>0)e.Link(SSS,i,In[i]);
 99         else    e.Link(i,TTT,-In[i]);
100 
101     for(i=1;i<=n;++i)e.Link(SS,i,INF);
102 
103     i=0;
104     while(Flow!=cnt)
105     {
106         e.Link(SSS,SS,1);
107         Dinic();i++;
108     }
109 
110     printf("%d\n",i);
111 
112     return 0;
113 }

 

 

posted @ 2015-12-31 02:49  Gster  阅读(264)  评论(0编辑  收藏  举报