結果

問題 No.1301 Strange Graph Shortest Path
ユーザー ああいいああいい
提出日時 2022-04-14 08:40:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,340 ms / 3,000 ms
コード長 2,218 bytes
コンパイル時間 274 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 229,856 KB
最終ジャッジ日時 2023-08-25 19:26:07
合計ジャッジ時間 42,845 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,308 KB
testcase_01 AC 77 ms
71,416 KB
testcase_02 AC 1,244 ms
221,844 KB
testcase_03 AC 1,340 ms
203,568 KB
testcase_04 AC 1,273 ms
227,680 KB
testcase_05 AC 1,185 ms
223,160 KB
testcase_06 AC 1,159 ms
216,076 KB
testcase_07 AC 1,152 ms
217,124 KB
testcase_08 AC 1,138 ms
205,348 KB
testcase_09 AC 1,123 ms
209,944 KB
testcase_10 AC 1,067 ms
205,052 KB
testcase_11 AC 1,335 ms
222,564 KB
testcase_12 AC 1,194 ms
221,440 KB
testcase_13 AC 1,151 ms
223,060 KB
testcase_14 AC 1,107 ms
208,792 KB
testcase_15 AC 1,106 ms
210,800 KB
testcase_16 AC 1,257 ms
228,044 KB
testcase_17 AC 1,205 ms
223,660 KB
testcase_18 AC 1,215 ms
211,384 KB
testcase_19 AC 1,118 ms
218,088 KB
testcase_20 AC 1,074 ms
215,984 KB
testcase_21 AC 1,093 ms
223,396 KB
testcase_22 AC 1,269 ms
219,444 KB
testcase_23 AC 1,202 ms
223,844 KB
testcase_24 AC 1,149 ms
217,620 KB
testcase_25 AC 1,218 ms
226,456 KB
testcase_26 AC 1,107 ms
215,540 KB
testcase_27 AC 1,149 ms
222,416 KB
testcase_28 AC 1,013 ms
213,524 KB
testcase_29 AC 1,176 ms
227,080 KB
testcase_30 AC 1,267 ms
225,376 KB
testcase_31 AC 1,196 ms
224,796 KB
testcase_32 AC 77 ms
71,224 KB
testcase_33 AC 598 ms
215,604 KB
testcase_34 AC 1,254 ms
229,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#その2 最初はコストが全部正の場合に使えるダイクストラ
from heapq import heappush,heappop
class MinCostFlow:
    inf = 10 ** 18

    def __init__(self,N):
        self.N = N
        self.G = [[] for _ in range(N)]
        self.H = [0] * N
        self.edge = []

    def add_edge(self,fr,to,cap,cost):
        e = [to,cap,cost,None]
        r = e[3] = [fr,0,-cost,e]
        self.G[fr].append(e)
        self.G[to].append(r)
        self.edge.append(e)
    def get_edge(self,i):
        return self.edge[i]

    def flow(self,s,t,f):
        N = self.N
        G = self.G
        inf = MinCostFlow.inf

        res = 0
        H = self.H  #ポテンシャル、コストが最初は正なので、最初は0でいい
        prev_v = [0] * N
        prev_e = [None] * N

        d0 = [inf] * N
        dist = [inf] * N
        while f:
            dist[:] = d0
            dist[s] = 0
            q = [(0,s)]
            while q:
                c,v = heappop(q)
                if dist[v] < c:continue
                r0 = dist[v] + H[v]
                for e in G[v]:
                    w,cap,cost,_ = e
                    if cap > 0 and r0 + cost - H[w] < dist[w]:
                        dist[w] = r = r0 + cost - H[w]
                        prev_v[w] = v
                        prev_e[w] = e
                        heappush(q,(r,w))
            if dist[t] == inf:
                return None
            """
            for i in range(N):
                H[i] += dist[i]
            """
            H = [h + d for h,d in zip(H,dist)]
            d = f
            v = t
            while v != s:
                d = min(d,prev_e[v][1])
                v = prev_v[v]
            f -= d
            res += d * H[t]
            v = t
            while v != s:
                e = prev_e[v]
                e[1] -= d
                e[3][1] += d
                v = prev_v[v]
        return res

mincost = MinCostFlow(N)
for _ in range(M):
    u,v,c,d = map(int,input().split())
    u -= 1
    v -= 1
    mincost.add_edge(u,v,1,c)
    mincost.add_edge(u,v,1,d)
    mincost.add_edge(v,u,1,c)
    mincost.add_edge(v,u,1,d)
print(mincost.flow(0,N-1,2))
0