結果

問題 No.1301 Strange Graph Shortest Path
ユーザー toyuzukotoyuzuko
提出日時 2020-12-06 12:01:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,241 ms / 3,000 ms
コード長 3,466 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 81,684 KB
実行使用メモリ 208,020 KB
最終ジャッジ日時 2023-10-17 13:00:12
合計ジャッジ時間 34,373 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,432 KB
testcase_01 AC 39 ms
53,432 KB
testcase_02 AC 799 ms
201,884 KB
testcase_03 AC 677 ms
184,736 KB
testcase_04 AC 890 ms
208,000 KB
testcase_05 AC 759 ms
201,564 KB
testcase_06 AC 921 ms
195,976 KB
testcase_07 AC 798 ms
195,588 KB
testcase_08 AC 734 ms
186,036 KB
testcase_09 AC 676 ms
189,056 KB
testcase_10 AC 691 ms
185,672 KB
testcase_11 AC 805 ms
201,324 KB
testcase_12 AC 780 ms
200,680 KB
testcase_13 AC 676 ms
200,916 KB
testcase_14 AC 882 ms
189,304 KB
testcase_15 AC 709 ms
189,644 KB
testcase_16 AC 830 ms
208,020 KB
testcase_17 AC 879 ms
205,280 KB
testcase_18 AC 821 ms
191,004 KB
testcase_19 AC 905 ms
195,292 KB
testcase_20 AC 982 ms
195,008 KB
testcase_21 AC 1,136 ms
201,664 KB
testcase_22 AC 1,241 ms
197,672 KB
testcase_23 AC 725 ms
202,436 KB
testcase_24 AC 882 ms
195,368 KB
testcase_25 AC 839 ms
205,628 KB
testcase_26 AC 786 ms
195,076 KB
testcase_27 AC 710 ms
199,064 KB
testcase_28 AC 746 ms
193,520 KB
testcase_29 AC 990 ms
206,220 KB
testcase_30 AC 728 ms
205,348 KB
testcase_31 AC 821 ms
206,200 KB
testcase_32 AC 40 ms
53,432 KB
testcase_33 AC 462 ms
196,168 KB
testcase_34 AC 716 ms
207,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappop, heappush, heapify

class MinCostFlow():
    def __init__(self, n):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.pos = []

    def add_edge(self, fr, to, cap, cost):
        #assert 0 <= fr < self.n
        #assert 0 <= to < self.n
        m = len(self.pos)
        self.pos.append((fr, len(self.graph[fr])))
        self.graph[fr].append([to, len(self.graph[to]), cap, cost])
        self.graph[to].append([fr, len(self.graph[fr]) - 1, 0, -cost])
        return m

    def get_edge(self, idx):
        #assert 0 <= idx < len(self.pos)
        to, rev, cap, cost = self.graph[self.pos[idx][0]][self.pos[idx][1]]
        rev_to, rev_rev, rev_cap, rev_cost = self.graph[to][rev]
        return self.pos[idx][0], to, cap + rev_cap, rev_cap, cost

    def edges(self):
        for i in range(len(self.pos)):
            yield self.get_edge(i)

    def dual_ref(self, s, t):
        dist = [2**63 - 1] * self.n
        dist[s] = 0
        vis = [0] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        queue = []
        heappush(queue, (0, s))
        while queue:
            k, v = heappop(queue)
            if vis[v]: continue
            vis[v] = True
            if v == t: break
            for i in range(len(self.graph[v])):
                to, rev, cap, cost = self.graph[v][i]
                if vis[to] or cap == 0: continue
                cost += self.dual[v] - self.dual[to]
                if dist[to] - dist[v] > cost:
                    dist[to] = dist[v] + cost
                    self.pv[to] = v
                    self.pe[to] = i
                    heappush(queue, (dist[to], to))
        if not vis[t]: return False
        for v in range(self.n):
            if not vis[v]: continue
            self.dual[v] -= dist[t] - dist[v]
        return True

    def flow(self, s, t):
        return self.flow_with_limit(s, t, 2**63 - 1)

    def flow_with_limit(self, s, t, limit):
        return self.slope_with_limit(s, t, limit)[-1]

    def slope(self, s, t):
        return self.slope_with_limit(s, t, 2**63 - 1)

    def slope_with_limit(self, s, t, limit):
        #assert 0 <= s < self.n
        #assert 0 <= t < self.n
        #assert s != t
        flow = 0
        cost = 0
        prev_cost = -1
        res = [(flow, cost)]
        self.dual = [0] * self.n
        while flow < limit:
            if not self.dual_ref(s, t): break
            c = limit - flow
            v = t
            while v != s:
                c = min(c, self.graph[self.pv[v]][self.pe[v]][2])
                v = self.pv[v]
            v = t
            while v != s:
                to, rev, cap, _ = self.graph[self.pv[v]][self.pe[v]]
                self.graph[self.pv[v]][self.pe[v]][2] -= c
                self.graph[v][rev][2] += c
                v = self.pv[v]
            d = -self.dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                res.pop()
            res.append((flow, cost))
            prev_cost = cost
        return res

import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

N, M = map(int, input().split())

mcf = MinCostFlow(N)

for _ in range(M):
    u, v, c, d = map(int, input().split())
    mcf.add_edge(u - 1, v - 1, 1, c)
    mcf.add_edge(v - 1, u - 1, 1, c)
    mcf.add_edge(u - 1, v - 1, 1, d)
    mcf.add_edge(v - 1, u - 1, 1, d)

print(mcf.flow_with_limit(0, N - 1, 2)[1])
0