結果
| 問題 |
No.2604 Initial Motion
|
| コンテスト | |
| ユーザー |
katonyonko
|
| 提出日時 | 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 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | TLE * 1 -- * 38 |
ソースコード
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)
katonyonko