题解:P13451 [GCJ 2009 Finals] Wi-fi Towers
题意
有
分析
从可以选择升级或不升级中,我们不难注意到这是一道最小割的题(这不废话吗)。又从得分有正负,且
最大权值闭合图问题
那么就有好学的同学要问了:什么是最大权值闭合图问题呀?最大权值闭合图问题就是字面意思呀。开玩笑的,最大权值闭合图问题就是:给你一张有向图,每个点都有一个权值(可以为正或负或
做法
众所周知,网络流首先要有源汇点。所以我们先建立一个超级源点
设节点
- 若
w_u > 0 ,添加边s \xrightarrow{w_u} u 。 - 若
w_u < 0 ,添加边u \xrightarrow{-w_u} t 。 - 原图中的有向边
u \to v 改为容量为\infty 的边。
最终答案为:
因为这题是多测,所以要记得清空(多测不清空,爆零两行泪)。
代码
如有不懂,见代码。
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int maxn=12e2+10;
constexpr int maxm=24e4+10;
constexpr int INF=LLONG_MAX>>1;
int n,e[maxn],c,d,m,tmp,h[maxn],k,T,ans,res,maxh,maxf,s,t,gap[maxn],cnt;
struct node
{
int to,w,bw;
node()=default;
node(int a,int b,int c):to(a),w(b),bw(c){}
};
vector<node>gra[maxn];
stack<int>b[maxn];
void add_flow(int u,int v,int w)
{
gra[u].emplace_back(v,w,gra[v].size());
gra[v].emplace_back(u,0,gra[u].size()-1);
}
bool bfs()
{
for(int i=1;i<=t+9;++i)
{
h[i]=INF;
}
queue<int>q;
q.emplace(t);
h[t]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
for(auto &g:gra[u])
{
int &v=g.to;
int &bw=g.bw;
if(h[v]>h[u]+1&&gra[v][bw].w)
{
h[v]=h[u]+1;
q.emplace(v);
}
}
}
return h[s]!=INF;
}
bool push(int u)
{
bool init=(u==s);
for(auto &g:gra[u])
{
int &w=g.w;
int &v=g.to;
int &bw=g.bw;
if(!w||(!init&&h[u]!=h[v]+1)||h[v]==INF)
{
continue;
}
int f=init?w:min(w,e[u]);
if(v!=s&&v!=t&&!e[v])
{
b[h[v]].emplace(v);
maxh=max(maxh,h[v]);
}
e[u]-=f;
e[v]+=f;
w-=f;
gra[v][bw].w+=f;
if(!e[u])
{
return 0;
}
}
return 1;
}
int get_maxh()
{
while(maxh>-1&&b[maxh].empty())
{
--maxh;
}
return maxh==-1?0:b[maxh].top();
}
void relabel(int u)
{
h[u]=INF;
for(auto &g:gra[u])
{
if(g.w)
{
h[u]=min(h[u],h[g.to]);
}
}
++h[u];
if(h[u]<t)
{
b[h[u]].emplace(u);
maxh=max(maxh,h[u]);
++gap[h[u]];
}
}
int hlpp()
{
if(!bfs())
{
return 0;
}
for(int i=1;i<=t+9;++i)
{
gap[i]=0;
}
for(int i=1;i<=t;++i)
{
if(h[i]!=INF)
{
++gap[h[i]];
}
}
h[s]=t;
push(s);
int u;
while(u=get_maxh())
{
b[maxh].pop();
if(push(u))
{
if(!--gap[h[u]])
{
for(int i=1;i<=n;++i)
{
if(i!=s&&h[i]>h[u]&&h[i]<n+1)
{
h[i]=n+1;
}
}
}
relabel(u);
}
}
return e[t];
}
int jl(int x1,int y1,int x2,int y2)
{
return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
}
bool check(int x1,int y1,int x2,int y2,int r)
{
return jl(x1,y1,x2,y2)<=r*r;
}
struct XHT
{
int x,y,r;
XHT()=default;
XHT(int a,int b,int c):x(a),y(b),r(c){}
};
XHT xht[maxm];
void init()
{
for(int i = 0; i <= t; ++i)
{
gra[i].clear();
while(!b[i].empty())
{
b[i].pop();
}
}
fill(h,h+t+2,0);
fill(gap,gap+t+2,0);
fill(e,e+t+2,0);
maxh=0;
cnt=0;
}
signed main()
{
scanf("%lld",&T);
for(int qwq=1;qwq<=T;++qwq)
{
scanf("%lld",&n);
s=0;
t=n+1;
init();
for(int i=1,w; i<=n; ++i)
{
scanf("%lld%lld%lld%lld",&xht[i].x,&xht[i].y,&xht[i].r,&w);
if(w>=0)
{
cnt+=w;
add_flow(s,i,w);
}
else
{
add_flow(i,t,-w);
}
}
for(int i=1; i<=n; ++i)
{
for(int j=1; j<=n; ++j)
{
if(i==j)
{
continue;
}
int x1=xht[i].x;
int y1=xht[i].y;
int x2=xht[j].x;
int y2=xht[j].y;
int r=xht[i].r;
if(check(x1,y1,x2,y2,r))
{
add_flow(i,j,INF);
}
}
}
maxf=hlpp();
printf("Case #%lld: %lld\n",qwq,cnt-maxf);
}
return 0;
}