3993: [SDOI2015]星际战争
Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 1036 Solved: 483
[ Submit][ Status][ Discuss]
Description
3333年,在银河系的某星球上,X军团和Y军团正在激烈地作战。在战斗的某一阶段,Y军团一共派遣了N个巨型机器人进攻X军团的阵地,其中第i个巨型机器人的装甲值为Ai。当一个巨型机器人的装甲值减少到0或者以下时,这个巨型机器人就被摧毁了。X军团有M个激光武器,其中第i个激光武器每秒可以削减一个巨型机器人Bi的装甲值。激光武器的攻击是连续的。这种激光武器非常奇怪,一个激光武器只能攻击一些特定的敌人。Y军团看到自己的巨型机器人被X军团一个一个消灭,他们急需下达更多的指令。为了这个目标,Y军团需要知道X军团最少需要用多长时间才能将Y军团的所有巨型机器人摧毁。但是他们不会计算这个问题,因此向你求助。
Input
第一行,两个整数,N、M。
第二行,N个整数,A1、A2…AN。
第三行,M个整数,B1、B2…BM。
接下来的M行,每行N个整数,这些整数均为0或者1。这部分中的第i行的第j个整数为0表示第i个激光武器不可以攻击第j个巨型机器人,为1表示第i个激光武器可以攻击第j个巨型机器人。
Output
一行,一个实数,表示X军团要摧毁Y军团的所有巨型机器人最少需要的时间。输出结果与标准答案的绝对误差不超过10-3即视为正确。
Sample Input
2 2
3 10
4 6
0 1
1 1
3 10
4 6
0 1
1 1
Sample Output
1.300000
HINT
【样例说明1】
战斗开始后的前0.5秒,激光武器1攻击2号巨型机器人,激光武器2攻击1号巨型机器人。1号巨型机器人被完全摧毁,2号巨型机器人还剩余8的装甲值;
接下来的0.8秒,激光武器1、2同时攻击2号巨型机器人。2号巨型机器人被完全摧毁。
对于全部的数据,1<=N, M<=50,1<=Ai<=105,1<=Bi<=1000,输入数据保证X军团一定能摧毁Y军团的所有巨型机器人
Source
题解:二分+最大流
二分一个可行的时间,判断是否满流。因为有小数,可以先整体扩大1000000
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define N 30000
#define LL long long
using namespace std;
const LL inf=1e12;
int n,m,tot,s,t;
int point[N],next[N],v[N],last[N];
int deep[N],cur[N],num[N],mp[53][53];
LL remain[N],a[N],b[N],sum;
void add(int x,int y,LL z)
{
tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
//cout<<x<<" "<<y<<" "<<z<<endl;
}
LL addflow(int s,int t)
{
int now=t; LL ans=inf;
while (now!=s) {
ans=min(ans,remain[last[now]]);
now=v[last[now]^1];
}
now=t;
while (now!=s) {
remain[last[now]]-=ans;
remain[last[now]^1]+=ans;
now=v[last[now]^1];
}
return ans;
}
void bfs(int s,int t)
{
for (int i=1;i<=t;i++) deep[i]=t;
deep[t]=0;
queue<int> p; p.push(t);
while (!p.empty()) {
int now=p.front(); p.pop();
for (int i=point[now];i!=-1;i=next[i])
if(deep[v[i]]==t&&remain[i^1])
deep[v[i]]=deep[now]+1,p.push(v[i]);
}
}
LL isap(int s,int t)
{
LL ans=0; int now=s;
bfs(s,t);
for (int i=1;i<=t;i++) num[deep[i]]++;
for (int i=1;i<=t;i++) cur[i]=point[i];
while (deep[now]<t) {
if (now==t) {
ans+=addflow(s,t);
now=s;
}
bool pd=false;
for (int i=cur[now];i!=-1;i=next[i])
if (deep[now]==deep[v[i]]+1&&remain[i]) {
last[v[i]]=i;
cur[now]=i;
pd=true;
now=v[i];
break;
}
if (!pd) {
int minn=t;
for (int i=point[now];i!=-1;i=next[i])
if (remain[i]) minn=min(minn,deep[v[i]]);
if (!--num[deep[now]]) break;
num[deep[now]=minn+1]++;
cur[now]=point[now];
if (now!=s) now=v[last[now]^1];
}
}
return ans;
}
bool pd(LL x)
{
tot=-1;
memset(point,-1,sizeof(point));
memset(num,0,sizeof(num));
s=1; t=n+m+2;
for (int i=1;i<=m;i++) {
add(s,i+1,b[i]*x);
for (int j=1;j<=n;j++)
if (mp[i][j]) add(i+1,j+m+1,inf);
}
for (int i=1;i<=n;i++) add(i+m+1,t,a[i]);
if (isap(s,t)==sum) return true;
else return false;
}
int main()
{
freopen("a.in","r",stdin);
freopen("my.out","w",stdout);
scanf("%d%d",&n,&m);
sum=0;
for (int i=1;i<=n;i++) scanf("%I64d",&a[i]),a[i]*=1000000,sum+=a[i];
for (int j=1;j<=m;j++) scanf("%I64d",&b[j]);
LL l=1; LL r=sum; LL ans=sum;
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++) scanf("%d",&mp[i][j]);
while (l<=r) {
LL mid=(l+r)/2;
if (pd(mid)) ans=min(ans,mid),r=mid-1;
else l=mid+1;
}
printf("%.6lf\n",ans/1000000.0);
}