【BZOJ1822】[JSOI2010]冷冻波(二分,网络流)

时间:2022-06-21 10:38:22

【BZOJ1822】[JSOI2010]冷冻波(二分,网络流)

题面

BZOJ

洛谷

题解

先预处理每个巫妖可以打到哪些小精灵,然后二分答案,网络流判定即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
#define inf 1000000000
#define ll long long
#define MAXL 100000
#define MAX 500
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
struct Line{int v,next,w;}e[MAXL];
int h[MAX],cnt=2;
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};h[u]=cnt++;
e[cnt]=(Line){u,h[v],0};h[v]=cnt++;
}
int S,T;
int level[MAX];
bool bfs()
{
memset(level,0,sizeof(level));level[S]=1;
queue<int> Q;Q.push(S);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=h[u];i;i=e[i].next)
if(e[i].w&&!level[e[i].v])
level[e[i].v]=level[u]+1,Q.push(e[i].v);
}
return level[T];
}
int dfs(int u,int flow)
{
if(u==T||!flow)return flow;
int ret=0;
for(int i=h[u];i;i=e[i].next)
{
int v=e[i].v,d;
if(e[i].w&&level[v]==level[u]+1)
{
d=dfs(v,min(flow,e[i].w));
flow-=d;ret+=d;
e[i].w-=d;e[i^1].w+=d;
if(!flow)break;
}
}
if(!ret)level[u]=0;
return ret;
}
int Dinic()
{
int ret=0;
while(bfs())ret+=dfs(S,inf);
return ret;
}
struct Node{int x,y,r,t;}g[MAX],a[MAX],t[MAX];
ll Sqr(ll x){return x*x;}
ll SqrDis(Node a,Node b){return Sqr(a.x-b.x)+Sqr(a.y-b.y);}
bool match(Node a,Node b,Node c)
{
if(SqrDis(a,b)>=Sqr(a.r))return false;
ll A=b.y-a.y,B=a.x-b.x,C=b.x*a.y-a.x*b.y;
ll d=A*c.x+B*c.y+C,dd=Sqr(A)+Sqr(B);
if(c.r&&Sqr(d)<Sqr(c.r)*dd)return false;
return true;
}
bool G[MAX][MAX];
int n,m,K;
bool check(int mid)
{
memset(h,0,sizeof(h));cnt=2;S=0;T=n+m+1;
for(int i=1;i<=n;++i)Add(S,i,mid/g[i].t+1);
for(int i=1;i<=m;++i)Add(i+n,T,1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(G[i][j])Add(i,j+n,1);
return Dinic()>=m;
}
int main()
{
n=read();m=read();K=read();
for(int i=1;i<=n;++i)g[i].x=read(),g[i].y=read(),g[i].r=read(),g[i].t=read();
for(int i=1;i<=m;++i)a[i].x=read(),a[i].y=read();
for(int i=1;i<=K;++i)t[i].x=read(),t[i].y=read(),t[i].r=read();
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
bool fl=true;
for(int k=0;k<=K;++k)
if(!match(g[i],a[j],t[k])){fl=false;break;}
G[i][j]=fl;
}
for(int j=1;j<=m;++j)
{
bool fl=false;
for(int k=1;k<=n;++k)
if(G[k][j])fl=true;
if(!fl){puts("-1");return 0;}
}
int l=0,r=1e4,ret=1e9;
while(l<=r)
{
int mid=(l+r)>>1;
if(check(mid))r=mid-1,ret=mid;
else l=mid+1;
}
printf("%d\n",ret);
return 0;
}