比赛 |
20120704 |
评测结果 |
C |
题目名称 |
危险游戏 |
最终得分 |
0 |
用户昵称 |
fuhao |
运行时间 |
0.000 s |
代码语言 |
Pascal |
内存使用 |
0.00 MiB |
提交时间 |
2012-07-04 11:54:57 |
显示代码纯文本
const much=100000; maxn=10001; maxt=100001;
var
tt,ans,j,i,n,t,f1,f2,kx,ky,q,x,y,z:longint;
fa,a,b:array[0..maxn] of longint;
f:array[0..maxt,1..3] of longint;
next,link,cost,pre:array[0..much*2] of longint;
v:array[0..maxn] of boolean;
found:boolean;
procedure change(var x,y:longint);
var t:longint;
begin t:=x; x:=y; y:=t; end;
procedure kp(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=f[(l+r) shr 1,3];
repeat
while f[i,3]<mid do inc(i);
while f[j,3]>mid do dec(j);
if i<=j then
begin
change(f[i,1],f[j,1]);
change(f[i,2],f[j,2]);
change(f[i,3],f[j,3]);
inc(i); dec(j);
end;
until i>j;
if i<r then kp(i,r);
if l<j then kp(l,j);
end;
function sf(k:longint):longint;
begin
if k<>fa[k] then sf:=sf(fa[k]) else sf:=k;
fa[k]:=sf;
end;
procedure insert(x,y,z,k:longint);
var tot:longint;
begin
inc(tt); tot:=tt+k;
next[tot]:=a[x]; a[x]:=tot;
pre[tot]:=0; pre[next[tot]]:=tot; link[tot]:=y;
cost[tot]:=z;
end;
procedure kruscal;
var i,j:longint;
begin
j:=0;
for i:=1 to t do
begin
f1:=sf(f[i,1]); f2:=sf(f[i,2]);
if f1<>f2 then
begin fa[f1]:=f2; ans:=ans+f[i,3]; inc(j);
insert(f[i,1],f[i,2],f[i,3],0);
insert(f[i,2],f[i,1],f[i,3],much);
end;
if j=n-1 then break;
end;
end;
procedure dfs(k,aim:longint);
var i:longint;
begin
v[k]:=true; inc(b[0]); b[b[0]]:=k;
i:=a[k];
while i<>0 do
begin
if not v[link[i]] then
begin
if link[i]=aim then found:=true;
if found then
begin
if cost[i]>cost[kx] then kx:=i;
exit;
end;
dfs(link[i],aim);
end;
i:=next[i];
if found then exit;
end;
if found then exit;
end;
begin
assign(input,'tubea.in'); reset(input);
assign(output,'tubea.out'); rewrite(output);
readln(n,t);
for i:=1 to t do readln(f[i,1],f[i,2],f[i,3]);
kp(1,t);
for i:=1 to n do fa[i]:=i;
kruscal;
writeln(ans);
read(q);
for i:=1 to q do
begin
read(kx,ky);
x:=f[kx,1]; y:=f[kx,2]; z:=ky;
found:=false; kx:=0; ky:=0;
for j:=1 to b[0] do v[b[j]]:=false;
dfs(x,y);
if kx<>0 then
begin
if kx<=n-1 then ky:=kx+much else ky:=kx-much;
pre[next[kx]]:=pre[kx]; next[pre[kx]]:=next[kx];
pre[next[ky]]:=pre[ky]; next[pre[ky]]:=next[ky];
ans:=ans-cost[kx]+z;
insert(x,y,z,0); insert(y,x,z,much);
end;
writeln(ans);
end;
close(input); close(output);
end.