码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj3993[SDOI2015]星际战争

时间:2015-09-23 19:05:08      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

http://www.lydsy.com/JudgeOnline/problem.php?id=3993

裸题。

二分后强行网络流。

技术分享
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj
 
using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-5)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
  {
        int res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!=- && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z==-){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-0,z=getchar());
        return (neg)?-res:res; 
    }
LL gll()
  {
      LL res=0;bool neg=0;char z;
        for(z=getchar();z!=EOF && z!=- && !isdigit(z);z=getchar());
        if(z==EOF)return 0;
        if(z==-){neg=1;z=getchar();}
        for(;z!=EOF && isdigit(z);res=res*10+z-0,z=getchar());
        return (neg)?-res:res; 
    }

const int maxn=50;
const DB INF=1e50;

int n,m;
int a[maxn+10],b[maxn+10];
int mp[maxn+10][maxn+10];
DB sum;

int S,T,now,info[maxn+maxn+10],last[maxn+maxn+10];
struct Tedge{int v,next;DB flow;}edge[2*(maxn+maxn+maxn*maxn)+10000];

void addedge(int u,int v,DB flow)
  {
      now++;edge[now].v=v;edge[now].next=info[u];edge[now].flow=flow;info[u]=now;
      now++;edge[now].v=u;edge[now].next=info[v];edge[now].flow=0;info[v]=now;
  }

void build(DB mid)
  {
      int i,j;
      now=-1;mmst(info,-1);
      S=0;T=m+n+1;
      re(i,1,m)addedge(S,i,mid*DB(b[i]));
      re(i,1,n)addedge(m+i,T,DB(a[i]));
      re(i,1,m)re(j,1,n)if(mp[i][j])addedge(i,m+j,INF);
  }

int level[maxn+maxn+10];
int head,tail,que[maxn+maxn+10];
int Dinic_build()
  {
      int i;
      re(i,0,n+m+1)level[i]=0;
      level[que[head=tail=1]=S]=1;
      while(head<=tail)
        {
            int u=que[head++],v;DB flow;
            for(i=info[u],v=edge[i].v,flow=edge[i].flow;i!=-1;i=edge[i].next,v=edge[i].v,flow=edge[i].flow)
              if(!level[v] && sgn(flow)>0)
                      level[que[++tail]=v]=level[u]+1;
        }
      return level[T];
  }
DB Dinic(int u,DB delta)
  {
      if(u==T)return delta;
      int &i=last[u],v;DB res=0.0,flow;
      for(v=edge[i].v,flow=edge[i].flow;i!=-1;i=edge[i].next,v=edge[i].v,flow=edge[i].flow)
        if(level[u]+1==level[v] && sgn(flow)>0)
          {
              DB tmp=Dinic(v,min(flow,delta));
              delta-=tmp;
              res+=tmp;
              edge[i].flow-=tmp;
              edge[i^1].flow+=tmp;
              if(sgn(delta)<=0)return res;
          }
      return res;
  }

int check(DB mid)
  {
      build(mid);
      int i;DB maxflow=0.0;
      while(Dinic_build())
        {
            re(i,0,m+n+1)last[i]=info[i];
            maxflow+=Dinic(S,INF);
        }
      return sgn(maxflow-sum)>=0;
  }

int main()
  {
      freopen("war.in","r",stdin);
        freopen("war.out","w",stdout);
        int i,j;
        n=gint();m=gint();
        re(i,1,n)a[i]=gint();
        re(i,1,m)b[i]=gint();
        re(i,1,m)re(j,1,n)mp[i][j]=gint();
        re(i,1,n)sum+=DB(a[i]);
        int tmp=check(10.0);
        DB l=0.0,r=10000000.0;
        while(l+1e-5<r)
          {
              DB mid=(l+r)/2.0;
                if(check(mid))
                  r=mid;
                else
                  l=mid;
          }
        DB ans=(l+r)/2.0;
        if(sgn(ans)==0)ans=0.0;
        PF("%0.6lf\n",ans);
        return 0;
  }
View Code

 

bzoj3993[SDOI2015]星际战争

标签:

原文地址:http://www.cnblogs.com/maijing/p/4832659.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!