解题报告 P4001 [ICPC-Beijing 2006]狼抓兔子
题目链接:P4001 [ICPC-Beijing 2006]狼抓兔子。
题目大意
给定
数据规模:
解法 1:网络最大流
略。
注意事项
-
我使用 HLPP 求最大流,时间复杂度
O(n^5) !!!注意 gap 优化中,被抬高的废弃点不要再把流量推回源点了。
既然这题不需要推回去,而明显 HLPP 的复杂度已经高得离谱了,所以能优化就优化一些吧。
实测推回去的话,最后一个点需要跑 14s;而不推回去的话,最大点跑 471ms。
AC 代码
评测记录。
#include<cstdio>
#include<cstring>
#include<queue>
#include<list>
namespace Qza{
#define MAXN 1000005
#define MAXM 6000010
#define INF 0x3f3f3f3f // 这里的无穷大不能太大,否则反向边加上一点容量就会变成负数。
int h[MAXN],f[MAXN],gap[MAXN];
int en[MAXN],to[MAXM],nex[MAXM],w[MAXM];
int id[1002][1002];
std::list<int> lst[MAXN];
std::vector<std::list<int>::iterator> it;
void add_edge(int x,int y,int v)
{
static int cnt=-1;
nex[++cnt]=en[x]; to[cnt]=y; en[x]=cnt; w[cnt]=v;
nex[++cnt]=en[y]; to[cnt]=x; en[y]=cnt; w[cnt]=v; // 因为是无向图。
}
int bfs(int s,int t)
{
register int u,v,hst=0;
std::queue<int> q;
for(h[t]=0,q.push(t);!q.empty();) {
u=q.front(); q.pop();
for(int p=en[u];p!=-1;p=nex[p])
if(w[p^1] && !h[v=to[p]] && v!=t)
hst=h[v]=h[u]+1,q.push(v);
}
return hst;
}
int HLPP(int s,int t,int n) // 废弃的点就不要往回推流了,实测非要推回去的话,最后一个点要跑14s。
{
if(s==t) return INF;
register int u,v,flow,minhv,nowh=0,hst=bfs(s,t);
if(!h[s]) return 0;
it.resize(n+2);
static std::vector<int> buc[MAXN];
h[s]=n;
for(int i=1;i<=n;++i)
if(h[i]) ++gap[h[i]],it[i]=lst[h[i]].insert(lst[h[i]].begin(),i);
else if(i!=t) h[i]=n<<1;
for(int p=en[s];p!=-1;p=nex[p])
if(v=to[p],flow=w[p]) {
f[s]-=flow; f[v]+=flow;
w[p]-=flow; w[p^1]+=flow;
if(f[v]==flow) {
buc[h[v]].push_back(v);
nowh=std::max(nowh,h[v]);
}
}
while(nowh) {
if(buc[nowh].empty()) {--nowh;continue;}
u=buc[nowh].back();
buc[nowh].pop_back();
minhv=n<<1;
for(int p=en[u];f[u]&&p!=-1;p=nex[p])
if(w[p]) {
if(h[v=to[p]]<h[u]) {
flow=std::min(f[u],w[p]);
f[u]-=flow; f[v]+=flow;
w[p]-=flow; w[p^1]+=flow;
if(f[v]==flow) buc[h[v]].push_back(v);
}
else minhv=std::min(minhv,h[v]);
}
if(!f[u]) continue;
if(h[u]<n && !(--gap[h[u]])) {
for(int i=h[u]+1;i<=hst;++i) {
for(std::list<int>::iterator p=lst[i].begin();p!=lst[i].end();++p)
h[*p]=n+1;
gap[i]=0;
lst[i].clear();
}
hst=h[u]-1;
lst[h[u]].clear();
h[u]=n+1;
//nowh=h[u]=n+1;
//buc[nowh].push_back(u);
}
else {
if(h[u]<n) it[u]=lst[h[u]].erase(it[u]);
nowh=h[u]=minhv+1;
if(h[u]<n) {
it[u]=lst[nowh].insert(lst[nowh].begin(),u);
++gap[nowh];
hst=std::max(hst,nowh);
buc[nowh].push_back(u);
}
}
}
return f[t];
}
int read()
{
char ch=getchar();int x=0;
while(ch<'0' || ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') x=(x*10)+(ch^48),ch=getchar();
return x;
}
#define id(x,y) id[x][y]
void main()
{
int n,m,s,t;
memset(en,0xff,sizeof(en));
n=read(); m=read();
s=n*m+1; t=s+1;
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
id(i,j)=(i-1)*m+j;
for(register int i=1;i<=n;++i)
for(register int j=1;j<m;++j)
add_edge(id(i,j),id(i,j+1),read());
for(register int i=1;i<n;++i)
for(register int j=1;j<=m;++j)
add_edge(id(i,j),id(i+1,j),read());
for(register int i=1;i<n;++i)
for(register int j=1;j<m;++j)
add_edge(id(i,j),id(i+1,j+1),read());
add_edge(s,id(1,1),INF);
add_edge(id(n,m),t,INF);
printf("%d",HLPP(s,t,t));
}
}
int main() {
Qza::main();
return 0;
}
解法 2:对偶图最短路
待填坑。。。