結果

問題 No.1301 Strange Graph Shortest Path
ユーザー NoneNone
提出日時 2021-04-18 20:10:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,119 ms / 3,000 ms
コード長 3,996 bytes
コンパイル時間 734 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 211,344 KB
最終ジャッジ日時 2023-09-17 08:03:54
合計ジャッジ時間 31,651 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,204 KB
testcase_01 AC 79 ms
71,056 KB
testcase_02 AC 923 ms
202,456 KB
testcase_03 AC 825 ms
185,784 KB
testcase_04 AC 1,010 ms
207,380 KB
testcase_05 AC 918 ms
203,084 KB
testcase_06 AC 1,026 ms
195,804 KB
testcase_07 AC 940 ms
196,528 KB
testcase_08 AC 839 ms
187,984 KB
testcase_09 AC 774 ms
189,552 KB
testcase_10 AC 782 ms
186,808 KB
testcase_11 AC 927 ms
201,596 KB
testcase_12 AC 905 ms
201,196 KB
testcase_13 AC 808 ms
201,360 KB
testcase_14 AC 1,005 ms
190,448 KB
testcase_15 AC 814 ms
189,756 KB
testcase_16 AC 986 ms
208,128 KB
testcase_17 AC 1,026 ms
205,960 KB
testcase_18 AC 978 ms
192,280 KB
testcase_19 AC 823 ms
195,972 KB
testcase_20 AC 857 ms
194,188 KB
testcase_21 AC 877 ms
202,472 KB
testcase_22 AC 1,013 ms
197,192 KB
testcase_23 AC 858 ms
203,412 KB
testcase_24 AC 1,022 ms
195,936 KB
testcase_25 AC 968 ms
207,332 KB
testcase_26 AC 920 ms
195,828 KB
testcase_27 AC 836 ms
201,060 KB
testcase_28 AC 859 ms
194,816 KB
testcase_29 AC 1,119 ms
207,276 KB
testcase_30 AC 864 ms
204,808 KB
testcase_31 AC 963 ms
205,336 KB
testcase_32 AC 80 ms
71,212 KB
testcase_33 AC 528 ms
196,340 KB
testcase_34 AC 839 ms
211,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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):
        """ i 番目に追加された辺について (from, to, 初期の cap, 現在の流量(始めは 0), コスト) """
        # 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, limit=2**63-1):
        """
        return: (最大流量、最小コスト)
        """
        return self.slope(s, t, limit)[-1]

    def slope(self, s, t, limit=2**63-1):
        """
        各流量毎の最小コスト (流量でソートされている)
        return: (流量1、最小コスト1), (流量2、最小コスト2), (流量3、最小コスト3),...
        """
        # 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

def example():
    global input
    example = iter(
        """
4 5 2
0 1 2 1
0 2 1 2
1 2 1 1
1 3 1 3
2 3 2 1
        """
            .strip().split("\n"))

    input = lambda: next(example)


############################################################
from heapq import *
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

# example()

BIG = 10**18
N,M = map(int,input().split())     # F=sinkに到達させたい流量
F=2
mcf = MinCostFlow(N)
for _ in range(M):
    u, v, cost1, cost2 = map(int, input().split())
    u,v=u-1,v-1
    mcf.add_edge(u, v, 1, cost1)
    mcf.add_edge(u, v, 1, cost2)
    mcf.add_edge(v, u, 1, cost1)
    mcf.add_edge(v, u, 1, cost2)

f,cost=mcf.flow(0,N-1,F)

print(cost)
0