結果

問題 No.654 Air E869120
ユーザー TANIGUCHI KousukeTANIGUCHI Kousuke
提出日時 2021-02-23 16:17:41
言語 Ruby
(3.3.0)
結果
AC  
実行時間 313 ms / 2,000 ms
コード長 2,197 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 11,912 KB
実行使用メモリ 24,324 KB
最終ジャッジ日時 2023-10-23 17:59:17
合計ジャッジ時間 10,585 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 84 ms
16,064 KB
testcase_01 AC 83 ms
16,064 KB
testcase_02 AC 83 ms
16,064 KB
testcase_03 AC 85 ms
16,064 KB
testcase_04 AC 84 ms
16,064 KB
testcase_05 AC 85 ms
16,064 KB
testcase_06 AC 86 ms
16,064 KB
testcase_07 AC 85 ms
16,064 KB
testcase_08 AC 85 ms
16,064 KB
testcase_09 AC 91 ms
16,064 KB
testcase_10 AC 313 ms
24,324 KB
testcase_11 AC 286 ms
22,128 KB
testcase_12 AC 291 ms
22,800 KB
testcase_13 AC 297 ms
23,020 KB
testcase_14 AC 288 ms
22,048 KB
testcase_15 AC 313 ms
22,768 KB
testcase_16 AC 231 ms
18,500 KB
testcase_17 AC 231 ms
18,500 KB
testcase_18 AC 234 ms
18,260 KB
testcase_19 AC 251 ms
18,668 KB
testcase_20 AC 233 ms
17,304 KB
testcase_21 AC 232 ms
17,392 KB
testcase_22 AC 231 ms
17,012 KB
testcase_23 AC 229 ms
17,076 KB
testcase_24 AC 238 ms
17,276 KB
testcase_25 AC 223 ms
16,644 KB
testcase_26 AC 226 ms
16,948 KB
testcase_27 AC 204 ms
16,620 KB
testcase_28 AC 207 ms
16,780 KB
testcase_29 AC 206 ms
16,352 KB
testcase_30 AC 182 ms
16,352 KB
testcase_31 AC 197 ms
16,352 KB
testcase_32 AC 193 ms
16,352 KB
testcase_33 AC 206 ms
16,352 KB
testcase_34 AC 184 ms
16,352 KB
testcase_35 AC 83 ms
16,064 KB
testcase_36 AC 84 ms
16,064 KB
testcase_37 AC 83 ms
16,064 KB
testcase_38 AC 92 ms
16,064 KB
testcase_39 AC 88 ms
16,064 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

INF = 10 ** 15

class Graph
  Edge = Struct.new(:to, :cap, :rev)
  def size; @g.size; end
  def _min(a,b); a < b ? a : b; end
  def initialize(n); @g = Array.new(n){ [] }; end
  def add_edge(from, to, cap)
    fwd, rev = @g[from].size, @g[to].size
    @g[from] << Edge.new(to, cap, rev)
    @g[to] << Edge.new(from, 0, fwd)
    self
  end
  
  def max_flow(s,t)
    flow = 0
    while level = levelize(s,t)
      active = Array.new(size, 0)
      while f = find_flow(s, t, level, active)
        flow += f
      end
    end
    flow
  end

  def find_flow(s, t, level, active)
    parent = Array.new(size)
    parent[s] = s
    u = s
    until u == t
      if (j = active[u]) >= @g[u].size
        break if u == s
        u = parent[u]
        active[u] += 1
      else
        e = @g[u][j]
        if e.cap > 0 && level[e.to] < level[u]
          parent[e.to] = u
          u = e.to
        else
          active[u] += 1
        end
      end
    end
    return nil if u == s
    f = INF
    u = s
    until u == t
      e = @g[u][active[u]]
      f = _min(e.cap, f)
      u = e.to
    end
    u = s
    until u == t
      fwd = @g[u][active[u]]
      rev = @g[fwd.to][fwd.rev]
      fwd.cap -= f
      rev.cap += f
      u = fwd.to
    end
    return f
  end

  def levelize(s,t)
    level = Array.new(size, @g.size * 2)
    level[t] = 0
    q = [t]
    until q.empty?
      u = q.shift
      d = level[u]
      @g[u].each do |e|
        if @g[e.to][e.rev].cap > 0 && level[e.to] > d + 1
          level[e.to] = d + 1
          q << e.to
        end
      end
    end
    level[s] = @g.size if level[s] <= @g.size
    level[s] <= @g.size ? level : nil
  end
end
Line = Struct.new(:id, :u, :v, :s, :t, :w) do
  def from; 2 * id; end
  def to; 2 * id + 1; end
end
N,M,d = gets.split.map(&:to_i)
S = 2 * M; T = 2 * M + 1;
G = Graph.new(2 * M + 2)
L = M.times.map{|id| u,v,s,t,w = gets.split.map(&:to_i); Line.new(id,u,v,s,t,w) }
L.each do |l|
  G.add_edge(l.from, l.to, l.w)
  G.add_edge(S, l.from, INF) if l.u == 1
  G.add_edge(l.to, T, INF) if l.v == N
end
L.each do |l|
  L.each do |r|
    next if l.v != r.u
    G.add_edge(l.to, r.from, INF) if l.t + d <= r.s
  end
end

puts G.max_flow(S, T)

0