結果

問題 No.2604 Initial Motion
ユーザー 👑 amentorimaruamentorimaru
提出日時 2023-11-15 21:26:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,382 ms / 3,000 ms
コード長 3,809 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 87,584 KB
最終ジャッジ日時 2024-09-26 04:53:53
合計ジャッジ時間 43,283 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
53,376 KB
testcase_01 AC 43 ms
52,992 KB
testcase_02 AC 45 ms
53,504 KB
testcase_03 AC 213 ms
78,216 KB
testcase_04 AC 197 ms
77,312 KB
testcase_05 AC 210 ms
78,184 KB
testcase_06 AC 186 ms
77,696 KB
testcase_07 AC 189 ms
77,848 KB
testcase_08 AC 193 ms
77,932 KB
testcase_09 AC 185 ms
77,568 KB
testcase_10 AC 210 ms
78,208 KB
testcase_11 AC 195 ms
77,696 KB
testcase_12 AC 194 ms
77,848 KB
testcase_13 AC 1,632 ms
82,492 KB
testcase_14 AC 923 ms
80,784 KB
testcase_15 AC 639 ms
80,076 KB
testcase_16 AC 1,284 ms
83,144 KB
testcase_17 AC 1,890 ms
83,428 KB
testcase_18 AC 1,846 ms
83,416 KB
testcase_19 AC 1,831 ms
83,404 KB
testcase_20 AC 1,177 ms
81,888 KB
testcase_21 AC 1,001 ms
81,036 KB
testcase_22 AC 1,723 ms
83,292 KB
testcase_23 AC 1,094 ms
82,004 KB
testcase_24 AC 1,632 ms
83,020 KB
testcase_25 AC 2,247 ms
83,884 KB
testcase_26 AC 1,315 ms
82,548 KB
testcase_27 AC 839 ms
80,316 KB
testcase_28 AC 1,070 ms
81,184 KB
testcase_29 AC 1,596 ms
82,912 KB
testcase_30 AC 928 ms
80,876 KB
testcase_31 AC 1,254 ms
82,124 KB
testcase_32 AC 1,564 ms
82,260 KB
testcase_33 AC 110 ms
78,208 KB
testcase_34 AC 1,546 ms
82,764 KB
testcase_35 AC 2,289 ms
87,584 KB
testcase_36 AC 2,382 ms
84,784 KB
testcase_37 AC 130 ms
78,464 KB
testcase_38 AC 138 ms
77,572 KB
testcase_39 AC 128 ms
77,012 KB
testcase_40 AC 1,993 ms
82,120 KB
testcase_41 AC 2,029 ms
82,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
def read_values(): return tuple(map(int, input().split()))
def read_list(): return list(map(int, input().split()))

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,len(self.g[To]),cap,cost])
        self.g[To].append([From,len(self.g[From])-1,0,-cost])
    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[0]] or (not(e[2])):continue
                    '''
                     |-dual[e.to]+dual[v]| <= (n-1)C
                     cost <= C - -(n-1)C + 0 = nC
                    '''
                    cost=e[3]-dual[e[0]]+dual[v]
                    if dist[e[0]]-dist[v]>cost:
                        dist[e[0]]=dist[v]+cost
                        pv[e[0]]=v
                        pe[e[0]]=i
                        heapq.heappush(que,(dist[e[0]],e[0]))
            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]][2])
                v=pv[v]
            v=t
            while(v!=s):
                self.g[pv[v]][pe[v]][2]-=c
                self.g[v][self.g[pv[v]][pe[v]][1]][2]+=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 main():  
    k,n,m=read_values()
    a=read_list()
    b=read_list()
    c=[0]*n
    mcf=mcf_graph(n+2)
    for _ in range(m):
        u,v,d=read_values()
        mcf.add_edge(u-1,v-1,1000000,d)
        mcf.add_edge(v-1,u-1,1000000,d)
    for v in a:
        c[v-1]+=1
    for i in range(n):
        if c[i]:
            mcf.add_edge(n,i,c[i],0)
        if b[i]:
            mcf.add_edge(i,n+1,b[i],0)
    print(mcf.flow(n,n+1)[1])
        
        
if __name__ == "__main__":
    main()
0