BZOJ_3993_[SDOI2015] Interstellar War _ two points + network flow

BZOJ_3993_[SDOI2015] Interstellar War _ two points + network flow

Description

 In 3333, on a planet in the Milky Way, the X Corps and the Y Corps are fighting fiercely. At a certain stage of the battle, the Y Corps sent a total of N giant robots to attack the X Corps positions, of which the i-th giant robot had an armor value of Ai. When a giant robot's armor value is reduced to 0 or below, the giant robot is destroyed. X Corps has M laser weapons, of which the i-th laser weapon can reduce the armor value of a giant robot Bi every second. The attack of the laser weapon is continuous. This laser weapon is very strange, a laser weapon can only attack some specific enemies. The Y Corps saw their giant robots being wiped out by the X Corps one by one, and they urgently needed more orders. For this purpose, the Y Corps needs to know the minimum time it will take for the X Corps to destroy all the giant robots of the Y Corps. But they don't count the problem, so turn to you for help.

Input

The first line, two integers, N, M.

The second line, N integers, A1, A2...AN.
The third line, M integers, B1, B2...BM.
The next M lines, each line contains N integers, all of which are 0 or 1. In this part, the jth integer in the i-th row is 0, which means that the i-th laser weapon cannot attack the j-th giant robot, and 1 means that the i-th laser weapon can attack the j-th giant robot.

Output

 One line, a real number, represents the minimum time it would take for the X Corps to destroy all the giant robots of the Y Corps. The absolute error between the output result and the standard answer is no more than 10-3, which is regarded as correct.

Sample Input

2 2
3 10
4 6
0 1
1 1

Sample Output

1.300000

HINT

 【Example Description 1】

In the first 0.5 seconds after the battle starts, laser weapon 1 attacks giant robot No. 2, and laser weapon 2 attacks giant robot No. 1. Giant robot No. 1 is completely destroyed, and giant robot No. 2 still has 8 armor value;
In the next 0.8 seconds, the laser weapons 1 and 2 attack the giant robot No. 2 at the same time. Giant Robot 2 was completely destroyed.
For all the data, 1<=N, M<=50, 1<=Ai<=105, 1<=Bi<=1000, the input data guarantees that X Corps can destroy all the giant robots of Y Corps

 First, the binary answer is transformed into a decision problem.
Let the answer of dichotomy be x, S is connected to the edge of x*b[i] to each weapon, each weapon is connected to the edge of inf to the robot, and the robot is connected to the edge of a[i] to T.
Find the maximum flow and compare with sum{a[i]}.
Note that the flow requires floating point numbers.
 
Code:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
using namespace std;
typedef double f2;
#define N 150
#define M 300050
#define S (n+m+1)
#define T (n+m+2)
#define inf 10000000
f2 flow[M];
int head[N],to[M],nxt[M],cnt,n,m,a[N],b[N];
int dep[N],Q[N],l,r,sum,map[N][N];
f2 fabs(f2 x){return x>0?x:-x;}
inline void add(int u,int v,f2 w) {
    to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; flow[cnt]=w;
    to[++cnt]=u; nxt[cnt]=head[v]; head[v]=cnt; flow[cnt]=0;
}
bool bfs() {
    memset(dep,0,sizeof(dep));
    dep[S]=1;int i;l=r=0;Q[r++]=S;
    while(l<r) {
        int x=Q[l++];
        for(i=head[x];i;i=nxt[i]) if(!dep[to[i]]&&flow[i]) {
            dep[to[i]]=dep[x]+1; if(to[i]==T) return 1; Q[r++]=to[i];
        }
    }
    return 0;
}
f2 dfs(int x,f2 mf) {
    if(x==T) return mf;
    int i;
    f2 nf=0;
    for(i=head[x];i;i=nxt[i]) {
        if(dep[to[i]]==dep[x]+1&&flow[i]) {
            f2 tmp=dfs(to[i],min(mf-nf,flow[i]));
            nf+=tmp;
            flow[i]-=tmp;
            flow[i^1]+=tmp;
            if(nf==mf) break;
        }
    }
    dep[x]=0;
    return nf;
}
bool check(f2 x) {
    memset(head,0,sizeof(head)); cnt=1;
    int i,j;
    for(i=1;i<=n;i++) add(i,T,a[i]);
    for(i=1;i<=m;i++) add(S,i+n,b[i]*x);
    for(i=1;i<=m;i++) {
        for(j=1;j<=n;j++) if(map[i][j]) {
            add(i+n,j,inf);
        }
    }
    f2 years=0,f;
    while(bfs()) while(1) {
        f2 f=dfs(S,inf);
        if(fabs(f)<1e-6) break;
        ans+=f;
    }
    return fabs(ans-sum)<1e-6;
}
int main() {
    scanf("%d%d",&n,&m);
    int i,j;
    for(i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
    for(i=1;i<=m;i++) scanf("%d",&b[i]);
    for(i=1;i<=m;i++) {
        for(j=1;j<=n;j++) {
            scanf("%d",&map[i][j]);
        }
    }
    f2 L=0,R=1e5;
    for(i=1;i<=70;i++) {
        f2 mid=(L+R)/2;
        if(check(mid)) R=mid;
        else L=mid;
    }
    printf("%.4lf\n",L);
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325049321&siteId=291194637