2001: [Hnoi2010]City 城市建设 - BZOJ

时间:2023-03-09 00:04:42
2001: [Hnoi2010]City 城市建设 - BZOJ

Description
PS国是一个拥有诸多城市的大国,国王Louis为城市的交通建设可谓绞尽脑汁。Louis可以在某些城市之间修建道路,在不同的城市之间修建道路需要不同的花费。Louis希望建造最少的道路使得国内所有的城市连通。但是由于某些因素,城市之间修建道路需要的花费会随着时间而改变,Louis会不断得到某道路的修建代价改变的消息,他希望每得到一条消息后能立即知道使城市连通的最小花费总和, Louis决定求助于你来完成这个任务。
Input
文件第一行包含三个整数N,M,Q,分别表示城市的数目,可以修建的道路个数,及收到的消息个数。 接下来M行,第i+1行有三个用空格隔开的整数Xi,Yi,Zi(1≤Xi,Yi≤n, 0≤Zi≤50000000),表示在城市Xi与城市Yi之间修建道路的代价为Zi。接下来Q行,每行包含两个数k,d,表示输入的第k个道路的修建代价修改为d(即将Zk修改为d)。
Output
输出包含Q行,第i行输出得知前i条消息后使城市连通的最小花费总和。
Sample Input
5 5 3
1 2 1
2 3 2
3 4 3
4 5 4
5 1 5
1 6
1 1
5 3

Sample Output
14
10
9
HINT

【数据规模】
对于20%的数据, n≤1000,m≤6000,Q≤6000。
有20%的数据,n≤1000,m≤50000,Q≤8000,修改后的代价不会比之前的代价低。
对于100%的数据, n≤20000,m≤50000,Q≤50000。

在机房小伙伴z55250825的耐心讲解下,认真的把他的代码抄了一遍(卧槽,还抄错了一次,卧槽,抄错的地方竟然是并查集的地方)

论文61页就是这道题,可以看一下

反正我是这么想的,这个分治是这样的

slove(l,r)

先把这个区间生成树的一定会选的边求出,把不需要的边删除,看起来好人性化的样子

contraction就是把一定会选的边计算出来,reduction就是把不会选的边去掉

论文中证明了它的复杂度,做法也在里面了,我就不说什么了,反正我也不会说

代码基本上是抄z55250825的,因为不会写啊

 const
maxn=;
inf=maxlongint;
type
edge=record
u,v,w,k:longint;
end;
g=array[..maxn]of edge;
var
e:array[..]of g;
t,tt:g;
f,d,size,qx,qy,sum,aa,pos:array[..maxn]of longint;
ans:array[..maxn]of int64;
n,m,q:longint; function find(x:longint):longint;
begin
if f[x]=x then exit(x);
f[x]:=find(f[x]);
exit(f[x]);
end; procedure union(u,v:longint);
begin
if u=v then exit;
if size[u]<size[v] then
begin
f[u]:=v;
inc(size[v],size[u]);
end
else
begin
f[v]:=u;
inc(size[u],size[v]);
end;
end; procedure sort(l,r:longint;var a:g);
var
i,j,y:longint;
t:edge;
begin
i:=l;
j:=r;
y:=a[(l+r)>>].w;
repeat
while a[i].w<y do
inc(i);
while a[j].w>y do
dec(j);
if i<=j then
begin
t:=a[i];
a[i]:=a[j];
a[j]:=t;
inc(i);
dec(j);
end;
until i>j;
if i<r then sort(i,r,a);
if j>l then sort(l,j,a);
end; procedure contraction(var tot:longint;var cnt:int64);
var
i,tmp,u,v:longint;
begin
tmp:=;
for i:= to tot do
begin
f[t[i].u]:=t[i].u;
f[t[i].v]:=t[i].v;
size[t[i].u]:=;
size[t[i].v]:=;
end;
sort(,tot,t);
for i:= to tot do
begin
u:=find(t[i].u);
v:=find(t[i].v);
if u=v then continue;
union(u,v);
inc(tmp);
tt[tmp]:=t[i];
end;
for i:= to tmp do
begin
f[tt[i].u]:=tt[i].u;
f[tt[i].v]:=tt[i].v;
size[tt[i].u]:=;
size[tt[i].v]:=;
end;
for i:= to tmp do
if tt[i].w<>-inf then
begin
u:=find(tt[i].u);
v:=find(tt[i].v);
if u=v then continue;
union(u,v);
inc(cnt,tt[i].w);
end;
tmp:=;
for i:= to tot do
if find(t[i].u)<>find(t[i].v) then
begin
inc(tmp);
tt[tmp]:=t[i];
pos[t[i].k]:=tmp;
tt[tmp].u:=f[t[i].u];
tt[tmp].v:=f[t[i].v];
end;
for i:= to tmp do
t[i]:=tt[i];
tot:=tmp;
end; procedure reduction(var tot:longint);
var
i,tmp,u,v:longint;
begin
tmp:=;
for i:= to tot do
begin
f[t[i].u]:=t[i].u;
f[t[i].v]:=t[i].v;
size[t[i].u]:=;
size[t[i].v]:=;
end;
sort(,tot,t);
for i:= to tot do
if (find(t[i].u)<>find(t[i].v)) or (t[i].w=inf) then
begin
union(f[t[i].u],f[t[i].v]);
inc(tmp);
tt[tmp]:=t[i];
end;
for i:= to tmp do
t[i]:=tt[i];
tot:=tmp;
end; procedure slove(dep,l,r:longint;cnt:int64);
var
i,mid:longint;
begin
if l=r then aa[qx[l]]:=qy[l];
for i:= to sum[dep] do
with e[dep][i] do
w:=aa[k];
for i:= to sum[dep] do
begin
t[i]:=e[dep][i];
pos[t[i].k]:=i;
end;
if l=r then
begin
ans[l]:=cnt;
for i:= to sum[dep] do
begin
f[t[i].u]:=t[i].u;
f[t[i].v]:=t[i].v;
size[t[i].u]:=;
size[t[i].v]:=;
end;
sort(,sum[dep],t);
for i:= to sum[dep] do
if find(t[i].u)<>find(t[i].v) then
begin
union(f[t[i].u],f[t[i].v]);
inc(ans[l],t[i].w);
end;
exit;
end;
sum[dep+]:=sum[dep];
for i:=l to r do
t[pos[qx[i]]].w:=-inf;
contraction(sum[dep+],cnt);
for i:=l to r do
t[pos[qx[i]]].w:=inf;
reduction(sum[dep+]);
for i:= to sum[dep+] do
e[dep+][i]:=t[i];
mid:=(l+r)>>;
slove(dep+,l,mid,cnt);
slove(dep+,mid+,r,cnt);
end; procedure init;
var
i:longint;
begin
read(n,m,q);
sum[]:=m;
for i:= to m do
with e[][i] do
begin
read(u,v,w);
k:=i;
aa[i]:=w;
end;
for i:= to q do
read(qx[i],qy[i]);
end; procedure work;
var
i:longint;
begin
slove(,,q,);
for i:= to q do
writeln(ans[i]);
end; begin
init;
work;
end.