結果

問題 No.2604 Initial Motion
ユーザー katonyonkokatonyonko
提出日時 2024-01-12 23:57:30
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,273 bytes
コンパイル時間 240 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 96,540 KB
最終ジャッジ日時 2024-09-28 00:28:58
合計ジャッジ時間 7,479 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
62,432 KB
testcase_01 AC 42 ms
55,648 KB
testcase_02 AC 54 ms
64,936 KB
testcase_03 AC 236 ms
77,528 KB
testcase_04 AC 227 ms
77,136 KB
testcase_05 AC 225 ms
77,312 KB
testcase_06 AC 219 ms
77,172 KB
testcase_07 AC 225 ms
77,732 KB
testcase_08 AC 232 ms
77,440 KB
testcase_09 AC 297 ms
77,368 KB
testcase_10 AC 225 ms
77,320 KB
testcase_11 AC 242 ms
77,200 KB
testcase_12 AC 296 ms
77,264 KB
testcase_13 TLE -
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 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()))
  min_cost_flow = MinCostFlow(N+2)
  for _ in range(M):
    a,b,c=map(int,input().split())
    min_cost_flow.addEdge(a, b, 10**5, c)
    min_cost_flow.addEdge(b, a, 10**5, c)
  for i in range(K):
    min_cost_flow.addEdge(0, A[i], 1, 0)
  for i in range(N):
    min_cost_flow.addEdge(i+1, N+1, B[i], 0)
  print(min_cost_flow.minCostFlow(0, 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