Submission #2865560


Source Code Expand

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|
  sum = 0
  picked.each_cons(2).map do |arr|
    s,t = arr.first,arr.last
    sum += graph.dist[s][t]
  end
  sum
end

#p dists
p dists.min

Submission Info

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

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 4
TLE × 7
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 81404 KB
02.txt TLE 2107 ms 3068 KB
03.txt TLE 2077 ms 3068 KB
04.txt TLE 2108 ms 4860 KB
05.txt TLE 2108 ms 8956 KB
06.txt TLE 2112 ms 78588 KB
07.txt TLE 2111 ms 83580 KB
08.txt AC 1082 ms 3068 KB
sample_01.txt AC 7 ms 1788 KB
sample_02.txt AC 7 ms 1788 KB
sample_03.txt AC 7 ms 1788 KB