Submission #2865556


Source Code Expand

# coding: utf-8
#
# ----------------------
# WarshallFloyd
# ----------------------
# :u ノードのインデックス
# :k u の出次数
# :v u に隣接する頂点の番号
# :w 隣接する頂点への重み
#
INF  = Float::INFINITY
Node = Struct.new(:u, :k, :v, :w)

class Graph
  attr :nodes, :dist
  def initialize(n)
    @nodes = Array.new(n){ Node.new }
    @nodes = @nodes.map.with_index do |node,idx|
      node.u = idx
      node
    end
    @dist = Array.new(n).map{Array.new(n, 0)}
  end

  def add_graph_edge(u, v, w)
    #puts "#{u} -> #{v} (#{w})"
    if @nodes[u].v.nil?
      @nodes[u].v = [v]
      @nodes[u].w = {v=>w}
    elsif @nodes[u].v.is_a?(Array)
      @nodes[u].v << v
      @nodes[u].w = {v=>w}.merge(@nodes[u].w)
    else
      @nodes[u].v = [@nodes[u].v, v]
      @nodes[u].w = {v=>w}.merge(@nodes[u].w)
    end
    @nodes[u].k = if @nodes[u].v.is_a?(Array)
                    @nodes[u].v.length
                  else
                    1
                  end
  end

  def warshall_floyd()
    # initialize
    for i in 0...@nodes.length
      for j in 0...@nodes.length
        @dist[i][j] = if not @nodes[i].w.nil? and @nodes[i].w.has_key?(j)
                        @nodes[i].w[j]
                      else
                        INF
                      end
      end
      @dist[i][i] = 0
    end

    # calc
    for k in 0...@nodes.length
      for i in 0...@nodes.length
        for j in 0...@nodes.length
          if @dist[i][k] != INF and @dist[k][j] != INF
            @dist[i][j] = [@dist[i][j], @dist[i][k] + @dist[k][j]].min
          end
        end
      end
    end
  end

  def has_negative_cycle?()
    ans = false
    for v in 0...V
      ans = true if @dist[v][v] < 0
    end
    ans
  end
end

lines = $stdin.read
array = lines.split("\n")

N,M,R = array[0].split(" ").map(&:to_i)
graph = Graph.new(N)
rarr  = array[1].split(" ").map(&:to_i).map{|idx| idx-1}

array[2..M+1].each do |s|
  s,t,d = s.split(" ").map(&:to_i)
  s,t=s-1,t-1
  graph.add_graph_edge(s,t,d)
  graph.add_graph_edge(t,s,d)
end

# execute WarshallFloyd !
graph.warshall_floyd()

dists = rarr.permutation(rarr.length).map do |picked|
  #p picked
  picked.each_cons(2).map do |arr|
    s,t = arr.first,arr.last
    graph.dist[s][t]
  end.sum
end

#p dists
puts dists.min

Submission Info

Submission Time
Task D - joisino's travel
User hiroyuking
Language Ruby (2.3.3)
Score 0
Code Size 2407 Byte
Status RE
Exec Time 2112 ms
Memory 81404 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
RE × 3
TLE × 6
RE × 5
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
01.txt TLE 2112 ms 79740 KB
02.txt TLE 2001 ms 2556 KB
03.txt RE 1942 ms 2556 KB
04.txt TLE 2108 ms 4860 KB
05.txt TLE 2108 ms 8956 KB
06.txt TLE 2112 ms 80380 KB
07.txt TLE 2112 ms 81404 KB
08.txt RE 939 ms 2556 KB
sample_01.txt RE 8 ms 1788 KB
sample_02.txt RE 8 ms 1788 KB
sample_03.txt RE 8 ms 1788 KB