結果

問題 No.1301 Strange Graph Shortest Path
ユーザー zkouzkou
提出日時 2020-11-10 21:37:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,377 ms / 3,000 ms
コード長 4,379 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 222,348 KB
最終ジャッジ日時 2023-10-10 21:38:33
合計ジャッジ時間 36,670 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
71,316 KB
testcase_01 AC 80 ms
71,492 KB
testcase_02 AC 1,081 ms
212,856 KB
testcase_03 AC 979 ms
194,844 KB
testcase_04 AC 1,268 ms
221,844 KB
testcase_05 AC 958 ms
209,916 KB
testcase_06 AC 1,273 ms
211,296 KB
testcase_07 AC 1,128 ms
208,176 KB
testcase_08 AC 976 ms
195,924 KB
testcase_09 AC 921 ms
200,804 KB
testcase_10 AC 894 ms
194,940 KB
testcase_11 AC 1,052 ms
212,432 KB
testcase_12 AC 1,069 ms
214,456 KB
testcase_13 AC 1,008 ms
214,240 KB
testcase_14 AC 1,221 ms
203,232 KB
testcase_15 AC 900 ms
198,632 KB
testcase_16 AC 1,191 ms
222,344 KB
testcase_17 AC 1,158 ms
219,308 KB
testcase_18 AC 1,178 ms
204,988 KB
testcase_19 AC 938 ms
204,984 KB
testcase_20 AC 1,122 ms
207,544 KB
testcase_21 AC 1,132 ms
216,240 KB
testcase_22 AC 1,225 ms
212,096 KB
testcase_23 AC 979 ms
214,872 KB
testcase_24 AC 1,230 ms
210,296 KB
testcase_25 AC 1,142 ms
220,216 KB
testcase_26 AC 1,139 ms
209,316 KB
testcase_27 AC 887 ms
208,876 KB
testcase_28 AC 893 ms
202,968 KB
testcase_29 AC 1,377 ms
222,348 KB
testcase_30 AC 1,073 ms
218,268 KB
testcase_31 AC 1,140 ms
218,060 KB
testcase_32 AC 81 ms
71,180 KB
testcase_33 AC 569 ms
206,344 KB
testcase_34 AC 886 ms
217,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

input = sys.stdin.readline

class mcf_graph_int_cost:
    """
    頂点数、及び、costの総和が、4294967295 (== (1 << 32) - 1) を超えない前提下での高速な実装。
    どちらかが超えると動きません。ごめんなさい。
    """

    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 _dual_ref(self, s, t):
        self.dist = [0xFFFFFFFFFFFFFFFF] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        self.vis = [False] * self.n

        que = [s] # s ==  (0 << 32) + s 
        self.dist[s] = 0
        while que:
            v = heapq.heappop(que) & 4294967295
            if self.vis[v]:
                continue
            self.vis[v] = True
            if v == t:
                break
            for i in range(len(self.g[v])):
                e = self.g[v][i]
                if self.vis[e.to] or e.cap == 0:
                    continue
                cost = e.cost - self.dual[e.to] + self.dual[v]
                if self.dist[e.to] > self.dist[v] + cost:
                    self.dist[e.to] = self.dist[v] + cost
                    self.pv[e.to] = v
                    self.pe[e.to] = i
                    heapq.heappush(que, ((self.dist[e.to] << 32) + e.to))
        if not self.vis[t]:
            return False

        for v in range(self.n):
            if not self.vis[v]:
                continue
            self.dual[v] -= self.dist[t] - self.dist[v]
        
        return True


    def slope(self, s, t, flow_limit=0xFFFFFFFFFFFFFFFF):
        # assert 0 <= s < self.n
        # assert 0 <= t < self.n
        # assert s != t
        
        self.dual = [0] * self.n
        self.dist = [0xFFFFFFFFFFFFFFFF] * self.n
        self.pv = [-1] * self.n
        self.pe = [-1] * self.n
        self.vis = [False] * self.n
        
        flow = 0
        cost = 0
        prev_cost = -1
        result = [(flow, cost)]
        while flow < flow_limit:
            if not self._dual_ref(s, t):
                break
            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[self.pv[v]][self.pe[v]].cap)
                v = self.pv[v]
            v = t
            while v != s:
                e = self.g[self.pv[v]][self.pe[v]]
                e.cap -= c
                self.g[v][e.rev].cap += c
                v = self.pv[v]
            d = -self.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=0xFFFFFFFFFFFFFFFF):
        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


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

g = mcf_graph_int_cost(N)

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

print(g.flow(0, N - 1, 2)[1])
    
    
0