結果

問題 No.1301 Strange Graph Shortest Path
ユーザー Kite_kumaKite_kuma
提出日時 2020-10-30 23:04:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,924 ms / 3,000 ms
コード長 4,241 bytes
コンパイル時間 409 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 300,372 KB
最終ジャッジ日時 2024-09-13 00:33:21
合計ジャッジ時間 52,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,108 KB
testcase_01 AC 39 ms
55,548 KB
testcase_02 AC 1,618 ms
290,608 KB
testcase_03 AC 1,359 ms
270,232 KB
testcase_04 AC 1,854 ms
299,696 KB
testcase_05 AC 1,558 ms
290,792 KB
testcase_06 AC 1,809 ms
286,352 KB
testcase_07 AC 1,592 ms
286,700 KB
testcase_08 AC 1,426 ms
268,192 KB
testcase_09 AC 1,278 ms
270,436 KB
testcase_10 AC 1,261 ms
269,616 KB
testcase_11 AC 1,541 ms
291,228 KB
testcase_12 AC 1,540 ms
291,944 KB
testcase_13 AC 1,400 ms
291,856 KB
testcase_14 AC 1,723 ms
269,420 KB
testcase_15 AC 1,366 ms
270,152 KB
testcase_16 AC 1,748 ms
300,372 KB
testcase_17 AC 1,826 ms
294,820 KB
testcase_18 AC 1,713 ms
280,052 KB
testcase_19 AC 1,348 ms
287,676 KB
testcase_20 AC 1,581 ms
285,184 KB
testcase_21 AC 1,662 ms
292,252 KB
testcase_22 AC 1,723 ms
289,788 KB
testcase_23 AC 1,435 ms
291,836 KB
testcase_24 AC 1,747 ms
286,872 KB
testcase_25 AC 1,765 ms
298,804 KB
testcase_26 AC 1,597 ms
287,600 KB
testcase_27 AC 1,346 ms
290,132 KB
testcase_28 AC 1,463 ms
283,024 KB
testcase_29 AC 1,924 ms
299,724 KB
testcase_30 AC 1,446 ms
297,488 KB
testcase_31 AC 1,610 ms
297,424 KB
testcase_32 AC 38 ms
54,676 KB
testcase_33 AC 934 ms
294,228 KB
testcase_34 AC 1,461 ms
294,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

import heapq


class mcf_graph:

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

    def add_edge(self, from_, to, cap, cost):
        assert 0 <= from_ < self.n
        assert 0 <= to < self.n
        m = len(self.pos)
        self.pos.append((from_, len(self.g[from_])))
        self.g[from_].append(self.__class__._edge(
            to, len(self.g[to]), cap, cost))
        self.g[to].append(self.__class__._edge(
            from_, len(self.g[from_]) - 1, 0, -cost))
        return m

    class edge:
        def __init__(self, from_, to, cap, flow, cost):
            self.from_ = from_
            self.to = to
            self.cap = cap
            self.flow = flow
            self.cost = cost

    def get_edge(self, i):
        _e = self.g[self.pos[i][0]][self.pos[i][1]]
        _re = self.g[_e.to][_e.rev]
        return self.__class__.edge(self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost)

    def edges(self):
        ret = []
        for i in range(len(self.pos)):
            _e = self.g[self.pos[i][0]][self.pos[i][1]]
            _re = self.g[_e.to][_e.rev]
            ret.append(self.__class__.edge(
                self.pos[i][0], _e.to, _e.cap + _re.cap, _re.cap, _e.cost))
        return ret

    def slope(self, s, t, flow_limit=float('inf')):
        # assert 0 <= s < self.n
        # assert 0 <= t < self.n
        # assert s != t

        dual = [0] * self.n
        dist = [float('inf')] * self.n
        pv = [-1] * self.n
        pe = [-1] * self.n

        def _dual_ref():
            nonlocal dual, dist, pv, pe
            dist = [float('inf')] * self.n
            pv = [-1] * self.n
            pe = [-1] * self.n

            que = [(0, s)]
            dist[s] = 0
            while que:
                dist_v, v = heapq.heappop(que)
                if dist[v] < dist_v:
                    continue
                if v == t:
                    break
                for i in range(len(self.g[v])):
                    e = self.g[v][i]
                    if e.cap == 0:
                        continue
                    cost = e.cost - dual[e.to] + dual[v]
                    if dist[e.to] > dist[v] + cost:
                        dist[e.to] = dist[v] + cost
                        pv[e.to] = v
                        pe[e.to] = i
                        heapq.heappush(que, (dist[e.to], e.to))
            if dist[t] == float('inf'):
                return False

            for v in range(self.n):
                if dist[v] == float('inf'):
                    continue
                dual[v] -= dist[t] - dist[v]

            return True

        flow = 0
        cost = 0
        prev_cost = -1
        result = [(flow, cost)]
        while flow < flow_limit:
            if not _dual_ref():
                break
            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[pv[v]][pe[v]].cap)
                v = pv[v]
            v = t
            while v != s:
                e = self.g[pv[v]][pe[v]]
                e.cap -= c
                self.g[v][e.rev].cap += c
                v = pv[v]
            d = -dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                result.pop()
            result.append((flow, cost))
            prev_cost = cost
        return result

    def flow(self, s, t, flow_limit=float('inf')):
        return self.slope(s, t, flow_limit)[-1]

    class _edge:
        def __init__(self, to, rev, cap, cost):
            self.to = to
            self.rev = rev
            self.cap = cap
            self.cost = cost


BIG = 10 ** 9

input = sys.stdin.readline


n, m = map(int, input().split())
s, t = 0, n - 1
graph = mcf_graph(n + m)

for i in range(m):
    u, v, c, d = map(int, input().split())
    assert 1 <= u <= n and 1 <= v <= n and c <= d
    u -= 1
    v -= 1
    mid = n + i
    graph.add_edge(u, mid, 2, c)
    graph.add_edge(mid, v, 1, 0)
    graph.add_edge(mid, v, 1, d - c)
    graph.add_edge(v, mid, 2, c)
    graph.add_edge(mid, u, 1, 0)
    graph.add_edge(mid, u, 1, d - c)

flow, cost = graph.flow(s, t, 2)
assert flow == 2
print(cost)
0