[BZOJ2929][POI1999]洞穴攀行(网络流)
2016-04-06 16:48
337 查看
题目描述
传送门题解
题意不明啊= =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); }
相关文章推荐
- Linux多线程网络编程参考
- 添加网络打印机时,无法添加,服务里面没有Print Spooler
- Android使用HttpURLConnection访问接口
- 打开设置界面报错问题
- 笔试笔记3 HTTPS HTTP
- Exception in thread "http-bio-8080-exec-2" java.lang.OutOfMemoryError: PermGen space
- TCP和UDP 粘包 消息保护边界
- TCP与UDP在概念上的区别
- python 网络编程 openurl [errno 10060]
- HttpClient 学习笔记--源自技术
- 笔试笔记2 SNMP为应用层协议 UDP 计算机网络拓扑结构 静态路由 缺省路由 组播 VOIP
- 通过win7中cmd命令提示符telnet模拟http请求
- SpringMVC源码剖析(五)-消息转换器HttpMessageConverter
- 浅谈浏览器http的缓存机制
- Caffe学习系列(18): 绘制网络模型
- HTTP状态代码以及定义(深度好文,赶紧收藏)
- gem install 出现Errno::ECONNRESET: Connection reset by peer - SSL_connect (https://api.rubygems.org
- MDN开发者网络
- ios AFNetworking 3.0 报错 : *** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLSt
- 【bzoj 3275】Number(最小割)