登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Yavin(某沙茶的代码库)

Star Wars fan and OIer

 
 
 

日志

 
 

SGU212(HLPP 最高标号预流推进)  

2012-04-29 13:37:19|  分类: SGU200系列 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
分层图的网络流,自然用最高标号
用优先队列实现,加gap优化
跑出 421ms 也是很不错了
但我对程序的正确性仍有所怀疑
以后能用sap就用sap吧

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int NUM=1500+100;
const int MAX=300000+10;
const int INF=2147480000;

priority_queue< pair<int,int> > q;

struct Edge
{
int s,t,next;
int c,flow;
}e[MAX*2];
int res[NUM],h[NUM];
int begin[NUM],top;

int n,m,L;
int level[NUM],S,T;
int gap[MAX];
int num[MAX];

void add(int x,int y,int c)
{
num[top/2+1]=top;
e[top].s=x;e[top].t=y;e[top].c=c;
e[top].next=begin[x];begin[x]=top++;
e[top].s=y;e[top].t=x;e[top].c=0;
e[top].next=begin[y];begin[y]=top++;
}

int que[MAX],first,last,hash[MAX];

void SPFA()
{
memset(h,-1,sizeof h);
que[last++]=T;
h[T]=0;hash[T]=1;
int u,v,i;
while(first<last)
{
u=que[first++];
hash[u]=0;
for(i=begin[u];i!=-1;i=e[i].next)
{
if(!e[i].c)
{
v=e[i].t;
if(h[v]==-1||h[v]>h[u]+1)
{
h[v]=h[u]+1;
if(!hash[v])que[last++]=v;
hash[v]=1;
}
}
}
}
memset(hash,0,sizeof hash);
}

void push(int i)
{
int u=e[i].s,v=e[i].t;
int temp=min(e[i].c-e[i].flow,res[u]);
if(temp>0)
{
res[u]-=temp;
res[v]+=temp;
if(!hash[v])
{
q.push(make_pair(h[v],v));
hash[v]=1;
}
e[i].flow+=temp;
e[i^1].flow-=temp;
}
}

void pre(int first)
{
memset(gap,0,sizeof gap);
memset(res,0,sizeof res);
int i;
for(i=0;i<top;++i)
e[i].flow=0;
SPFA();
res[S]=first;
hash[S]=1;
q.push(make_pair(h[S],S));
for(i=1;i<=n;++i)
++gap[h[i]];
}

void push_relable(int u)
{
hash[u]=0;
int m,i;
while(res[u]&&h[u]<2*n+1)
{
m=h[u]-1;
for(i=begin[u];i!=-1;i=e[i].next)
if(m==h[e[i].t]&&e[i].flow<e[i].c)
{
push(i);
if(res[u]==0)
break;
}
if(!res[u])break;
m=INF;
for(i=begin[u];i!=-1;i=e[i].next)
{
if(e[i].flow<e[i].c)
{
m=min(h[e[i].t],m);
}
}
if(m>=2*n+1)break;
if(m+1!=h[u])
{
--gap[h[u]];
if(!gap[h[u]])
{
for(i=1;i<=n;++i)
if(h[i]>h[u]&&h[i]<2*n+1)
{
--gap[h[i]];
++gap[h[i]=2*n+1];
}
}
h[u]=m+1;
gap[h[u]]++;
}
}
}

void maxflow()
{
pre(INF);
pair<int,int> now;
while(!q.empty())
{
now=q.top();q.pop();
if(now.second!=T)push_relable(now.second);
}
pre(res[T]);
while(!q.empty())
{
now=q.top();q.pop();
if(now.second!=T)push_relable(now.second);
}
int i;
for(i=1;i<=m;++i)
printf("%d\n",e[num[i]].flow);
}

void init()
{
memset(begin,-1,sizeof begin);
int i,x,y,c;
scanf("%d %d %d",&n,&m,&L);
for(i=1;i<=n;++i)
{
scanf("%d",&level[i]);
if(level[i]==1)S=i;
if(level[i]==L)T=i;
}
for(i=1;i<=m;++i)
{
scanf("%d %d %d",&x,&y,&c);
add(x,y,c);
}
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
#endif
init();
maxflow();
}




  评论这张
 
阅读(2693)| 评论(2)

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018