結果

問題 No.2604 Initial Motion
ユーザー katonyonkokatonyonko
提出日時 2024-01-12 23:49:00
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,679 bytes
コンパイル時間 244 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 110,200 KB
最終ジャッジ日時 2024-09-28 00:27:20
合計ジャッジ時間 5,040 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
64,240 KB
testcase_01 AC 52 ms
55,296 KB
testcase_02 AC 73 ms
68,096 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import io
import sys
from collections import defaultdict, deque, Counter
from itertools import permutations, combinations, accumulate
from heapq import heappush, heappop
from bisect import bisect_right, bisect_left
from math import gcd
import math

_INPUT = """\
6
4 5 4
1 1 1 4
1 1 2 0 0
1 2 4
1 3 5
4 5 1
5 3 3
4 5 4
2 3 4 5
4 0 0 0 0
1 2 1
1 3 1
1 4 1
1 5 1
20 10 10
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
4 0 0 3 3 0 1 5 0 4
1 2 4
2 3 2
3 4 3
4 5 1
5 6 2
6 7 4
7 8 5
2 9 3
6 9 1
9 10 8
"""


# 最小費用流(minimum cost flow)
class MinCostFlow:
    def __init__(self, n):
        self.n = n
        self.G = [[] for i in range(n)]

    def addEdge(self, f, t, cap, cost):
        # [to, cap, cost, rev]
        self.G[f].append([t, cap, cost, len(self.G[t])])
        self.G[t].append([f, 0, -cost, len(self.G[f])-1])

    def minCostFlow(self, s, t, f):
        n = self.n
        G = self.G
        prevv = [0]*n; preve = [0]*n
        INF = 1<<63

        res = 0
        while f:
            dist = [INF]*n
            dist[s] = 0
            update = 1
            while update:
                update = 0
                for v in range(n):
                    if dist[v] == INF:
                        continue
                    gv = G[v]
                    for i in range(len(gv)):
                        to, cap, cost, rev = gv[i]
                        if cap > 0 and dist[v] + cost < dist[to]:
                            dist[to] = dist[v] + cost
                            prevv[to] = v; preve[to] = i
                            update = 1
            if dist[t] == INF:
                return -1

            d = f; v = t
            while v != s:
                d = min(d, G[prevv[v]][preve[v]][1])
                v = prevv[v]
            f -= d
            res += d * dist[t]
            v = t
            while v != s:
                e = G[prevv[v]][preve[v]]
                e[1] -= d
                G[v][e[3]][1] += d
                v = prevv[v]
        return res

def Dijkstra(G,s):
  done=[False]*len(G)
  inf=10**20
  C=[inf]*len(G)
  C[s]=0
  h=[]
  heappush(h,(0,s))
  while h:
    x,y=heappop(h)
    if done[y]:
      continue
    done[y]=True
    for v in G[y]:
      if C[v[1]]>C[y]+v[0]:
        C[v[1]]=C[y]+v[0]
        heappush(h,(C[v[1]],v[1]))
  return C

def input():
  return sys.stdin.readline()[:-1]

def solve(test):
  K, N, M = map(int, input().split())
  A = list(map(int, input().split()))
  B = list(map(int, input().split()))
  G=[[] for _ in range(N)]
  for _ in range(M):
    a,b,c=map(int,input().split())
    G[a-1].append((c,b-1))
    G[b-1].append((c,a-1))
  min_cost_flow = MinCostFlow(K+N+2)
  for i in range(K):
    min_cost_flow.addEdge(0, i+1, 1, 0)
    D=Dijkstra(G,A[i]-1)
    for j in range(N):
      min_cost_flow.addEdge(i+1, K+j+1, 1, D[j])
  for i in range(N):
    min_cost_flow.addEdge(K+i+1, K+N+1, B[i], 0)
  print(min_cost_flow.minCostFlow(0, K+N+1, K))

def random_input():
  from random import randint,shuffle
  N=randint(1,10)
  M=randint(1,N)
  A=list(range(1,M+1))+[randint(1,M) for _ in range(N-M)]
  shuffle(A)
  return (" ".join(map(str, [N,M]))+"\n"+" ".join(map(str, A))+"\n")*3

def simple_solve():
  return []

def main(test):
  if test==0:
    solve(0)
  elif test==1:
    sys.stdin = io.StringIO(_INPUT)
    case_no=int(input())
    for _ in range(case_no):
      solve(0)
  else:
    for i in range(1000):
      sys.stdin = io.StringIO(random_input())
      x=solve(1)
      y=simple_solve()
      if x!=y:
        print(i,x,y)
        print(*[line for line in sys.stdin],sep='')
        break

#0:提出用、1:与えられたテスト用、2:ストレステスト用
main(0)
0