def mincost_path
      return [] if length==0
      # calc cost
      self[0].each{|n|
        n.total_cost=n.word.cost
        n.nearest_prev=nil
      }
      (1...length).each{|i|
        prevs=@ends[i-1]
        curs=@begins[i]
        prevs.each{|prev|
          # 途中で行き止まりのNodeはtotal_costが設定されない
          next if prev.total_cost.nil?
          curs.each{|cur|
            join_cost=@mat.cost(prev.word.right.id,cur.word.left.id)
            next if join_cost.nil?
            cost=prev.total_cost+join_cost+cur.word.cost
            if !cur.total_cost || cost < cur.total_cost
              cur.total_cost=cost
              cur.nearest_prev=prev
            end
          }
        }
      }
      # calc mincost path
      ret=[]
      cur=self[-1][0]
      until cur.nil?
        ret.push cur
        cur=cur.nearest_prev
      end
      # TODO: disconnected
      #  return nil unless ...
      # success
      return ret.reverse
    end
def mincost_path
      return [] if length==0
      # calc cost
      self[0].each{|n|
        n.total_cost=n.word.cost
        n.nearest_prev=nil
      }
      (1...length).each{|i|
        prevs=@ends[i-1]
        curs=@begins[i]
        prevs.each{|prev|
          # 途中で行き止まりのNodeはtotal_costが設定されない
          next if prev.total_cost.nil?
          curs.each{|cur|
            join_cost=@mat.cost(prev.word.right.id,cur.word.left.id)
            next if join_cost.nil?
            cost=prev.total_cost+join_cost+cur.word.cost
            if !cur.total_cost || cost < cur.total_cost
              cur.total_cost=cost
              cur.nearest_prev=prev
            end
          }
        }
      }
      # calc mincost path
      ret=[]
      cur=self[-1][0]
      until cur.nil?
        ret.push cur
        cur=cur.nearest_prev
      end
      # TODO: disconnected
      #  return nil unless ...
      # success
      return ret.reverse
    end
Matrix -> [Node] | nil