poj 3308 网络流

由行向列建图,增加两个源汇点,源点向行连边,容量为该行的花费,列向汇点连边,容量为该列的代价,一个伞兵的位置为x,y,

则从x行向y列连一天容量为无穷大的边,最后求最大流(最小割的容量)即可

View Code
#include<cstdio>
#include<cstring>
#include<cmath>
const int MAX=100005;
const double INF=1000000000;
struct
{
int v,next;
double c;
}edge[1000000];
int E,head[MAX];
int gap[MAX],cur[MAX];
int pre[MAX],dis[MAX];
void add_edge(int s,int t,double c,double cc)
{
/*加边的时候同时加两条,
一条正的,一条反的,
一般情况下反的容量是0
*/
edge[E].v=t; edge[E].c=c;
edge[E].next=head[s];
head[s]=E++;
edge[E].v=s; edge[E].c=cc;
edge[E].next=head[t];
head[t]=E++;
}
double min(double a,double b){return (a==-1||b<a)?b:a;}
double SAP(int s,int t,int n)
{
memset(gap,0,sizeof(gap));
memset(dis,0,sizeof(dis));
int i;
for(i=0;i<n;i++)cur[i]=head[i];
int u=pre[s]=s,v;
double maxflow=0,aug=-1;
gap[0]=n;
while(dis[s]<n)
{
loop: for(i=cur[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].c>0&&dis[u]==dis[v]+1)
{
aug=min(aug,edge[i].c);
pre[v]=u;
cur[u]=i;
u=v;
if(u==t)
{
for(u=pre[u];v!=s;v=u,u=pre[u])
{
edge[cur[u]].c-=aug;
edge[cur[u]^1].c+=aug;
}
maxflow+=aug;
aug=-1;
}
goto loop;
}
}
int mindis=n;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(edge[i].c>0&&dis[v]<mindis)
{
cur[u]=i;
mindis=dis[v];
}
}
if((--gap[dis[u]])==0)break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
int main()
{

int t,m,n,L;
scanf("%d",&t);
while(t--)
{
memset(head,-1,sizeof(head));
E=0;
scanf("%d%d%d",&m,&n,&L);
int i,j;double co;
for(i=1;i<=m;i++)
{
scanf("%lf",&co);
add_edge(0,i,log(co),0);
}
for(i=1;i<=n;i++)
{
scanf("%lf",&co);
add_edge(m+i,m+n+1,log(co),0);
}
for(i=1;i<=L;i++)
{
int x,y;
scanf("%d%d",&x,&y);
add_edge(x,y+m,INF,0);
}
printf("%.4lf\n",exp(SAP(0,n+m+1,n+m+2)));
}
return 0;
}



posted @ 2012-02-27 15:50  Because Of You  Views(237)  Comments(0Edit  收藏  举报