二分+最大流 。
eps要设1e-7。。。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #define maxv 205 #define maxe 1000050 #define inf 1000000007 #define eps 1e-7 using namespace std; int n,m,a[maxv],b[maxv],g[maxv],nume=1,dis[maxv],map[maxv][maxv],s,t; struct edge { int v,nxt; double f; }e[maxe]; double sum=0,max_flow; queue <int> q; void addedge(int u,int v,double f) { e[++nume].v=v;e[nume].f=f;e[nume].nxt=g[u];g[u]=nume; e[++nume].v=u;e[nume].f=0;e[nume].nxt=g[v];g[v]=nume; } void build(double x) { s=0;t=n+m+1;memset(g,0,sizeof(g));nume=1; for (int i=1;i<=m;i++) addedge(s,i,(double)b[i]*x); for (int i=1;i<=n;i++) addedge(i+m,t,a[i]); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) if (map[i][j]) addedge(i,j+m,inf); } bool bfs() { for (int i=s;i<=t;i++) dis[i]=inf;dis[s]=0;q.push(s); while (!q.empty()) { int head=q.front();q.pop(); for (int i=g[head];i;i=e[i].nxt) { int v=e[i].v; if (fabs(e[i].f)>eps && dis[v]>dis[head]+1) { dis[v]=dis[head]+1; q.push(v); } } } return dis[t]!=inf; } double dinic(int x,double low) { if (x==t) return low; double ret=0.0; for (int i=g[x];i && (low>0.0);i=e[i].nxt) { int v=e[i].v; if (fabs(e[i].f)>eps && dis[v]==dis[x]+1) { double dd=dinic(v,min(low,e[i].f)); ret+=dd;low-=dd;e[i].f-=dd;e[i^1].f+=dd; } } if (fabs(ret)<eps) dis[x]=inf; return ret; } bool check(double t) { max_flow=0.0;build(t); while (bfs()) max_flow+=dinic(s,inf); return fabs(max_flow-sum)<eps; } double DC() { double l=0,r=inf,mid,ans; while (r-l>eps) { mid=(l+r)/2; if (check(mid)) ans=r=mid; else l=mid; } return ans; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) {scanf("%d",&a[i]);sum+=(double)a[i];} for (int i=1;i<=m;i++) scanf("%d",&b[i]); for (int i=1;i<=m;i++) for (int j=1;j<=n;j++) scanf("%d",&map[i][j]); printf("%.6lf\n",DC()); return 0; }
时间: 2024-11-06 02:36:45