結果

問題 No.1301 Strange Graph Shortest Path
ユーザー tktk_snsntktk_snsn
提出日時 2020-11-27 23:58:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,054 ms / 3,000 ms
コード長 3,143 bytes
コンパイル時間 403 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 207,248 KB
最終ジャッジ日時 2023-10-09 22:11:17
合計ジャッジ時間 31,417 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,136 KB
testcase_01 AC 80 ms
71,344 KB
testcase_02 AC 881 ms
200,900 KB
testcase_03 AC 801 ms
184,648 KB
testcase_04 AC 990 ms
206,220 KB
testcase_05 AC 892 ms
201,552 KB
testcase_06 AC 995 ms
194,444 KB
testcase_07 AC 912 ms
194,476 KB
testcase_08 AC 833 ms
185,864 KB
testcase_09 AC 760 ms
187,832 KB
testcase_10 AC 783 ms
184,372 KB
testcase_11 AC 895 ms
199,008 KB
testcase_12 AC 881 ms
200,320 KB
testcase_13 AC 784 ms
200,088 KB
testcase_14 AC 964 ms
188,024 KB
testcase_15 AC 765 ms
188,056 KB
testcase_16 AC 918 ms
206,228 KB
testcase_17 AC 963 ms
204,004 KB
testcase_18 AC 943 ms
190,304 KB
testcase_19 AC 784 ms
193,792 KB
testcase_20 AC 846 ms
192,932 KB
testcase_21 AC 863 ms
201,172 KB
testcase_22 AC 997 ms
196,012 KB
testcase_23 AC 841 ms
201,436 KB
testcase_24 AC 971 ms
194,160 KB
testcase_25 AC 915 ms
205,004 KB
testcase_26 AC 847 ms
193,768 KB
testcase_27 AC 805 ms
198,452 KB
testcase_28 AC 837 ms
193,024 KB
testcase_29 AC 1,054 ms
205,228 KB
testcase_30 AC 828 ms
203,580 KB
testcase_31 AC 917 ms
203,912 KB
testcase_32 AC 80 ms
71,292 KB
testcase_33 AC 513 ms
194,452 KB
testcase_34 AC 804 ms
207,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)


class MCF_graph(object):
    def __init__(self, n):
        self.n = n
        self.g = [[] for _ in range(n)]  # to, rev, cap, cost
        self.pos = []

    def add_edge(self, frm, to, cap, cost):
        m = len(self.pos)
        self.pos.append((frm, len(self.g[frm])))
        self.g[frm].append([to, len(self.g[to]), cap, cost])
        self.g[to].append([frm, len(self.g[frm]) - 1, 0, -cost])
        return m

    def __get_edge(self, i):
        e_to, e_rev, e_cap, e_cost = self.g[self.pos[i][0]][self.pos[i][1]]
        re_to, _, re_cap, _ = self.g[e_to][e_rev]
        # from, to, cap, flow, cost
        return (re_to, e_to, e_cap + re_cap, re_cap, e_cost)

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.__get_edge(i)

    def flow(self, s, t, flow_limit=10**18):
        return self.slope(s, t, flow_limit)[-1]

    def slope(self, s, t, flow_limit=10**18):
        dual = [0] * self.n
        flow = 0
        cost = 0
        prev_cost = -1
        result = [(0, 0)]  # cap, cost
        while flow < flow_limit:
            # call dual_ref()
            dist = [10**18] * self.n
            pv = [-1] * self.n
            pe = [-1] * self.n
            vis = [False] * self.n
            dist[s] = 0
            que = [(0, s)]
            while que:
                _, v = heapq.heappop(que)
                if vis[v]:
                    continue
                vis[v] = True
                if v == t:
                    break
                for i, (e_to, _, e_cap, e_cost) in enumerate(self.g[v]):
                    if vis[e_to] or (not e_cap):
                        continue
                    tmp_cost = e_cost - dual[e_to] + dual[v]
                    if dist[e_to] > dist[v] + tmp_cost:
                        dist[e_to] = dist[v] + tmp_cost
                        pv[e_to] = v
                        pe[e_to] = i
                        heapq.heappush(que, (dist[e_to], e_to))
            if not vis[t]:
                break
            for v, visited in enumerate(vis):
                if not visited:
                    continue
                dual[v] -= dist[t] - dist[v]
            # end dual_ref()

            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[pv[v]][pe[v]][2])
                v = pv[v]
            v = t
            while v != s:
                i, j = pv[v], pe[v]
                self.g[i][j][2] -= c
                self.g[v][self.g[i][j][1]][2] += c
                v = i
            d = -dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                result.pop()
            result.append((flow, cost))
            prev_cost = cost
        return result


n, m = map(int, input().split())
g = MCF_graph(n)
for _ in range(m):
    a, b, c, d = map(int, input().split())
    a -= 1
    b -= 1
    g.add_edge(a, b, 1, c)
    g.add_edge(a, b, 1, d)
    g.add_edge(b, a, 1, c)
    g.add_edge(b, a, 1, d)

res = g.flow(0, n - 1, 2)[-1]
print(res)
0