| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
sap在此题也不太高效,贴一下很挫的代码#include<iostream>
#include<climits>
#include<cstring>
const int maxn=400,maxm=400;
const int int_max=10000001;
using namespace std;
int dist[maxn+1],count_dist[maxn+1],his[maxn+1],pre[maxn+6];
int top;
int n,m;
struct node
{
int data,weight;
node *next,*anti;
}*retrack[maxn+1],*current[maxn+1],*path[maxn+1],data[maxn*2+5];
node*add_edge(int a,int b,int w)
{
node*p=&data[++top];
p->data=b;
p->weight=w;
p->next=retrack[a];
retrack[a]=p;
return p;
}
void ins_edge(int a,int b,int w)
{
node*p1=add_edge(a,b,w),*p2=add_edge(b,a,0);
p1->anti=p2;
p2->anti=p1;
}
int max_flow(int s,int t)
{
int i,now_flow,total,min_dist;
node *p,*locate;
bool flag;
memset(count_dist,0,sizeof(count_dist));
memset(dist,0,sizeof(dist));
count_dist[0]=m;
for(i=1;i<=m;i++)
current[i]=retrack[i];
for(total=0,now_flow=int_max,i=s;dist[s]<m;)
{
his[i]=now_flow;
for(flag=false,p=current[i];p!=NULL;p=p->next)
if((p->weight>0)&&(dist[i]==dist[p->data]+1))
{
if(p->weight<now_flow) now_flow=p->weight;
pre[p->data]=i;
path[p->data]=p;
current[i]=p;
i=p->data;
if(t==i)
{
for(total+=now_flow;i!=s;i=pre[i])
{
path[i]->weight-=now_flow;
path[i]->anti->weight+=now_flow;
}
now_flow=int_max;
}
flag=true;break;
}
if(!flag)
{
for(min_dist=m-1,p=retrack[i];p!=NULL;p=p->next)
if((p->weight>0)&&(dist[p->data]<min_dist))
{
min_dist=dist[p->data];
locate=p;
}
/*if(p==NULL)
{
min_dist=m-1;
}*/
current[i]=locate;
count_dist[dist[i]]--;
if(count_dist[dist[i]]==0)break;
dist[i]=min_dist+1;
count_dist[dist[i]]++;
if(i!=s)
{
i=pre[i];
now_flow=his[i];
}
}
}
return total;
}
int main()
{
int i,si,ei,ci;
while(~scanf("%d%d",&n,&m))
{
top=0;
for(i=1;i<=200;i++)
retrack[i]=NULL;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&si,&ei,&ci);
ins_edge(si,ei,ci);
}
printf("%d\n",max_flow(1,m));
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator