I and OI
Past...

我们新建一个0号点,作为水库的水源,到每个点的费用就是wi.

注意到每个点的水源只有一个,所以最后答案是一棵树.显然是最小生成树了.

code:

/************************************************************** 
    Problem: 1601
    User: exponent 
    Language: Pascal 
    Result: Accepted 
    Time:100 ms 
    Memory:584 kb 
****************************************************************/ 
  
const maxn=301; 
      oo=maxlongint>>1; 
var   mind:array[0..maxn] of longint; 
      d:array[0..maxn,0..maxn] of longint; 
      vis:array[0..maxn] of boolean; 
      n,i,j,w:longint; 
  
  
      procedure prim(root:longint); 
      var   i,j,k,min,sum:longint; 
      begin
            sum:=0; 
            fillchar(vis,sizeof(vis),0); 
            vis[root]:=true; 
            for i:=1 to n do mind[i]:=d[i,root]; 
            for i:=1 to n do
            begin
                  min:=oo; 
                  for j:=1 to n do
                  if (not vis[j])and(mind[j]<min) then
                  begin
                        min:=mind[j]; 
                        k:=j; 
                  end; 
                  vis[k]:=true; 
                  sum:=sum+min; 
                  for j:=1 to n do
                  if (not vis[j])and(d[j,k]<mind[j]) then
                    mind[j]:=d[j,k]; 
            end; 
            writeln(sum); 
      end; 
  
begin
      readln(n); 
      for i:=1 to n do
      begin
            readln(w); 
            d[0,i]:=w; 
            d[i,0]:=w; 
      end; 
      for i:=1 to n do
      begin
             for j:=1 to n do
             begin
                   read(d[i,j]); 
                   if d[i,j]=0 then d[i,j]:=oo; 
             end; 
             readln; 
      end; 
  
      Prim(0); 
end.
posted on 2011-08-13 20:55  exponent  阅读(327)  评论(0编辑  收藏  举报