虽然没人看,不过今天发一下克鲁斯卡尔(Kruscal)的PHP算法

 

Truck History

Time Limit: 2000MS
Memory Limit: 65536K
Total Submissions: 14955
Accepted: 5719

Description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on.      

Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as      
1/Σ(to,td)d(to,td)  
where the sum goes over all pairs of types in the derivation plan such that t   o    is the original type and t   d  the type derived from it and d(t   o,t   d) is the distance of the types.      
Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan.      

Input

The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

Output

For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

Sample Input

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Output

The highest possible quality is 1/3.

 

题意:历史上,曾用7个小写字母来表示每种truck的型号,每两种型号之间的差距为字母串中不同字母的个数。现在给出n种不同型号的truck,问怎样使

1/Σ(to,td)d(to,td)的值最小。(即找到一条连接所有truck的最短路径。)

和昨天是一样的题,今天换了个解法,这次是每次添加一条边,看看新的边权的和,代码上:

 

<?php 
define("MaxE", 2000); 
error_reporting( E_ALL&~E_NOTICE ); 
class Edge{ 
    public $from; 
    public $to; 
    public $value; 
} 
class Kruscal{ 
    public $edge = array(); 
    public $father = array(); 
    public $n; 
    public $value; 
    public $str=array(); 
 public $size; 
    
    function find($x){ 
        if($x!=$this->father[$x]){ 
            return $this->father[$x] = $this->find($this->father[$x]); 
        } 
        return $this->father[$x]; 
    } 
    
    function Union($a,$b){ 
        $x = $this->father[$a]; 
        $y = $this->father[$b]; 
        if($x!=$y)$this->father[$y] = $this->father[$x]; 
        return ; 
    } 
    function kruscal(){ 
        $this->value = 0; 
        $this->sort($this->edge); 
   
  /* for($i = 0;$i<$this->size;$i++) 
   { 
    echo "value:" + $this->edge[$i]->value."<br>"; 
   } 
  */ 
   
  for($i=0;$i<$this->size;$i++) 
  { 
   $k=0; 
   $x=$this->find($this->edge[$i]->from); 
   $y=$this->find($this->edge[$i]->to); 
   if($x==$y) continue; 
     
   $k++; 
   $this->Union($this->edge[$i]->from,$this->edge[$i]->to); 
   $this->value += $this->edge[$i]->value; 
   //echo "i:".$i."k:".$k."n-1".($this->n-1)."<br>"; 
   if($k==($this->n-1)) 
    break; 
  } 
    } 
    
    function init(){ 
        for($i=0;$i<2005;$i++) 
            $this->father[$i] = $i; 
        for($i=1;$i<=$this->n;$i++){ 
            for($j=$i+1;$j<=$this->n;$j++){ 
                $a=0; 
                for($k=0;$k<7;$k++) 
    { 
      if(substr($this->str[$i],$k,1)!=substr($this->str[$j],$k,1)) 
      $a++; 
    } 
    //echo "a:".$a."|".$this->size."<br>"; 
    $this->edge[$this->size]->from=$i; 
                $this->edge[$this->size]->to=$j; 
                $this->edge[$this->size]->value=$a;$this->size++; 
            } 
        } 
    } 
    
    
    
    function sort($arr){ 
        for($i=count($arr);$i>0;$i--){ 
            for($j=0;$j<$i-1;$j++){ 
                if($arr[$j]->value > $arr[$j+1]->value){ 
                    $t = $arr[$j]->value; 
                    $arr[$j]->value = $arr[$j+1]->value; 
                    $arr[$j+1]->value = $t; 
                } 
            } 
        }return $arr; 
    } 
  
    function main() 
 { 
   $this->size = 0; 
   $this->n = 4; 
   $this->str[1] = "aaaaaaa"; 
   $this->str[2] = "baaaaaa"; 
   $this->str[3] = "abaaaaa"; 
   $this->str[4] = "aabaaaa"; 
   for($i=0;$i<=50;$i++) 
   { 
    for($j=0;$j<=50;$j++) 
    { 
   $this->map[$i][$j]=0; 
    } 
   } 
   $this->init(); 
   
  /* for($i = 0;$i<$this->size;$i++) 
  { 
   echo "value:" + $this->edge[$i]->value."<br>"; 
  }*/ 
   
   
   $this->kruscal(); 
   echo "The highest possible quality is 1/".$this->value.".<br>"; 
 } 
} 
?> 
<?php 
$k = new Kruscal(); 
$k->main(); 
?>


你可能感兴趣的:(算法,PHP,最小生成树,克鲁斯卡尔,kruscal)