bzoj3993: [SDOI2015]星际战争(二分+最大流)

本文介绍了一种结合二分查找与最大流算法解决激光武器攻击装甲集群问题的方法。通过二分查找确定最小攻击时间,并使用最大流算法验证在该时间内能否达成足够的伤害值来摧毁所有目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目传送门

解法:
题目要求最短时间,那么二分答案咯。
然后用最大流来判断就好。

st向每台激光连时间*Bi表示他的伤害。
然后其实我们并不需要知道他怎么安排攻击。
这个交给最大流来做。
我们需要求的是激光按照最优的分配能不能总伤害达到消灭全部装甲所需要的伤害。
那么每台装甲向ed连Ai表示消灭这个装甲只需要Ai点伤害。
注意精度

代码实现:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {
    int x,y,next,other;double c;
}a[11000];int len,last[110];
void ins(int x,int y,double c) {
    int k1,k2;
    len++;k1=len;a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
    len++;k2=len;a[len].x=y;a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
int st,ed,head,tail,list[110],h[110];
bool bt_h() {
    head=1;tail=2;list[1]=st;memset(h,0,sizeof(h));h[st]=1;
    while(head!=tail) {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next) {
            int y=a[k].y;
            if(h[y]==0&&a[k].c>0) {h[y]=h[x]+1;list[tail++]=y;}
        }head++;
    }if(h[ed]==0)return false;return true;
}
double find_flow(int x,double f) {
    if(x==ed)return f;
    double s=0,t;
    for(int k=last[x];k;k=a[k].next) {
        int y=a[k].y;
        if(h[y]==h[x]+1&&a[k].c>0&&s<f) {
            t=find_flow(y,min(a[k].c,f-s));s+=t;a[k].c-=t;a[a[k].other].c+=t;
        }
    }if(s==0)h[x]=0;return s;
}
const double esp=1e-8;
int A[110],B[110],map[61][61];
int main() {
    int n,m;scanf("%d%d",&m,&n);double sum=0;
    for(int i=1;i<=m;i++){scanf("%d",&A[i]);sum+=double(A[i]);}
    for(int i=1;i<=n;i++)scanf("%d",&B[i]);
    for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&map[i][j]);
    double l=0.0,r=10000.0,ans,mid;st=n+m+1;ed=st+1;
    while(l<=r) {
        mid=(l+r)/2.0;len=0;memset(last,0,sizeof(last));
        for(int i=1;i<=n;i++)ins(st,i,double(B[i]*mid));
        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(map[i][j]==1)ins(i,j+n,999999999);
        for(int i=1;i<=m;i++)ins(i+n,ed,double(A[i]));
        double s=0;while(bt_h()==true)s+=find_flow(st,999999999);
        if(fabs(s-sum)<esp){ans=mid;r=mid-0.00000001;}else l=mid+0.00000001;
    }printf("%.6lf\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值