[BZOJ2929][POI1999]洞穴攀行(网络流)

来源:互联网 发布:查看linux sftp软件 编辑:程序博客网 时间:2024/04/28 16:55

题目描述

传送门

题解

题意不明啊= =
1出发和到达n的边只能走一次,其余随意。
那么很裸的网络流。

代码

#include<iostream>#include<cstring>#include<cstdio>#include<queue>using namespace std;const int max_n=205;const int max_N=max_n+2;const int max_m=max_n*max_n;const int max_e=max_m*2;const int INF=1e9;int n,m,N,x;int tot,point[max_N],next[max_e],v[max_e],remain[max_e];int deep[max_N],num[max_N],last[max_N],cur[max_N];int maxflow;queue <int> q;inline void addedge(int x,int y,int cap){    ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;    ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;}inline void bfs(int t){    for (int i=1;i<=N;++i) deep[i]=N;    deep[t]=0;    for (int i=1;i<=N;++i) cur[i]=point[i];    while (!q.empty()) q.pop();    q.push(t);    while (!q.empty()){        int now=q.front(); q.pop();        for (int i=point[now];i!=-1;i=next[i])          if (deep[v[i]]==N&&remain[i^1]){            deep[v[i]]=deep[now]+1;            q.push(v[i]);          }    }}inline int addflow(int s,int t){    int now=t,ans=INF;    while (now!=s){        ans=min(ans,remain[last[now]]);        now=v[last[now]^1];    }    now=t;    while (now!=s){        remain[last[now]]-=ans;        remain[last[now]^1]+=ans;        now=v[last[now]^1];    }    return ans;}inline void isap(int s,int t){    bfs(t);    for (int i=1;i<=N;++i) ++num[deep[i]];    int now=s;    while (deep[s]<N){        if (now==t){            maxflow+=addflow(s,t);            now=s;        }        bool has_find=false;        for (int i=cur[now];i!=-1;i=next[i])          if (deep[v[i]]+1==deep[now]&&remain[i]){            has_find=true;            cur[now]=i;            last[v[i]]=i;            now=v[i];            break;          }        if (!has_find){            int minn=N-1;            for (int i=point[now];i!=-1;i=next[i])              if (remain[i]) minn=min(minn,deep[v[i]]);            if (!(--num[deep[now]])) break;            num[deep[now]=minn+1]++;            cur[now]=point[now];            if (now!=s) now=v[last[now]^1];        }    }}int main(){    tot=-1;    memset(point,-1,sizeof(point));    memset(next,-1,sizeof(next));    scanf("%d",&n);    N=n+2;    for (int i=1;i<n;++i){        scanf("%d",&m);        for (int j=1;j<=m;++j){            scanf("%d",&x);            if (i==1||x==n) addedge(1+i,1+x,1);            else addedge(1+i,1+x,INF);        }    }    addedge(1,2,INF);    addedge(N-1,N,INF);    isap(1,N);    printf("%d\n",maxflow);}
0 0
原创粉丝点击