結果

問題 No.1301 Strange Graph Shortest Path
ユーザー zkouzkou
提出日時 2020-11-07 10:40:01
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 4,435 bytes
コンパイル時間 371 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 513,152 KB
最終ジャッジ日時 2024-09-13 00:42:15
合計ジャッジ時間 94,660 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 AC 38 ms
53,420 KB
testcase_02 TLE -
testcase_03 AC 2,890 ms
399,924 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 AC 2,708 ms
405,644 KB
testcase_10 AC 2,927 ms
403,068 KB
testcase_11 TLE -
testcase_12 TLE -
testcase_13 AC 2,864 ms
444,884 KB
testcase_14 TLE -
testcase_15 AC 2,660 ms
408,652 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 AC 2,627 ms
418,072 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 2,695 ms
417,584 KB
testcase_28 AC 2,893 ms
432,288 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

input = sys.stdin.readline

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
        vis = [False] * self.n

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

            que = [(0, s)]
            dist[s] = 0
            while que:
                _, v = heapq.heappop(que)
                if vis[v]:
                    continue
                vis[v] = True
                if v == t:
                    break
                for i in range(len(self.g[v])):
                    e = self.g[v][i]
                    if vis[e.to] or 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 not vis[t]:
                return False

            for v in range(self.n):
                if not vis[v]:
                    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



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

g = mcf_graph(N + 4 * M)

for i in range(M):
    u, v, c, d = map(int, input().split())
    u -= 1
    v -= 1
    # コスト c の、一回しか通れない無向辺
    u2 = N + i
    v2 = N + M + i
    g.add_edge(u, u2, 1, 0)
    g.add_edge(v, u2, 1, 0)
    g.add_edge(u2, v2, 1, c)
    g.add_edge(v2, u, 1, 0)
    g.add_edge(v2, v, 1, 0)
    # コスト d の、一回しか通れない無向辺
    u3 = N + 2 * M + i
    v3 = N + 3 * M + i
    g.add_edge(u, u3, 1, 0)
    g.add_edge(v, u3, 1, 0)
    g.add_edge(u3, v3, 1, d)
    g.add_edge(v3, u, 1, 0)
    g.add_edge(v3, v, 1, 0)

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