結果

問題 No.1301 Strange Graph Shortest Path
ユーザー zkouzkou
提出日時 2020-11-10 21:34:22
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 4,085 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 168,808 KB
最終ジャッジ日時 2024-09-13 00:48:21
合計ジャッジ時間 97,491 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
11,264 KB
testcase_01 AC 32 ms
11,136 KB
testcase_02 AC 2,825 ms
151,668 KB
testcase_03 AC 2,429 ms
134,776 KB
testcase_04 TLE -
testcase_05 AC 2,618 ms
150,240 KB
testcase_06 TLE -
testcase_07 AC 2,877 ms
149,336 KB
testcase_08 AC 2,470 ms
135,864 KB
testcase_09 AC 2,710 ms
141,564 KB
testcase_10 AC 2,357 ms
135,556 KB
testcase_11 AC 2,979 ms
153,308 KB
testcase_12 TLE -
testcase_13 AC 2,643 ms
152,736 KB
testcase_14 TLE -
testcase_15 AC 2,657 ms
141,728 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 2,803 ms
143,560 KB
testcase_19 AC 2,798 ms
150,516 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 AC 2,841 ms
153,272 KB
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 2,942 ms
149,828 KB
testcase_27 AC 2,777 ms
151,160 KB
testcase_28 AC 2,497 ms
145,008 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
testcase_32 AC 32 ms
11,136 KB
testcase_33 AC 2,144 ms
148,756 KB
testcase_34 TLE -
権限があれば一括ダウンロードができます

ソースコード

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

        que = [(0, s)]
        self.dist[s] = 0
        while que:
            _, v = heapq.heappop(que)
            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], 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=float('inf')):
        # assert 0 <= s < self.n
        # assert 0 <= t < self.n
        # assert s != t
        
        self.dual = [0] * self.n
        self.dist = [float('inf')] * 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=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)

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