比赛 20120704 评测结果 EEEEEEEWWT
题目名称 危险游戏 最终得分 0
用户昵称 isabella 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-04 11:55:03
显示代码纯文本
const
 maxn=2000000000;
var
 n,m,i,j,k,a,b,c,ans,q,xx,yy,mid,temp:longint;
 map:array[1..100,1..100]of longint;
 fa,x,y,w:array[0..100000]of longint;
 v:array[1..100]of longint;

 procedure dfs(dn,k,now:longint);
  var i:longint;
  begin
   if now>=ans then exit;
   if dn=n then begin ans:=now;exit;end;

   inc(v[k]);
   for i:=1 to n do
    if map[k,i]<maxn then
     begin
      if v[i]=0 then dfs(dn+1,i,now+map[k,i]) else dfs(dn,i,now+map[k,i]);
     end;
   dec(v[k]);
  end;

 procedure sort(l,r:longint);
 var i,j:longint;
 begin
  i:=l;j:=r;mid:=w[(l+r)div 2];
  repeat
   while w[i]<mid do inc(i);
   while w[j]>mid do dec(j);
   if i<=j then
    begin temp:=w[i];w[i]:=w[j];w[j]:=temp;
          temp:=y[i];y[i]:=y[j];y[j]:=temp;
          temp:=x[i];x[i]:=x[j];x[j]:=temp;
          inc(i);dec(j);end;
  until i>j;
  if i<r then sort(i,r);
  if l<j then sort(l,j);
 end;

 function find(x:longint):longint;
  begin
   if fa[x]=x then exit(x);
   fa[x]:=find(fa[x]);
   exit(fa[x]);
  end;

 procedure hebing(x,y:longint);
  var i,j:longint;
  begin
   i:=find(x);
   j:=find(y);
   fa[i]:=j;
  end;

begin
assign(input,'tubea.in');reset(input);
assign(output,'tubea.out');rewrite(output);
 readln(n,m);
 if n<=100 then
  begin
   fillchar(map,sizeof(map),$7f);
   fillchar(v,sizeof(v),0);
   ans:=maxlongint;
   for i:=1 to m do
     begin
      read(a,b,c);map[a,b]:=c;map[b,a]:=c;
      x[i]:=a;y[i]:=b;w[i]:=c;
     end;
   dfs(1,1,0);
   writeln(ans);

   readln(q);
   for i:=1 to q do
    begin
     fillchar(v,sizeof(v),0);
     readln(a,b);
     map[x[a],y[a]]:=b;map[y[a],x[a]]:=b;
     dfs(1,1,0);writeln(ans);
    end;
   close(input);close(output);halt;
  end;

 for i:=1 to m do read(x[i],y[i],w[i]);
 sort(1,m);
 ans:=0;
 for i:=1 to n do fa[i]:=i;
 for i:=1 to m do
  begin
   if find(x[i])<>find(y[i]) then
    begin ans:=ans+w[i];hebing(x[i],y[i]);end;
  end;
 writeln(ans);

 readln(q);
 for k:=1 to q do
  begin
   readln(a,b);

   j:=a;xx:=x[a];yy:=y[a];
   while w[j-1]>b do
    begin
     x[j]:=x[j-1];y[j]:=y[j-1];w[j]:=w[j-1];dec(j);
    end;
   x[j]:=xx;y[j]:=yy;w[j]:=b;
   ans:=0;
   for i:=1 to n do fa[i]:=i;
   for i:=1 to m do
    begin
     if find(x[i])<>find(y[i]) then begin ans:=ans+w[i];hebing(x[i],y[i]);end;
    end;
   writeln(ans);

  end;
 close(input);close(output);
end.