結果

問題 No.2604 Initial Motion
ユーザー miya145592miya145592
提出日時 2024-01-13 01:56:25
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 4,514 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 94,044 KB
最終ジャッジ日時 2024-01-13 01:57:33
合計ジャッジ時間 37,004 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,460 KB
testcase_01 AC 37 ms
53,460 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 236 ms
77,836 KB
testcase_04 AC 232 ms
78,484 KB
testcase_05 AC 232 ms
78,492 KB
testcase_06 AC 206 ms
77,952 KB
testcase_07 AC 201 ms
77,836 KB
testcase_08 AC 244 ms
78,108 KB
testcase_09 AC 200 ms
77,828 KB
testcase_10 AC 208 ms
77,836 KB
testcase_11 AC 205 ms
77,968 KB
testcase_12 AC 204 ms
78,096 KB
testcase_13 AC 2,665 ms
86,252 KB
testcase_14 AC 1,702 ms
83,904 KB
testcase_15 AC 1,008 ms
82,756 KB
testcase_16 AC 2,127 ms
85,480 KB
testcase_17 TLE -
testcase_18 AC 2,901 ms
87,012 KB
testcase_19 AC 2,951 ms
87,300 KB
testcase_20 AC 2,125 ms
85,480 KB
testcase_21 AC 1,812 ms
84,116 KB
testcase_22 AC 2,831 ms
86,728 KB
testcase_23 AC 1,934 ms
84,364 KB
testcase_24 AC 2,574 ms
85,936 KB
testcase_25 TLE -
testcase_26 AC 2,353 ms
85,316 KB
testcase_27 AC 1,464 ms
83,384 KB
testcase_28 AC 2,044 ms
85,228 KB
testcase_29 AC 2,645 ms
86,036 KB
testcase_30 AC 1,598 ms
83,608 KB
testcase_31 AC 2,149 ms
85,524 KB
testcase_32 AC 2,673 ms
85,800 KB
testcase_33 AC 98 ms
80,420 KB
testcase_34 AC 2,528 ms
86,696 KB
testcase_35 TLE -
testcase_36 TLE -
testcase_37 AC 119 ms
80,316 KB
testcase_38 AC 108 ms
76,612 KB
testcase_39 AC 116 ms
76,620 KB
testcase_40 TLE -
testcase_41 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://github.com/shakayami/ACL-for-python/blob/master/mincostflow.py
import heapq
class mcf_graph():
    n=1
    pos=[]
    g=[[]]
    def __init__(self,N):
        self.n=N
        self.pos=[]
        self.g=[[] for i in range(N)]
    def add_edge(self,From,To,cap,cost):
        assert 0<=From and From<self.n
        assert 0<=To and To<self.n
        m=len(self.pos)
        self.pos.append((From,len(self.g[From])))
        self.g[From].append({"to":To,"rev":len(self.g[To]),"cap":cap,"cost":cost})
        self.g[To].append({"to":From,"rev":len(self.g[From])-1,"cap":0,"cost":-cost})
    def get_edge(self,i):
        m=len(self.pos)
        assert 0<=i and i<m
        _e=self.g[self.pos[i][0]][self.pos[i][1]]
        _re=self.g[_e["to"]][_e["rev"]]
        return {"from":self.pos[i][0],"to":_e["to"],"cap":_e["cap"]+_re["cap"],
        "flow":_re["cap"],"cost":_e["cost"]}
    def edges(self):
        m=len(self.pos)
        result=[{} for i in range(m)]
        for i in range(m):
            tmp=self.get_edge(i)
            result[i]["from"]=tmp["from"]
            result[i]["to"]=tmp["to"]
            result[i]["cap"]=tmp["cap"]
            result[i]["flow"]=tmp["flow"]
            result[i]["cost"]=tmp["cost"]
        return result
    def flow(self,s,t,flow_limit=-1-(-1<<63)):
        return self.slope(s,t,flow_limit)[-1]
    def slope(self,s,t,flow_limit=-1-(-1<<63)):
        assert 0<=s and s<self.n
        assert 0<=t and t<self.n
        assert s!=t
        '''
         variants (C = maxcost):
         -(n-1)C <= dual[s] <= dual[i] <= dual[t] = 0
         reduced cost (= e.cost + dual[e.from] - dual[e.to]) >= 0 for all edge
        '''
        dual=[0 for i in range(self.n)]
        dist=[0 for i in range(self.n)]
        pv=[0 for i in range(self.n)]
        pe=[0 for i in range(self.n)]
        vis=[False for i in range(self.n)]
        def dual_ref():
            for i in range(self.n):
                dist[i]=-1-(-1<<63)
                pv[i]=-1
                pe[i]=-1
                vis[i]=False
            que=[]
            heapq.heappush(que,(0,s))
            dist[s]=0
            while(que):
                v=heapq.heappop(que)[1]
                if vis[v]:continue
                vis[v]=True
                if v==t:break
                '''
                 dist[v] = shortest(s, v) + dual[s] - dual[v]
                 dist[v] >= 0 (all reduced cost are positive)
                 dist[v] <= (n-1)C
                '''
                for i in range(len(self.g[v])):
                    e=self.g[v][i]
                    if vis[e["to"]] or (not(e["cap"])):continue
                    '''
                     |-dual[e.to]+dual[v]| <= (n-1)C
                     cost <= C - -(n-1)C + 0 = nC
                    '''
                    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):
                self.g[pv[v]][pe[v]]["cap"]-=c
                self.g[v][self.g[pv[v]][pe[v]]["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
    
import sys
input = sys.stdin.readline
K, N, M = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
MCF = mcf_graph(N+2)
INF = 10**5
C = [0 for _ in range(N)]
for a in A:
    a-=1
    C[a]+=1
for i in range(N):
    if C[i]>0:
        MCF.add_edge(0, i+1, C[i], 0)
    if B[i]>0:
        MCF.add_edge(i+1, N+1, B[i], 0)
for _ in range(M):
    u, v, d = map(int, input().split())
    MCF.add_edge(u, v, INF, d)
    MCF.add_edge(v, u, INF, d)
ans = MCF.flow(0, N+1, K)
print(ans[1])
0