s_id stringlengths 10 10 | p_id stringlengths 6 6 | u_id stringlengths 10 10 | date stringlengths 10 10 | language stringclasses 1
value | original_language stringclasses 11
values | filename_ext stringclasses 1
value | status stringclasses 1
value | cpu_time stringlengths 1 5 | memory stringlengths 1 7 | code_size stringlengths 1 6 | code stringlengths 1 539k |
|---|---|---|---|---|---|---|---|---|---|---|---|
s791421264 | p03805 | u583010173 | 1524642778 | Python | Python (3.4.3) | py | Runtime Error | 62 | 3064 | 480 | # -*- coding: utf-8 -*-
n, m = [int(x) for x in input().split()]
adj = [[0]*m for i in range(m)]
visited = [0]*m
for i in range(m):
a, b = [int(x) for x in input().split()]
adj[a-1][b-1] = 1
adj[b-1][a-1] = 1
ans = 0
def dfs(v):
global ans
if sum(visited) == m:
ans += 1
for i in range(m):
if adj[v-1][i] == 1 and visited[i] == 0:
visited[i] = 1
dfs(i+1)
visited[i] = 0
visited[0] = 1
dfs(1)
print(ans) |
s302593404 | p03805 | u583010173 | 1524642602 | Python | Python (3.4.3) | py | Runtime Error | 56 | 3064 | 463 | # -*- coding: utf-8 -*-
n, m = [int(x) for x in input().split()]
adj = [[0]*m for i in range(m)]
visited = [0]*m
for i in range(m):
a, b = [int(x) for x in input().split()]
adj[a-1][b-1] = 1
adj[b-1][a-1] = 1
ans = 0
def dfs(v):
global ans
if sum(visited) == m:
ans += 1
for i in range(m):
if adj[v-1][i] == 1 and visited[i] == 0:
visited[i] = 1
dfs(i)
visited[i] = 0
dfs(1)
print(ans) |
s059310216 | p03805 | u136090046 | 1521997036 | Python | Python (3.4.3) | py | Runtime Error | 48 | 3064 | 656 | import sys
n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
if m == 0 or m == 1:
print(1)
sys.exit()
adjacency_list = [[] for x in range(n + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s474777228 | p03805 | u653005308 | 1521663235 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1013 | n,m=map(int,input().split())
path=[[[0]*n]*n]
for i in range(m):
a,b=map(int,input().split())
path[a][b]=1
path[b][a]=1#隣接グラフを作成
visited=[[0]*n]#どこを通ったか記憶
visited[0]=1#1は最初に通る
def dfs(position,visited,n):#深さ優先探索
all_visited=1
for i in range(n):
if visited[i]==0:
all_visited=0#行ったことがない点があれば0
break#どこかで見つかったらループ終了
if all_visited==1:
return 1#全部回れたら1回カウント
count=0#経路の個数をカウント
for next in range(n):#次に移動する点
if path[position][next]==0:
continue#道がなければ次へ
if visited[next]==1:
continue#行ったことがあれば次へ
visited[next]=1
count+=dfs(next,visited,n)
visited[i]=0#行った記録を消去して前のループに戻る
visited[position]=0
return count
print(0,n,visited) |
s055026138 | p03805 | u653005308 | 1521663029 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1033 | n,m=map(int,input().split())
path=[[[0]*(n+1)]*(n+1)]
for i in range(m):
a,b=map(int,input().split())
path[a][b]=1
path[b][a]=1#隣接グラフを作成
visited=[[0]*(n+1)]#どこを通ったか記憶
visited[1]=1#1は最初に通る
def dfs(position,visited,n):#深さ優先探索
all_visited=1
for i in range(1,n+1):
if visited[i]==0:
all_visited=0#行ったことがない点があれば0
break#どこかで見つかったらループ終了
if all_visited==1:
return 1#全部回れたら1回カウント
count=0#経路の個数をカウント
for next in range(1,n+1):#次に移動する点
if path[position][next]==0:
continue#道がなければ次へ
if visited[next]==1:
continue#行ったことがあれば次へ
visited[next]=1
count+=dfs(next,visited,n)
visited[i]=0#行った記録を消去して前のループに戻る
visited[position]=0
return count
print(1,n,visited) |
s139028504 | p03805 | u653005308 | 1521420370 | Python | Python (3.4.3) | py | Runtime Error | 20 | 3064 | 691 | def dfs(v,n,visited):#深さ優先探索
all_visited=1#全ての点を通ったか調べる
for i in range(n):
if visited[i]==0:
all_visited=0
if all_visited==1:
return 1
count=0
for i in range(n):
if path[v][i]!=0:
continue
if visited[i]==1:
continue
visited[i]=1
count+=dfs(i,n,visited)
visited[i]=0
return count
n,m=map(int,input().split())
path=[[0]*n for i in range(n)]
for i in range(n):
a,b=map(int,input().split())
path[a-1][b-1]=1
path[b-1][a-1]=1#隣接していれば1
visited=[0]*n
visited[0]=1#1は初めに通る
print(dfs(0,n,visited))
|
s879995295 | p03805 | u429843511 | 1521255765 | Python | Python (3.4.3) | py | Runtime Error | 38 | 3064 | 652 | nmax = 8
def dfs(v, N, visited):
all_visited=True
for i in range(N):
if visited[i]==False:
all_visited=False
if all_visited==True:
return 1
ret=0
for i in range(N):
if graph[v][i]==False:
continue
if visited[i]==True:
continue
visited[i]=True
ret+=dfs(i,N,visited)
visited[i]=False
return ret
N, M = map(int, input().split())
graph = [[False]*M for i in range(N)]
for i in range(M):
A, B = map(int, input().split())
graph[A-1][B-1]=True
graph[B-1][A-1]=True
visited = [False]*N
visited[0]=True
print(dfs(0,N,visited)) |
s673693607 | p03805 | u136090046 | 1521252732 | Python | Python (3.4.3) | py | Runtime Error | 47 | 3064 | 656 | import sys
n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
if m == 0 or m == 1:
print(1)
sys.exit()
adjacency_list = [[] for x in range(m + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s398385467 | p03805 | u136090046 | 1521252511 | Python | Python (3.4.3) | py | Runtime Error | 49 | 3064 | 645 | import sys
n, m = map(int, input().split())
if m == 0:
print(1)
sys.exit()
array = [[int(x) for x in input().split()] for x in range(m)]
adjacency_list = [[] for x in range(m + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s244848467 | p03805 | u136090046 | 1521252342 | Python | Python (3.4.3) | py | Runtime Error | 49 | 3064 | 638 | import sys
sys.setrecursionlimit(100000)
n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
adjacency_list = [[] for x in range(m + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s750495172 | p03805 | u136090046 | 1521252255 | Python | Python (3.4.3) | py | Runtime Error | 50 | 3064 | 594 | n, m = map(int, input().split())
array = [[int(x) for x in input().split()] for x in range(m)]
adjacency_list = [[] for x in range(m + 1)]
for i in array:
adjacency_list[i[0]].append(i[1])
adjacency_list[i[1]].append(i[0])
searched = [False for x in range(m+1)]
def calc(start, maze, searched):
res = 0
if searched[start]:
return 0
searched[start] = True
if searched.count(True) == n:
return 1
else:
for next in maze[start]:
res += calc(next, maze, list(searched))
return res
print(calc(1, adjacency_list, searched))
|
s671305291 | p03805 | u136090046 | 1520202801 | Python | Python (3.4.3) | py | Runtime Error | 43 | 3064 | 772 | import sys
sys.setrecursionlimit(1000000)
def judge(now, edges, searched):
pattern = 0
if searched[now] == "searched":
return 0
searched[now] = "searched"
if searched.count("searched") == len(searched) - 1:
return 1
for next in edges[now]:
pattern += judge(next, edges, list(searched))
return pattern
def main():
n, m = map(int, input().split())
edges = [[int(x) for x in input().split()] for x in range(m)]
adjacent_edges = [[] for x in range(m + 1)]
for edge in edges:
adjacent_edges[edge[0]].append(edge[1])
adjacent_edges[edge[1]].append(edge[0])
searched = ["yet" for x in range(n + 1)]
print(judge(1, adjacent_edges, searched))
if __name__ == '__main__':
main()
|
s864081975 | p03805 | u136090046 | 1520202704 | Python | Python (3.4.3) | py | Runtime Error | 44 | 3064 | 727 | def judge(now, edges, searched):
pattern = 0
if searched[now] == "searched":
return 0
searched[now] = "searched"
if searched.count("searched") == len(searched) - 1:
return 1
for next in edges[now]:
pattern += judge(next, edges, list(searched))
return pattern
def main():
n, m = map(int, input().split())
edges = [[int(x) for x in input().split()] for x in range(m)]
adjacent_edges = [[] for x in range(m + 1)]
for edge in edges:
adjacent_edges[edge[0]].append(edge[1])
adjacent_edges[edge[1]].append(edge[0])
searched = ["yet" for x in range(n + 1)]
print(judge(1, adjacent_edges, searched))
if __name__ == '__main__':
main()
|
s971961629 | p03805 | u143492911 | 1519357606 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3060 | 432 | import numpy as np
n,m=map(int,input().split())
if m==0:
print(0)
exit()
ans=[[] for i in range(n)]
for i in range(m):
a,b=map(int,input().split())
ans[a-1].append((b-1))
ans[b-1].append((a-1))
v=[]
def dfs(now,visited=[]):
global v
if len(visited)==n:
v.append([visited])
return
for j in ans[now]:
for j not in visited:
dfs(j,visited+[j])
dfs(0,[0])
print(len(v)) |
s244131116 | p03805 | u867069435 | 1518485418 | Python | Python (3.4.3) | py | Runtime Error | 163 | 13144 | 454 | import numpy as np
n, m = map(int, input().split())
if m == 0:
print(0)
exit()
r = [[] for i in range(m)]
for i in range(m):
f, s = map(int, input().split())
r[f-1].append(s - 1)
r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
global ans
if len(visited) == n:
ans.append([visited])
return
for j in r[now]:
if j not in visited:
dfs(j, visited + [j])
dfs(0, [0])
print(len(ans)) |
s390212401 | p03805 | u867069435 | 1518485374 | Python | Python (3.4.3) | py | Runtime Error | 161 | 13140 | 483 | import numpy as np
n, m = map(int, input().split())
r = [[] for i in range(m)]
for i in range(m):
f, s = map(int, input().split())
r[f-1].append(s - 1)
r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
try:
global ans
if len(visited) == n:
ans.append([visited])
return
for j in r[now]:
if j not in visited:
dfs(j, visited + [j])
except:
return
dfs(0, [0])
print(len(ans)) |
s420596562 | p03805 | u867069435 | 1518485249 | Python | Python (3.4.3) | py | Runtime Error | 163 | 13120 | 419 | import numpy as np
n, m = map(int, input().split())
r = [[] for i in range(m)]
for i in range(m):
f, s = map(int, input().split())
r[f-1].append(s - 1)
r[s-1].append(f - 1)
ans = []
def dfs(now, visited = []):
global ans
if len(visited) == n:
ans.append([visited])
return
for j in r[now]:
if j not in visited:
dfs(j, visited + [j])
dfs(0, [0])
print(len(ans)) |
s182471661 | p03805 | u621935300 | 1513140930 | Python | Python (2.7.6) | py | Runtime Error | 16 | 2940 | 497 | import itertools
import collections
N,M=map(int,raw_input().split())
#print N,M
a=[tuple(map(int,raw_input().split())) for i in range(N)]
#print a
b=list(itertools.combinations(a,N-1))
#print b
#print collections.Counter(b[0])
cnt=0
for i in b:
c=[]
for j,k in i:
#print j,k
c.append(j)
c.append(k)
#print c
t_c=collections.Counter(c)
t_c_v=(collections.Counter(c)).values()
#print t_c,t_c_v
if t_c[1]==1:
for m in t_c_v:
if m>=3:
break
else:
cnt+=1
print cnt
|
s082044547 | p03805 | u839537730 | 1501446983 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 523 | N, M = list(map(int, input().split(" ")))
graph = [list() for i in range(N+1)]
for i in range(M):
a, b = list(map(int, input().split(" ")))
graph[a].append(b)
graph[b].append(a)
def DFS(node, prev, visited):
visited.append(node)
if len(visited) == N:
return 1
res = 0
for edge in graph[node]:
if edge == prev:
continue
elif edge in visited:
continue
res += dfs(edge, node, visited)
return res
print(DFS(1, 0, [])) |
s646123245 | p03805 | u839537730 | 1501446929 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 523 | N, M = list(map(int, input().split(" ")))
graph = [list() for i in range(N+1)]
for i in range(M):
a, b = list(map(int, input().split(" ")))
graph[a].append(b)
graph[b].append(a)
def DFS(node, prev, visited):
visited.append(node)
if len(visited) == N:
return 1
res = 0
for edge in graph[node]:
if edge == prev:
continue
elif edge in visited:
continue
res += dfs(edge, node, visited)
return res
print(DFS(1, 0, [])) |
s856309727 | p03805 | u946386741 | 1496241952 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3188 | 1188 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(lambda x: int(x) - 1, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0]].append(l[i][1])
lis[l[i][1]].append(l[i][0])
# 隣接行列
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
# 再起関数
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now)
for node in nextnodes:
if node in visited:
continue
que.append([node, lis[node], visited[:]])
print(len(list(filter(lambda i : len(i) is N, ends)))) |
s702828872 | p03805 | u946386741 | 1496105287 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1191 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
lis[l[i][1] - 1].append(l[i][0])
# 隣接行列
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
# 再起関数
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i : len(i) is N, ends)))) |
s237668979 | p03805 | u946386741 | 1496105226 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1190 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
lis[l[i][1] - 1].append(l[i][0])
# 隣接行列
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
# 再起関数
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i: len(i) is 3, ends)))) |
s527979494 | p03805 | u946386741 | 1496105145 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1200 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
lis[l[i][1] - 1].append(l[i][0])
print(lis)
# 隣接行列
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
# 再起関数
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i: len(i) is 3, ends)))) |
s784866310 | p03805 | u946386741 | 1496105074 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1201 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
lis[l[i][1] - 1].append(l[i][0])
print(lis)
# 隣接行列
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
# 再起関数
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i: len(i) is 3, ends))))
|
s043035655 | p03805 | u946386741 | 1496104939 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1168 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
lis[l[i][1] - 1].append(l[i][0])
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
ends = []
que = []
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i: len(i) is 3, ends))))
|
s669963903 | p03805 | u946386741 | 1496104635 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1192 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
if l[i][1] == N:
lis[l[i][1] - 1].append(l[i][0])
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
ends = []
que = []
# def travel(now, nextnodes, visited):
# ends.append(visited)
# visited.append(now + 1)
# for node in nextnodes:
# if node in visited:
# continue
# travel(node - 1, lis[node - 1], visited[:])
#
#
# travel(0, lis[0], [])
que = [[0, lis[0], []]]
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now + 1)
for node in nextnodes:
if node in visited:
continue
que.append([node - 1, lis[node - 1], visited[:]])
print(len(list(filter(lambda i: len(i) is 3, ends)))) |
s776108862 | p03805 | u946386741 | 1496104328 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 896 | N, M = map(int, input().split())
l = []
for i in range(N):
l.append(list(map(int, input().split())))
lis = [[] for i in range(N)]
for i in range(N):
lis[l[i][0] - 1].append(l[i][1])
if l[i][1] == N:
lis[l[i][1] - 1].append(l[i][0])
# list = [[0 for c in range(M)] for r in range(N)]
#
# for i in range(M):
# tmp_n, tmp_m = map(int, input().split())
# list[tmp_n - 1][tmp_m - 1] = 1
#
#
# def dot(index1, index2):
# results = [[0 for c in range(M)] for r in range(N)]
# for (i, j) in zip(index1, zip(*index2)):
# print(i,j)
# return results
ends =[]
def travel(now, nextnodes, visited):
ends.append(visited)
visited.append(now+1)
for node in nextnodes:
if node in visited:
continue
travel(node-1, lis[node-1], visited[:])
travel(0, lis[0], [])
print(len(list(filter(lambda i : len(i) is 3, ends)))) |
s899036268 | p03805 | u820351940 | 1495428004 | Python | Python (3.4.3) | py | Runtime Error | 42 | 5236 | 653 | n, m = map(int, input().split())
ab = [list(map(int, input().split())) for x in range(m)]
abmap = {}
for a, b in ab:
atarget = abmap.get(a)
if atarget == None:
abmap[a] = []
abmap[a].append(b)
btarget = abmap.get(b)
if btarget == None:
abmap[b] = []
abmap[b].append(a)
que = [[1, abmap[1], []]]
ends = []
while que:
now, nextnodes, visited = que.pop()
ends.append(visited)
visited.append(now)
for node in nextnodes:
if node in visited:
continue
mvisited = visited[:]
que.append([node, abmap[node], mvisited])
print(len(filter(lambda x: len(x) == n, ends)))
|
s202737357 | p03805 | u556160473 | 1491095550 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 418 | n,m = map(int,input().split(' '))
bar = [[] for _ in range(m)]
for i in range(n):
a,b = map(lambda x:int(x)-1,input().split(' '))
bar[a].append(b)
bar[b].append(a)
result = 0
def search(root=[0]):
global result
if len(root) >= n:
result += 1
return
nxt = set(bar[root[-1]])
nxt = nxt - set(root)
for nxt_ in nxt:
search(root+[nxt_])
search()
print(result) |
s714900718 | p03805 | u556160473 | 1491095472 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 403 | n,m = map(int,input().split(' '))
bar = [[] for _ in range(m)]
for i in range(n):
a,b = map(lambda x:int(x)-1,input().split(' '))
bar[a].append(b)
bar[b].append(a)
result = 0
def search(root=[0]):
global result
if len(root) >= n:
result += 1
nxt = set(bar[root[-1]])
nxt = nxt - set(root)
for nxt_ in nxt:
search(root+[nxt_])
search()
print(result) |
s243644626 | p03805 | u451987205 | 1489883992 | Python | Python (2.7.6) | py | Runtime Error | 11 | 2696 | 573 | num = list(map(int,raw_input().split()))
point = num[0]
line = num[1]
d = {}
for i in range (point):
d[i+1] = []
for i in range(point):
pair = list(map(int,raw_input().split()))
d[pair[0]].append(pair[1])
d[pair[1]].append(pair[0])
old_list = []
new_list = [[1]]
for i in range (point-1):
old_list = new_list[:]
new_list =[]
for j in old_list:
for k in d[j[-1]]:
if k in j:
pass
else:
j.append(k)
new_list.append(j[:])
j.pop()
print len (new_list)
|
s295419836 | p03805 | u125205981 | 1488423243 | Python | Python (3.4.3) | py | Runtime Error | 25 | 3188 | 595 | import itertools
def path(n):
l = len(n)
i = 0
while i < l - 1:
a = n[i]
b = n[i + 1]
if a < b:
c = (a, b)
else:
c = (b, a)
for m in C:
if m == c:
break
else:
return False
i += 1
return True
N, M = map(int, input().split())
C = []
i = 0
while i < N:
C += [tuple(map(int, input().split()))]
i += 1
i = 0
l = range(1, N + 1)
for n in itertools.permutations(l, N):
if n[0] != 1:
break
if path(n) == True:
i += 1
print(str(i)) |
s146658656 | p03805 | u125205981 | 1488423166 | Python | Python (3.4.3) | py | Runtime Error | 34 | 3188 | 608 | import itertools
def path(n):
l = len(n)
i = 0
while i < l - 1:
a = n[i]
b = n[i + 1]
if a < b:
c = (a, b)
else:
c = (b, a)
for m in C:
if m == c:
break
else:
return False
i += 1
return True
N, M = map(int, input().split())
C = []
i = 0
while i < N:
C += [tuple(map(int, input().split()))]
i += 1
i = 0
l = range(1, N + 1)
for n in itertools.permutations(l, N):
print(n)
if n[0] != 1:
break
if path(n) == True:
i += 1
print(str(i)) |
s413951417 | p03805 | u125205981 | 1488422444 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 590 | import itertools
def path(n):
l = len(n)
i = 0
while i < l - 1:
a = n[i]
b = n[i + 1]
if a < b:
c = (a, b)
else:
c = (b, a)
for m in C:
if m == c:
break
else:
return False
i += 1
return True
N, M = map(int, input().split())
C = []
i = 0
while i < N:
C += [tuple(map(int, input().split()))]
i += 1
i = 0
l = range(1, N + 1)
for n in itertools.permutations(l, N):
if n[0] != 1:
break
if path(n) == True:
i += 1
print(i) |
s552589374 | p03805 | u526532903 | 1487727998 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 892 | #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;
int n;
int ans = 0;
void dfs(int c, int u, vector<bool> used, bool M[10][10]){
if(u == n - 1){
ans++;
return;
}
rep(i,n){
if(M[c][i] == 1 && used[i] == false){
used[c] = true;
dfs(i,u + 1,used,M);
}
}
}
int main(){
int m;
vector<bool> used(10);
bool M[10][10] = {0};
cin >> n >> m;
rep(i,m){
int a,b;
cin >> a >> b;
a--; b--;
M[a][b] = M[b][a] = 1;
}
dfs(0,0,used,M);
cout << ans << endl;
}
|
s941987335 | p03805 | u526532903 | 1487727816 | Python | Python (3.4.3) | py | Runtime Error | 17 | 2940 | 837 | #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;
int n;
int ans = 0;
bool M[10][10] = {0};
void dfs(int c, int u, vector<bool> used){
if(u == n - 1) ans++;
rep(i,n){
if(M[c][i] == 1 && used[i] == false){
used[c] = true;
dfs(i,u + 1,used);
}
}
}
int main(){
int m;
vector<bool> used(10);
cin >> n >> m;
rep(i,m){
int a,b;
cin >> a >> b;
a--; b--;
M[a][b] = M[b][a] = 1;
}
dfs(0,0,used);
cout << ans << endl;
}
|
s247137104 | p03805 | u526532903 | 1487727748 | Python | Python (3.4.3) | py | Runtime Error | 18 | 2940 | 825 | #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;
int n,M[10][10];
int ans = 0;
void dfs(int c, int u, vector<bool> used){
if(u == n - 1) ans++;
rep(i,n){
if(M[c][i] == 1 && used[i] == false){
used[c] = true;
dfs(i,u + 1,used);
}
}
}
int main(){
int m;
vector<bool> used(10);
cin >> n >> m;
rep(i,m){
int a,b;
cin >> a >> b;
a--; b--;
M[a][b] = M[b][a] = 1;
}
dfs(0,0,used);
cout << ans << endl;
}
|
s653854792 | p03805 | u252805217 | 1487540045 | Python | Python (3.4.3) | py | Runtime Error | 24 | 3064 | 484 | from itertools import permutations as perm
n, m = map(int, input().split())
path = [[False] * n for _ in range(n)]
for i in range(n):
path[i][i] = True
for _ in range(n):
ai, bi = map(int, input().split())
a, b = ai - 1, bi -1
path[a][b] = True
path[b][a] = True
ans = 0
for ps in perm(range(1, n)):
fst = ps[0]
if not path[0][fst]:
continue
check = [path[i][j] for (i, j) in zip(ps, ps[1:])]
if all(check):
ans += 1
print(ans) |
s974104621 | p03805 | u098572984 | 1487206301 | Python | Python (3.4.3) | py | Runtime Error | 20 | 3444 | 1395 | _N,_M=input().split()
N = int(_N)
M = int(_M)
l = []
for i in range(0,M):
_p, _q = input().split()
l.append((int(_p),int(_q)))
_A,_B=zip(l)
A=_A+_B
B=_B+_A
def gen_get_to(_fr):
global A
global B
global nodes
global step
for itr in range(0,len(A)):
if A[itr]==_fr:
if not B[itr] in nodes[0:step]:
yield B[itr]
def move(): #returns whether "move" succeeded or not
global nodes
global possible_memo
global step
global counter
global N
global end_flag
try:
_=next(possible_memo[step])
nodes[step+1]=_
step+=1
#print("success move to {}".format(nodes[step]))
#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==N-1:
counter+=1
#print('REACHED GOAL!!!')
possible_memo[step]=gen_get_to(nodes[step])
except StopIteration:
step-=1
#print("failed to move")
if not step==-1:
pass#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==-1:
#print("I fell from the first node")
end_flag=True
nodes=[0]*N
possible_memo=[0]*N
counter=0
step=0
nodes[step]=1
possible_memo[step]=gen_get_to(nodes[0])
end_flag=False
while not end_flag:
move()
print(counter) |
s071346904 | p03805 | u098572984 | 1487206165 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3444 | 1393 | _N,_M=input().split()
N = int(_N)
M = int(_M)
l = []
for i in range(0,M):
_p, _q = input().split
l.append((int(_p),int(_q)))
_A,_B=zip(l)
A=_A+_B
B=_B+_A
def gen_get_to(_fr):
global A
global B
global nodes
global step
for itr in range(0,len(A)):
if A[itr]==_fr:
if not B[itr] in nodes[0:step]:
yield B[itr]
def move(): #returns whether "move" succeeded or not
global nodes
global possible_memo
global step
global counter
global N
global end_flag
try:
_=next(possible_memo[step])
nodes[step+1]=_
step+=1
#print("success move to {}".format(nodes[step]))
#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==N-1:
counter+=1
#print('REACHED GOAL!!!')
possible_memo[step]=gen_get_to(nodes[step])
except StopIteration:
step-=1
#print("failed to move")
if not step==-1:
pass#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==-1:
#print("I fell from the first node")
end_flag=True
nodes=[0]*N
possible_memo=[0]*N
counter=0
step=0
nodes[step]=1
possible_memo[step]=gen_get_to(nodes[0])
end_flag=False
while not end_flag:
move()
print(counter) |
s242354167 | p03805 | u098572984 | 1487206031 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1330 | N,M=input().split()
l = []
for i in range(0,M):
l.append(input())
_A,_B=zip(l)
A=_A+_B
B=_B+_A
def gen_get_to(_fr):
global A
global B
global nodes
global step
for itr in range(0,len(A)):
if A[itr]==_fr:
if not B[itr] in nodes[0:step]:
yield B[itr]
def move(): #returns whether "move" succeeded or not
global nodes
global possible_memo
global step
global counter
global N
global end_flag
try:
_=next(possible_memo[step])
nodes[step+1]=_
step+=1
#print("success move to {}".format(nodes[step]))
#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==N-1:
counter+=1
#print('REACHED GOAL!!!')
possible_memo[step]=gen_get_to(nodes[step])
except StopIteration:
step-=1
#print("failed to move")
if not step==-1:
pass#print("map: {}".format(nodes[0:step+1]))
#print("I am at {}th node".format(step+1))
if step==-1:
#print("I fell from the first node")
end_flag=True
nodes=[0]*N
possible_memo=[0]*N
counter=0
step=0
nodes[step]=1
possible_memo[step]=gen_get_to(nodes[0])
end_flag=False
while not end_flag:
move()
print(counter) |
s434689462 | p03805 | u637175065 | 1486914152 | Python | Python (3.4.3) | py | Runtime Error | 53 | 5420 | 739 | import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time
sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def main():
n,m = LI()
r = 0
d = collections.defaultdict(set)
for _ in range(m):
a,b = LI()
d[a].add(b)
d[b].add(a)
for a in itertools.permutations(range(2,n+1)):
if a[0] not in d[1]:
continue
f = True
for i in range(m-2):
if a[i] not in d[a[i+1]]:
f = False
break
if f:
r += 1
return r
print(main())
|
s462087390 | p03805 | u425351967 | 1486867303 | Python | Python (3.4.3) | py | Runtime Error | 28 | 3064 | 502 | import itertools
N, M = [int(n) for n in input().split()]
path = [[False for i in range(M)]for j in range(M)]
for i in range(M):
a, b = [int(n)-1 for n in input().split()]
path[a][b] = True
path[b][a] = True
res = 0
for way in itertools.permutations(range(1,N), N-1):
exist = True
if path[0][way[0]] == False:
exist = False
for n in range(N-2):
if path[way[n]][way[n+1]] == False:
exist = False
if exist == True:
res += 1
print(res)
|
s222264558 | p03805 | u065683101 | 1486867264 | Python | Python (3.4.3) | py | Runtime Error | 51 | 4212 | 572 | def read():
return int(input())
def reads(sep=None):
return list(map(int, input().split(sep)))
def main():
n, m = reads();
g = [set() for _ in range(m)]
for _ in range(m):
a, b = reads()
g[a - 1].add(b - 1)
g[b - 1].add(a - 1)
count = 0
q = [(0, [])]
while q:
i, p = q.pop(0)
if len(p) == (n-1):
count += 1
for e in g[i]:
if e in p:
continue
pp = p[:]
pp.append(i)
q.append((e, pp))
print(count)
main()
|
s172128905 | p03806 | u911449886 | 1600606256 | Python | PyPy3 (7.3.0) | py | Runtime Error | 777 | 132380 | 1250 | from copy import deepcopy
def getval():
n,ma,mb = map(int,input().split())
p = [list(map(int,input().split())) for i in range(n)]
return n,ma,mb,p
def main(n,ma,mb,p):
dp = [[[-1 for i in range(401)] for i in range(401)]]
dp[0][0][0] = 0
for i in range(n):
temp = deepcopy(dp[-1])
temp[0][0] = 0
cur = p[i]
prev = dp[-1]
for a in range(401):
for b in range(401):
if cur[0]>a or cur[1]>b:
continue
if prev[a-cur[0]][b-cur[1]]==-1:
continue
elif prev[a][b]==-1:
temp[a][b] = prev[a-cur[0]][b-cur[1]] + cur[2]
#print(i,a,b,temp[a][b],prev[a-cur[0]][b-cur[1]])
else:
temp[a][b] = min(prev[a][b], prev[a-cur[0]][b-cur[1]] + cur[2])
#dp.append(temp)
ans = -1
for i in range(1,11):
if i*ma>10 or i*mb>10:
break
if dp[n][i*ma][i*mb]!=-1:
if ans==-1:
ans = dp[n][i*ma][i*mb]
else:
ans = min(ans,dp[n][i*ma][i*mb])
print(ans)
print(dp[n])
if __name__=="__main__":
n,ma,mb,p = getval()
main(n,ma,mb,p) |
s715934471 | p03806 | u787059958 | 1600399899 | Python | Python (3.8.2) | py | Runtime Error | 28 | 9032 | 645 | import math
N, Ma, Mb = map(int, input().split())
cost = [[0] * 10 for _ in range(10)]
for _ in range(N):
a, b, c = map(int, input().split())
a -= 1
b -= 1
cost[a][b] = c
min_cost = 10 ** 9 + 7
for k in range(N):
for i in range(N):
for j in range(N):
g = math.gcd(i + 1 + k + 1, k + 1 + j + 1)
if (i + 1 + k + 1) // g == Ma and (k + 1 + j + \
1) // g == Mb and cost[i][k] + cost[k][j] != 0:
# print(cost[i][k] + cost[k][j])
min_cost = min(min_cost, cost[i][k] + cost[k][j])
if min_cost == 10 ** 9 + 7:
print(-1)
else:
print(min_cost)
|
s193798375 | p03806 | u860829879 | 1599623819 | Python | Python (3.8.2) | py | Runtime Error | 710 | 107824 | 1290 | import numpy as np
from numba import njit
@njit
def main(n,ma,mb,abc):
inf=10**5
cost=inf*np.ones((401,401))
maxa=np.sum(np.transpose[abc][0])
maxb=np.sum(np.transpose[abc][1])
left=4*n//8
right=n
cnt=right-left
for i in range(2**cnt):
ta=0
tb=0
tc=0
for j in range(cnt):
if (i>>j)&1==1:
a,b,c=abc[left+j]
ta+=a
tb+=b
tc+=c
cost[ta,tb]=min(cost[ta,tb],tc)
ans=inf
for i in range(1,401):
targeta=ma*i
targetb=mb*i
if targeta>maxa or targetb>maxb:
break
for j in range(2**left):
ta=0
tb=0
tc=0
for k in range(left):
if (j>>k)&1==1:
a,b,c=abc[k]
ta+=a
tb+=b
tc+=c
ua=targeta-ta
ub=targetb-tb
if 0<=ua<=400 and 0<=ub<=400 and cost[ua,ub]!=inf:
ans=min(ans,tc+cost[ua,ub])
if ans==inf:
return -1
else:
return int(ans)
n,ma,mb=map(int,input().split())
abc=np.array([list(map(int,input().split())) for _ in range(n)])
print(main(n,ma,mb,abc)) |
s641199614 | p03806 | u074220993 | 1599245994 | Python | Python (3.8.2) | py | Runtime Error | 25 | 9480 | 581 | N, Ma, Mb = map(int, input().split())
Med = [tuple(map(int, input().split())) for _ in range(N)]
INF = 100000
from collections import defaultdict as dd
dp = [dd(lambda:INF) for _ in range(N+1)]#dp[i,j]:Aがiグラム、Bがjグラムの時の最小予算
for n in range(1,N+1):
a,b,c = Med[n-1]
dp[n-1][0,0] = 0
for (i,j),value in dp[n-1].items():
dp[n][i+a,j+b] = min(dp[n-1][i+a,j+b],value+c)
ans = INF
for (i,j),value in dp[N].items():
if i == j == 0:
continue
if i*Mb == j*Ma:
ans = min(ans, value)
print(ans if ans != INF else -1) |
s704777884 | p03806 | u074220993 | 1599244759 | Python | Python (3.8.2) | py | Runtime Error | 26 | 9236 | 527 | N, Ma, Mb = map(int, input().split())
Med = [tuple(map(int, input().split())) for _ in range(N)]
N *= 100
INF = 100000
dp = [[INF]*N+1 for _ in range(N+1)] #dp[i,j]:Aがiグラム、Bがjグラムの時の最小予算
dp[0][0] = 0
from itertools import product as prd
R = lambda x:reversed(range(x,N+1))
for a,b,c in Med:
for i,j in prd(R(a),R(b)):
dp[i][j] = min(dp[i][j], dp[i-a][j-b]+c)
ans = INF
for i,j in prd(R(1),R(1)):
if i*Mb == j*Ma:
ans = min(ans, dp[i][j])
print(ans if ans != INF else -1) |
s550617608 | p03806 | u678167152 | 1597408234 | Python | PyPy3 (7.3.0) | py | Runtime Error | 1021 | 76496 | 1238 | from itertools import groupby, accumulate, product, permutations, combinations
def solve():
ans = float('inf')
N, A, B = map(int, input().split())
mae = [list(map(int, input().split())) for _ in range((N+1)//2)]
usiro = [list(map(int, input().split())) for _ in range(N-(N+1)//2)]
if N==1:
a,b,c = mae[0]
if a*B==b*A:
return c
return -1
mae_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=(N+1)//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += mae[i][0];b += mae[i][1];c += mae[i][2]
mae_lis[a][b] = min(mae_lis[a][b],c)
usiro_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=N-(N+1)//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
usiro_lis[a][b] = min(usiro_lis[a][b],c)
for i in range(1,201):
for j in range(1,201):
c = mae_lis[i][j]
if c==float('inf'):
continue
for k in range(1,201):
if (i+k)*B%A!=0:
continue
l = (i+k)*B//A - j
if l<=0:
continue
ans = min(ans,c+usiro_lis[k][l])
return ans if ans<float('inf') else -1
print(solve()) |
s738360026 | p03806 | u678167152 | 1597408198 | Python | PyPy3 (7.3.0) | py | Runtime Error | 1016 | 76332 | 1230 | from itertools import groupby, accumulate, product, permutations, combinations
def solve():
ans = float('inf')
N, A, B = map(int, input().split())
mae = [list(map(int, input().split())) for _ in range((N+1)//2)]
usiro = [list(map(int, input().split())) for _ in range(N-(N+1)//2)]
if N==1:
a,b,c = mae[0]
if a*B==b*A:
return c
return -1
mae_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=N//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += mae[i][0];b += mae[i][1];c += mae[i][2]
mae_lis[a][b] = min(mae_lis[a][b],c)
usiro_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=N-N//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
usiro_lis[a][b] = min(usiro_lis[a][b],c)
for i in range(1,201):
for j in range(1,201):
c = mae_lis[i][j]
if c==float('inf'):
continue
for k in range(1,201):
if (i+k)*B%A!=0:
continue
l = (i+k)*B//A - j
if l<=0:
continue
ans = min(ans,c+usiro_lis[k][l])
return ans if ans<float('inf') else -1
print(solve()) |
s952460188 | p03806 | u678167152 | 1597408063 | Python | PyPy3 (7.3.0) | py | Runtime Error | 998 | 76492 | 1146 | from itertools import groupby, accumulate, product, permutations, combinations
def solve():
ans = float('inf')
N, A, B = map(int, input().split())
mae = [list(map(int, input().split())) for _ in range(N//2)]
usiro = [list(map(int, input().split())) for _ in range(N-N//2)]
mae_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=N//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += mae[i][0];b += mae[i][1];c += mae[i][2]
mae_lis[a][b] = min(mae_lis[a][b],c)
usiro_lis = [[float('inf')]*201 for _ in range(201)]
for p in product([0,1],repeat=N-N//2):
a,b,c = 0,0,0
for i in range(N//2):
if p[i]==1:
a += usiro[i][0];b += usiro[i][1];c += usiro[i][2]
usiro_lis[a][b] = min(usiro_lis[a][b],c)
for i in range(1,201):
for j in range(1,201):
c = mae_lis[i][j]
if c==float('inf'):
continue
for k in range(1,201):
if (i+k)*B%A!=0:
continue
l = (i+k)*B//A - j
if l<=0:
continue
ans = min(ans,c+usiro_lis[k][l])
return ans if ans<float('inf') else -1
print(solve()) |
s708720777 | p03806 | u843175622 | 1597408021 | Python | PyPy3 (7.3.0) | py | Runtime Error | 102 | 68696 | 678 | import numpy as np
n, ma, mb = map(int, input().split())
item = [tuple(map(int, input().split())) for _ in range(n)]
INF = 5000
dp = np.full((n + 1, 444, 444), INF, dtype=np.int)
dp[0][0][0] = 0
for i in range(n):
for j in range(401):
for k in range(401):
a, b, c = item[i]
dp[i + 1][j + a][k + b] = min(dp[i + 1]
[j + a][k + b], dp[i][j][k] + c)
dp[i + 1][j][k] = min(dp[i + 1][j][k], dp[i][j][k])
ans = INF
for i in range(1, 401):
for j in range(1, 401): # i : j == ma : mb
if ma * j == i * mb:
ans = min(ans, dp[n][i][j])
print(ans if ans != INF else -1)
|
s545450443 | p03806 | u503228842 | 1595363006 | Python | Python (3.8.2) | py | Runtime Error | 76 | 12744 | 793 | N, ma, mb = map(int, input().split())
a = [0]*N
b = [0]*N
c = [0]*N
NMAX = 10
ABMAX = 10
INF = 100000
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
dp = [[[INF]*(NMAX*ABMAX+1)for _ in range(NMAX*ABMAX+1)]for _ in range(N+1)]
dp[0][0][0] = 0
for i in range(N):
for ca in range(NMAX*ABMAX+1):
for cb in range(NMAX*ABMAX+1):
if dp[i][ca][cb] == INF:
continue
dp[i+1][ca][cb] = min(dp[i+1][ca][cb], dp[i][ca][cb])
dp[i+1][ca+a[i]][cb+b[i]
] = min(dp[i+1][ca+a[i]][cb+b[i]], dp[i][ca][cb]+c[i])
ans = INF
for ca in range(1, NMAX*ABMAX+1):
for cb in range(1, NMAX*ABMAX+1):
if ca * mb == cb * ma:
ans = min(ans, dp[N][ca][cb])
print(ans if ans != INF else -1)
|
s223785242 | p03806 | u786020649 | 1595218971 | Python | Python (3.8.2) | py | Runtime Error | 363 | 31916 | 1110 | import sys
import numpy as np
from collections import deque,defaultdict
inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
def main():
N,Ma,Mb=map(int,input().split())
ch=dict()
for _ in range(N):
a,b,c=map(int,input().split())
ch[(a,b)]=c
p=[(-1)*Mb,Ma]
pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
kpch=list(pch.keys())
infty=10**9
X=N*200
dp=[[infty]*(2*X+1) for _ in range(N)]
for i in range(N):
for j in range(2*X+1):
if i==0 and j==X+kpch[0]:
dp[i][j]=pch[kpch[0]]
elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
elif i>0:
dp[i][j]=dp[i-1][j]
# print(kpch)
# print(pch)
# print(dp)
if dp[N-1][X]==infty:
print(-1)
else:
print(dp[N-1][X])
if __name__=='__main__':
main()
|
s593846309 | p03806 | u786020649 | 1595218746 | Python | Python (3.8.2) | py | Runtime Error | 248 | 29372 | 1110 | import sys
import numpy as np
from collections import deque,defaultdict
inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
def main():
N,Ma,Mb=map(int,input().split())
ch=dict()
for _ in range(N):
a,b,c=map(int,input().split())
ch[(a,b)]=c
p=[(-1)*Mb,Ma]
pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
kpch=list(pch.keys())
infty=10**9
X=N*100
dp=[[infty]*(2*X+1) for _ in range(N)]
for i in range(N):
for j in range(2*X+1):
if i==0 and j==X+kpch[0]:
dp[i][j]=pch[kpch[0]]
elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
elif i>0:
dp[i][j]=dp[i-1][j]
# print(kpch)
# print(pch)
# print(dp)
if dp[N-1][X]==infty:
print(-1)
else:
print(dp[N-1][X])
if __name__=='__main__':
main()
|
s097984312 | p03806 | u786020649 | 1595218674 | Python | Python (3.8.2) | py | Runtime Error | 118 | 27144 | 1105 | import sys
import numpy as np
from collections import deque,defaultdict
inp=lambda v,w:v[0]*w[0]+v[1]*w[1]
isprop = lambda a,b: a[0]*b[1]==a[1]*b[0]
cwsum = lambda l:[sum([x[0] for x in l]), sum([x[1] for x in l])]
chmin = lambda a,b:(a,b)[b<a]
# cwsum = lambda l:np.sum(l,axis=0)
def main():
N,Ma,Mb=map(int,input().split())
ch=dict()
for _ in range(N):
a,b,c=map(int,input().split())
ch[(a,b)]=c
p=[(-1)*Mb,Ma]
pch={inp(p,v):ch[v] for v in iter(ch)} #p方向への射影
kpch=list(pch.keys())
infty=30
X=N*4
dp=[[infty]*(2*X+1) for _ in range(N)]
for i in range(N):
for j in range(2*X+1):
if i==0 and j==X+kpch[0]:
dp[i][j]=pch[kpch[0]]
elif i>0 and j>=kpch[i] and j<=2*X+kpch[i]:
dp[i][j]=chmin(dp[i-1][j],dp[i-1][j-kpch[i]]+pch[kpch[i]])
elif i>0:
dp[i][j]=dp[i-1][j]
# print(kpch)
# print(pch)
# print(dp)
if dp[N-1][X]==infty:
print(-1)
else:
print(dp[N-1][X])
if __name__=='__main__':
main()
|
s373287651 | p03806 | u122743999 | 1593919844 | Python | Python (3.8.2) | py | Runtime Error | 29 | 9164 | 565 | import more_itertools
import math
data = []
part = []
cost = math.inf
n = input()
n = n.split()
N = int(n[0])
Ma = int(n[1])
Mb = int(n[2])
for i in range(N):
part.append(i)
tmp = input()
tmp = tmp.split()
tmp = list(map(int, tmp))
data.append(tmp)
part = more_itertools.powerset(part)
for i in part:
a = 0
b = 0
c = 0
for j in i:
a += data[j][0]
b += data[j][1]
c += data[j][2]
if (Ma*b == Mb*a) and cost > c and (c > 0):
cost = c
if cost == math.inf:
print('-1')
else:
print(cost)
|
s631193967 | p03806 | u506549878 | 1593802477 | Python | Python (3.8.2) | py | Runtime Error | 2205 | 9436 | 660 | k, a, b = map(int, input().split())
item = [list(map(int, input().split())) for i in range(k)]
n = len(item)
cost_list = []
for i in range(2 ** n):
bag = []
for j in range(n): # このループが一番のポイント
if ((i >> j) & 1): # 順に右にシフトさせ最下位bitのチェックを行う
bag.append(item[j]) # フラグが立っていたら bag に果物を詰める
drag_a = 0
drag_b = 0
cost = 0
for var in bag:
drag_a += var[0]
drag_b += var[1]
cost += var[2]
if drag_a*b == drag_b*a:
cost_list.append(cost)
cost_list.remove(0)
print(min(cost_list)) |
s209217025 | p03806 | u679390859 | 1592874907 | Python | Python (3.8.2) | py | Runtime Error | 25 | 8924 | 930 | #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<math.h>
#include<limits.h>
#define BIG 1000007
int min(int a,int b){
if(a<b) return a;
else return b;
}
int main(){
int N,Ma,Mb;
scanf("%d %d %d",&N,&Ma,&Mb);
int a[42],b[42],c[42];
for(int i=0;i<N;i++)
scanf("%d %d %d",a+i,b+i,c+i);
int DP[42][402][402];
memset(DP,BIG,sizeof(DP));
DP[0][a[0]][b[0]] = c[0];
for(int i=1;i<N;i++){
for(int j=0;j<402;j++){
for(int k=0;k<402;k++){
if(j-a[i]<0 || k-b[i]<0)
DP[i][j][k] = DP[i-1][j][k];
else
DP[i][j][k]= min(DP[i-1][j][k],DP[i-1][j-a[i]][k-b[i]]+c[i]);
}
}
}
int ans = BIG;
for(int i=1;i*Ma<402 && i*Mb<402;i++){
ans = min(ans,DP[N-1][i*Ma][i*Mb]);
}
if(ans == BIG) printf("-1");
else printf("%d",ans);
} |
s991793142 | p03806 | u679390859 | 1592873678 | Python | Python (3.8.2) | py | Runtime Error | 26 | 9124 | 379 | N,M = map(int,input().split())
path = [[] for i in range(N)]
for _ in range(M):
a,b = map(int,input().split())
path[a-1].append(b-1)
path[b-1].append(a-1)
vis = [0 for i in range(N)]
cnt = 0
def dfs(now,depth):
global cnt
if depth == N: cnt+=1
for new in path[now]:
if vis[new] == 0:
vis[new] = 1
dfs(new,depth+1)
vis[new] = 0
vis[0] = 1
dfs(0,1)
print(cnt)
|
s065628614 | p03806 | u671861352 | 1591941497 | Python | PyPy3 (2.4.0) | py | Runtime Error | 169 | 38384 | 592 | #!python3
LI = lambda: list(map(int, input().split()))
# input
N, Ma, Mb = LI()
ABC = [LI() for _ in range(N)]
INF = 10 ** 5
def main():
w = [[INF] * 401 for _ in range(401)]
w[0][0] = 0
for a, b, c in ABC:
l = w.copy()
for j in range(a, 401):
for k in range(b, 401):
l[j][k] = min(l[j][k], w[j - a][k - b] + c)
w = l
ans = INF
n = 400 // max(Ma, Mb) + 1
for i in range(1, n):
ans = min(ans, w[Ma * i][Mb * i])
ans = -1 if ans == INF else ans
print(ans)
if __name__ == "__main__":
main()
|
s057427180 | p03806 | u591808161 | 1590954330 | Python | PyPy3 (2.4.0) | py | Runtime Error | 172 | 38256 | 839 | import sys
import itertools
import numpy as np
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np.array(list(map(int, input().split())))
def main():
n, ma, mb = MI()
num = ma / mb
buy = [i for i in range(n)]
mylist = [LI() for i in range(n)]
result = []
for j in range(1,len(buy)+1):
for conb in itertools.combinations(buy, j):
tempa = 0
tempb = 0
price = 0
for i in conb:
tempa += mylist[i][0]
tempb += mylist[i][1]
price += mylist[i][2]
if tempa/tempb == num:
result.append(price)
if len(result) == 0:
print(-1)
else:
print(min(result))
main()
|
s897183271 | p03806 | u591808161 | 1590954294 | Python | PyPy3 (2.4.0) | py | Runtime Error | 181 | 38512 | 822 | import sys
import itertools
import numpy as np
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np.array(list(map(int, input().split())))
def main():
n, ma, mb = MI()
num = ma / mb
buy = [i for i in range(n)]
mylist = [LI() for i in range(n)]
result = []
for j in range(1,len(buy)+1):
for conb in itertools.combinations(buy, j):
tempa = 0
tempb = 0
price = 0
for i in conb:
tempa += mylist[i][0]
tempb += mylist[i][1]
price += mylist[i][2]
if tempa/tempb == num:
result.append(price)
if len(result) == 0:
print(-1)
else:
print(min(result))
main() |
s896850114 | p03806 | u591808161 | 1590953973 | Python | PyPy3 (2.4.0) | py | Runtime Error | 169 | 38256 | 853 | import sys
import itertools
import numpy as np
np_array = np.array
input = sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return np_array(map(int, input().split()))
def main():
n, ma, mb = MI()
num = ma / mb
buy = [i for i in range(n)]
mylist = [LI() for i in range(n)]
result = []
for j in range(1,len(buy)+1):
for conb in itertools.combinations(buy, j):
tempa = 0
tempb = 0
price = 0
for i in conb:
tempa += mylist[i][0]
tempb += mylist[i][1]
price += mylist[i][2]
if tempa/tempb == num:
result.append(price)
if len(result) == 0:
print(-1)
else:
print(min(result))
main()
|
s854189784 | p03806 | u703950586 | 1590843175 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2110 | 138100 | 1369 | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
INF = 10**5
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
n,ma,mb = LI()
med = [LI() for _ in range(n)]
h1 = []
h2 = []
for i in range(2 ** (n//2)):
bit = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2):
if i & bit:
a,b,c = med[j]
ta += a
tb += b
tc += c
bit <<= 1
dif = ta * mb - tb * ma
h1.append((dif,tc))
for i in range(2 ** (n - n//2)):
bit = 1
ta = 0
tb = 0
tc = 0
for j in range(n - n//2):
if i & bit:
a,b,c = med[j+n//2]
ta += a
tb += b
tc += c
bit <<= 1
dif = tb * ma - ta * mb
h2.append((dif,tc))
h1.sort()
h2.sort()
i = 0
ans = INF
for h,c in h1:
while h2[i][0] < h:
i += 1
if i >= len(h2): break
else:
while h2[i][0] == h:
if h != 0 and h2[i][0] != 0:
ans = min(ans,c+h2[i][1])
i += 1
continue
break
if ans < INF:
print(ans)
else:
print(-1)
if __name__ == '__main__':
main() |
s624935712 | p03806 | u703950586 | 1590842542 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2110 | 136908 | 1305 | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
INF = 10**5
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
n,ma,mb = LI()
med = [LI() for _ in range(n)]
h1 = []
h2 = []
for i in range(2 ** (n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2):
if i & b:
a,b,c = med[j]
ta += a
tb += b
tc += c
b <<= 1
dif = ta * mb - tb * ma
h1.append((dif,tc))
for i in range(2 ** (n - n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n - n//2):
if i & b:
a,b,c = med[j+n//2]
ta += a
tb += b
tc += c
b <<= 1
dif = tb * ma - ta * mb
h2.append((dif,tc))
h1.sort()
h2.sort()
i = 0
ans = INF
for h,c in h1:
while h2[i][0] < h:
i += 1
if i >= len(h2): break
if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
ans = min(ans,c+h2[i][1])
else:
continue
break
if ans < INF:
print(ans)
else:
print(-1)
if __name__ == '__main__':
main() |
s556774115 | p03806 | u703950586 | 1590842388 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2110 | 136220 | 1298 | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
INF = 10**5
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
n,ma,mb = LI()
med = [LI() for _ in range(n)]
h1 = []
h2 = []
for i in range(2 ** (n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2):
if i & b:
a,b,c = med[j]
ta += a
tb += b
tc += c
b <<= 1
dif = ta * mb - tb * ma
h1.append((dif,tc))
for i in range(2 ** (n - n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2,n):
if i & b:
a,b,c = med[j]
ta += a
tb += b
tc += c
b <<= 1
dif = tb * ma - ta * mb
h2.append((dif,tc))
h1.sort()
h2.sort()
i = 0
ans = INF
for h,c in h1:
while h2[i][0] < h:
i += 1
if i >= len(h2): break
if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
ans = min(ans,c+h2[i][1])
else:
continue
break
if ans < INF:
print(ans)
else:
print(-1)
if __name__ == '__main__':
main() |
s352826277 | p03806 | u703950586 | 1590842196 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2111 | 137844 | 1246 | import sys,queue,math,copy,itertools,bisect,collections,heapq
def main():
INF = 10**18
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
n,ma,mb = LI()
med = [LI() for _ in range(n)]
h1 = []
h2 = []
for i in range(2 ** (n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2):
if i & b:
a,b,c = med[j]
ta += a
tb += b
tc += c
b <<= 1
dif = ta * mb - tb * ma
h1.append((dif,tc))
for i in range(2 ** (n//2)):
b = 1
ta = 0
tb = 0
tc = 0
for j in range(n//2,n):
if i & b:
a,b,c = med[j]
ta += a
tb += b
tc += c
b <<= 1
dif = tb * ma - ta * mb
h2.append((dif,tc))
h1.sort()
h2.sort()
i = 0
ans = INF
for h,c in h1:
while h2[i][0] < h:
i += 1
if i >= len(h2): break
if h == h2[i][0] and (h != 0 and h2[i][0] != 0):
ans = min(ans,c+h2[i][1])
if ans < INF:
print(ans)
else:
print(-1)
if __name__ == '__main__':
main() |
s797499419 | p03806 | u932465688 | 1587315074 | Python | PyPy3 (2.4.0) | py | Runtime Error | 226 | 62172 | 750 | n,ma,mb = map(int,input().split())
L = []
sa = 0
sb = 0
for i in range(n):
a,b,c = map(int,input().split())
sa += a
sb += b
L.append([a,b,c])
dp = [[[10000 for i in range(sa+1)] for i in range(sb+1)] for i in range(n+1)]
dp[0][0][0] = 0
for i in range(n):
a = L[i][0]
b = L[i][1]
c = L[i][2]
for j in range(sa+1):
for k in range(sb+1):
if j-a >= 0 and k-b >= 0:
dp[i+1][j][k] = min(dp[i][j-a][k-b]+c,dp[i+1][j][k],dp[i][j][k])
else:
dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k])
k = max(ma,mb)
t = max(sa,sb)
cur = 1
ans = 10000
while k*cur <= t:
ans = min(ans,dp[n][ma*cur][mb*cur])
cur += 1
if ans == 10000:
print(-1)
else:
print(ans)
|
s791275357 | p03806 | u627600101 | 1587099898 | Python | Python (3.4.3) | py | Runtime Error | 1695 | 4712 | 559 | N,Ma,Mb=map(int,input().split())
a=[0 for k in range(N)]
b=[0 for k in range(N)]
c=[0 for k in range(N)]
for k in range(N):
a[k],b[k],c[k]=map(int,input().split())
Sa=sum(a)
Sb=sum(b)
dp =[[5000 for k in range(Sb+1)]for k in range(Sa+1)]
dp[0][0]=0
for k in range(N):
for j in range(Sa-a[k]+1):
for l in range(Sb-b[k]+1):
dp[j+a[k]][l+b[k]]=min(dp[j][l]+c[k], dp[j+a[k]][l+b[k]])
ans=[5000]
for k in range(1, max(sum(a)//Mb,sum(b)//Ma)+1):
ans.append(dp[Mb*k][Ma*k])
if min(ans)==5000:
print(-1)
else:
print(min(ans)) |
s169804341 | p03806 | u932465688 | 1586873110 | Python | PyPy3 (2.4.0) | py | Runtime Error | 167 | 38384 | 874 | n,ma,mb = map(int,input().split())
L = []
for i in range(n):
a,b,c = map(int,input().split())
L.append([a,b,c])
dp = [[10**5]*401 for i in range(401)]
make = [[0,0],[L[0][0],L[0][1]]]
dp[L[0][0]][L[0][1]] = L[0][2]
dp[0][0] = 0
for i in range(1,n):
a = L[i][0]
b = L[i][1]
c = L[i][2]
for j in range(len(make)):
t = make[j][0]
u = make[j][1]
dp[t+a][u+b] = min(dp[t][u]+c,dp[t+a][u+b])
make.append([t+a, u+b])
make = list(set(make))
if ma <= mb:
cnt = 1
ans = 10**5
while mb*cnt <= 400:
ans = min(ans, dp[ma*cnt][mb*cnt])
cnt += 1
if ans == 10**5:
print(-1)
else:
print(ans)
else:
cnt = 1
ans = 10**5
while ma*cnt <= 400:
ans = min(ans, dp[ma*cnt][mb*cnt])
cnt += 1
if ans == 10**5:
print(-1)
else:
print(ans)
|
s927864364 | p03806 | u488401358 | 1586481705 | Python | PyPy3 (2.4.0) | py | Runtime Error | 174 | 38384 | 924 | from math import gcd
N,ma,mb=map(int,input().split())
med=[]
for i in range(0,N):
a,b,c=map(int,input().split())
med.append((a,b,c))
dp=[[[float("inf") for i in range(0,401)] for j in range(0,401)] for k in range(0,N)]
dp=[[float("inf") for i in range(0,401)] for j in range(0,401)]
for i in range(0,401):
for j in range(0,401):
if not (i==0 and j==0):
g=gcd(i,j)
I=i//g
J=j//g
if I==ma and J==mb:
dp[i][j]=0
else:
g=gcd(i+med[0][0],j+med[0][1])
I=(i+med[0][0])//g
J=(j+med[0][1])//g
if I==ma and J==mb:
dp[i][j]=med[0][2]
for i in range(1,N):
a,b,c=med[i]
for j in range(0,401-a):
for k in range(0,401-b):
dp[j][k]=min(dp[j][k],c+dp[j+a][k+b])
if dp[0][0]==float("inf"):
print(-1)
else:
print(dp[0][0]) |
s075136629 | p03806 | u562935282 | 1586319548 | Python | Python (3.4.3) | py | Runtime Error | 72 | 3828 | 787 | def main():
from collections import defaultdict
inf = 100 * 40 + 1
N, Ma, Mb = map(int, input().split())
dp = [inf] * (400 * N * 2 + 10)
# dp[x]:=minimum price
# (b-a)(Mb+Ma)-(b+a)(Mb-Ma)=0を目指す
for _ in range(N):
a, b, p = map(int, input().split())
x = (b - a) * (Mb + Ma) - (b + a) * (Mb - Ma)
for key in range(400 * N * 2 + 10):
if dp[key] == inf: continue
nkey = key + x
dp[nkey] = min(dp[nkey], dp[key] + p)
dp[x] = min(dp[x], p)
if dp[0] == inf:
print(-1)
else:
print(dp[0])
if __name__ == '__main__':
main()
# import sys
#
# sys.setrecursionlimit(10 ** 7)
#
# input = sys.stdin.readline
# rstrip()
# int(input())
# map(int, input().split())
|
s968188155 | p03806 | u654470292 | 1586151965 | Python | PyPy3 (2.4.0) | py | Runtime Error | 378 | 73688 | 1141 | import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
return sys.stdin.readline()[:-1]
def ruiseki(lst):
return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
n,ma,mb=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(n)]
# dp=[[[float('inf')]*(400+1) for i in range(400+1)] for j in range(n+1)]
dp=[]
tmp=[]
for j in range(401):
tmp2=[]
for k in range(401):
tmp2.append(float('inf'))
tmp.append(tmp2)
for i in range(n+1):
dp.append(tmp[:])
dp[0][0][0]=0
for i in range(n):
a,b,c=abc[i]
for j in range(400):
for k in range(400):
if dp[i][j][k]!=float('inf'):
dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)
dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
ans=float('inf')
now=[ma,mb]
while 1:
a,b=now
ans=min(ans,dp[n][a][b])
if a+ma>400 or b+mb>400:
break
now=[a+ma,b+mb]
if ans==float('inf'):
print(-1)
else:
print(ans) |
s935147326 | p03806 | u107601154 | 1585790988 | Python | Python (3.4.3) | py | Runtime Error | 21 | 3164 | 905 | N,Ma,Mb= (int(x) for x in input().split())
a = [0]*N
b = [0]*N
c = [0]*N
# use = [0]*N
ans = 40*100+1
for i in range (N):
a[i],b[i],c[i] = (int(x) for x in input().split())
# 0ならつかわない1なら使う
A = 0
B = 0
C = 0
def search(N,Ma,Mb,a,b,c,A,B,C):
if (B!= 0 and A/B == Ma/Mb):
return C
if(N==1):
# use[N-1] = 0
# if (B!= 0 and A/B == Ma/Mb):
# return C
use[N-1] = 1
A += a[N-1]
B += b[N-1]
C += c[N-1]
if (B!= 0 and A/B == Ma/Mb):
return C
else: C = ans
return C
else:
# use[N-1] = 0
c0 = search(N-1,Ma,Mb,a,b,c,A,B,C)
# use[N-1] = 1
A += a[N-1]
B += b[N-1]
C += c[N-1]
c1 = search(N-1,Ma,Mb,a,b,c,A,B,C)
return min(c0,c1)
Ans = search(N,Ma,Mb,a,b,c,A,B,C)
if(Ans == ans):
print("-1")
else: print(Ans) |
s908225473 | p03806 | u107601154 | 1585790860 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 909 | N,Ma,Mb= (int(x) for x in input().split())
a = [0]*N
b = [0]*N
c = [0]*N
# use = [0]*N
ans = 40*100+1
for i in range (N):
a[i],b[i],c[i] = (int(x) for x in input().split())
# 0ならつかわない1なら使う
A = 0
B = 0
C = 0
def search(N,Ma,Mb,a,b,c,A,B,C):
if (B!= 0 and A/B == Ma/Mb):
return C
if(N==1):
# use[N-1] = 0
# if (B!= 0 and A/B == Ma/Mb):
# return C
use[N-1] = 1
A += a[N-1]
B += b[N-1]
C += c[N-1]
if (B!= 0 and A/B == Ma/Mb):
return C
else: C = ans
return C
else:
# use[N-1] = 0
c0 = search(N-1,Ma,Mb,a,b,c,A,B,C)
# use[N-1] = 1
A += a[N-1]
B += b[N-1]
C += c[N-1]
c1 = search(N-1,Ma,Mb,a,b,c,A,B,C)
return min(c0,c1)
Ans = search(N,Ma,Mb,a,b,c,use,A,B,C)
if(Ans == ans):
print("-1")
else: print(Ans) |
s620103871 | p03806 | u994521204 | 1584462526 | Python | PyPy3 (2.4.0) | py | Runtime Error | 229 | 80420 | 613 | import sys
input = sys.stdin.buffer.readline
n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(401) for _ in range(401)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
a,b,c=ABC[i]
for j in range(401-a):
for k in range(401-b):
ni=i+1
dp[ni][j][k]=min(dp[ni][j][k], dp[i][j][k])
dp[ni][nj][nk]=min(dp[i][j][k]+c,dp[ni][nj][nk])
ans=infi
for i in range(1,401):
if ma*i>400:
break
if mb*i>400:
break
ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1) |
s083538201 | p03806 | u994521204 | 1584462042 | Python | Python (3.4.3) | py | Runtime Error | 160 | 55796 | 718 | n,ma,mb=map(int,input().split())
ABC=[list(map(int,input().split())) for _ in range(n)]
infi=10**15
dp=[[[infi]*(401) for _ in range(401)]for _ in range(41)]
dp[0][0][0]=0
for i in range(n):
a,b,c=ABC[i]
for j in range(401):
for k in range(401):
ni=i+1
nj=j+a
nk=k+b
dp[ni][nj][nk]=min(dp[ni][nj][nk], dp[i][j][k])
if nj>400:
continue
if nk>400:
continue
dp[ni][nj][nk]=min(dp[i][j][k]+c, dp[i][nj][nk], dp[ni][nj][nk])
ans=infi
for i in range(1,401):
if ma*i>100:
break
if mb*i>100:
break
ans=min(ans, dp[n][ma*i][mb*i])
print(ans if ans!=infi else -1) |
s706051446 | p03806 | u119226758 | 1584227774 | Python | Python (3.4.3) | py | Runtime Error | 19 | 3184 | 981 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min(dp[N][a][b], ans)
print(ans)
|
s393879168 | p03806 | u119226758 | 1584227501 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3184 | 983 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min([dp[N][a][b], ans])
print(ans)
|
s950612560 | p03806 | u119226758 | 1584227442 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3184 | 983 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min([dp[N][a][b], ans])
print(ans)
|
s197478403 | p03806 | u119226758 | 1584227381 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3184 | 1013 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
print(a,b,dp[N][a][b])
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min([dp[N][a][b], ans])
print(ans)
|
s074730363 | p03806 | u119226758 | 1584227210 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3064 | 1156 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
for it_MA in range(MA+1):
for it_MB in range(MB+1):
# print(it_N+1,it_MA,it_MB,dp[it_N+1][it_MA][it_MB])
#中身確認
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
print(a,b,dp[N][a][b])
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min([dp[N][a][b], ans])
print(ans)
|
s105563733 | p03806 | u119226758 | 1584227137 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 1155 | import math
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[[math.inf for i in range(MB+1)] for j in range(MA+1)] for k in range(N+1)]
dp[0][0][0] = 0
for it_N in range(N):
for it_MA in range(MA+1):
for it_MB in range(MB+1):
if it_MA >= a[it_N] and it_MB >= b[it_N]:
if dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] != math.inf:
dp[it_N+1][it_MA][it_MB] = min(dp[it_N][it_MA-a[it_N]][it_MB-b[it_N]] + c[it_N], dp[it_N][it_MA][it_MB])
else:
dp[it_N+1][it_MA][it_MB]=dp[it_N][it_MA][it_MB]
for it_MA in range(MA+1):
for it_MB in range(MB+1):
print(it_N+1,it_MA,it_MB,dp[it_N+1][it_MA][it_MB])
#中身確認
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
print(a,b,dp[N][a][b])
if a/b == rate and dp[N][a][b] != math.inf:
if ans == -1:
ans = dp[N][a][b]
else:
ans = min([dp[N][a][b], ans])
print(ans)
|
s875065258 | p03806 | u119226758 | 1583981003 | Python | Python (3.4.3) | py | Runtime Error | 131 | 4084 | 1031 |
N, Ma, Mb = map(int,input().split())
rate = Ma / Mb
a = [0] * N
b = [0] * N
c = [0] * N
MA = 0
MB = 0
for i in range(N):
a[i], b[i], c[i] = map(int, input().split())
MA += a[i]
MB += b[i]
dp = [[0 for i in range(MA+1)] for j in range(MB+1)]
src = [[0 for i in range(MA+1)] for j in range(MB+1)]
src[0][0] = 1
for it_N in range(N):
for it_MA in reversed(range(MA-a[it_N]+1)):
for it_MB in reversed(range(MB-b[it_N]+1)):
MAa = it_MA + a[it_N]
MBb = it_MB + b[it_N]
if src[it_MA][it_MB] == 1:
src[MAa][MBb] = 1
if dp[MAa][MBb] == 0:
dp[MAa][MBb] = dp[it_MA][it_MB] + c[it_N]
else:
dp[MAa][MBb] = min([dp[MAa][MBb], dp[it_MA][it_MB] + c[it_N]])
ans = -1
for a in range(1, MA+1):
for b in range(1, MB+1):
if a/b == rate and dp[a][b] != 0:
if ans == -1:
ans = dp[a][b]
else:
ans = min([dp[a][b], ans])
print(ans)
|
s311478211 | p03806 | u353797797 | 1582843613 | Python | Python (3.4.3) | py | Runtime Error | 18 | 3188 | 1753 | # 半分全列挙バージョン
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]
def main():
n,ma,mb=MI()
abc=[LI() for _ in range(n)]
inf=10**9
# 前半の要素をa1,b1、後半の要素をa2,b2とする
# (a1+a2):(b1+b2)=ma:mbとなればよい。式変形すると
# (mb*a1-ma*b1)+(mb*a2-ma*b2)=0となる
# つまり各要素についてmb*a-ma*bという指標を計算し、前半と後半で和が0になる組合せを探せばよい
# 前半全列挙
pre=[(0,0,0)]
for a,b,c in abc[:n//2]:
pn=len(pre)
for i in range(pn):
pa,pb,pc=abc[i]
pre.append((a+pa,b+pb,c+pc))
pre=[(a*mb-b*ma,c) for a,b,c in pre]
# 後半全列挙
pos=[(0,0,0)]
for a,b,c in abc[n//2:]:
pn=len(pos)
for i in range(pn):
pa,pb,pc=abc[i]
pos.append((a+pa,b+pb,c+pc))
pos=[(a*mb-b*ma,c) for a,b,c in pos]
# 指標が昇順になるよう並び替えて、前半は前から後半は後からしゃくとり法で
# 指標の和が0になるところのコストを求める
pre.sort()
pos.sort()
j=len(pos)-1
ans=inf
for val,c in pre:
while val+pos[j][0]>0 and j>0:j-=1
while val+pos[j][0]==0 and j>=0:
now=c+pos[j][1]
if now and now<ans:ans=now
j-=1
if j<0:break
if ans==inf:print(-1)
else:print(ans)
main() |
s036433514 | p03806 | u644907318 | 1582033166 | Python | Python (3.4.3) | py | Runtime Error | 2108 | 73592 | 1237 | from itertools import combinations
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
if N==1:
if A[0][0]*Mb==A[0][1]*Ma:
print(A[0][2])
else:
print(-1)
else:
A1 = A[:N//2] # N//2
A2 = A[N//2:] # N-N//2
B1 = []
for k in range(1,N//2+1):
for x in combinations(range(N//2),k):
cnt1 = 0
cnt2 = 0
for i in x:
cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
cnt2 += A1[i][2]
B1.append((cnt1,cnt2))
B2 = []
for k in range(1,N-N//2+1):
for x in combinations(range(N-N//2),k):
cnt1 = 0
cnt2 = 0
for i in x:
cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
cnt2 += A2[i][2]
B2.append((cnt1,cnt2))
B1 = sorted(B1,key=lambda x:x[1])
B1 = sorted(B1,key=lambda x:x[0])
B2 = sorted(B2,key=lambda x:x[1])
B2 = sorted(B2,key=lambda x:x[0])
cmin = 10000
for b1 in B1:
b,c = b1[0],b1[1]
ind = bisect_left(B2,(-b,0))
if B2[ind][0]==-b:
cmin = min(cmin,c+B2[ind][1])
if cmin>=10000:
print(-1)
else:
print(cmin) |
s124760218 | p03806 | u644907318 | 1582032518 | Python | PyPy3 (2.4.0) | py | Runtime Error | 2111 | 157740 | 1004 | from itertools import combinations
from bisect import bisect_left
N,Ma,Mb = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(N)]
A1 = A[:N//2] # N//2
A2 = A[N//2:] # N-N//2
B1 = []
for k in range(1,N//2+1):
for x in combinations(range(N//2),k):
cnt1 = 0
cnt2 = 0
for i in x:
cnt1 += Ma*A1[i][1]-Mb*A1[i][0]
cnt2 += A1[i][2]
B1.append((cnt1,cnt2))
B2 = []
for k in range(1,N-N//2+1):
for x in combinations(range(N-N//2),k):
cnt1 = 0
cnt2 = 0
for i in x:
cnt1 += Ma*A2[i][1]-Mb*A2[i][0]
cnt2 += A2[i][2]
B2.append((cnt1,cnt2))
B1 = sorted(B1,key=lambda x:x[1])
B1 = sorted(B1,key=lambda x:x[0])
B2 = sorted(B2,key=lambda x:x[1])
B2 = sorted(B2,key=lambda x:x[0])
cmin = 10000
for b1 in B1:
b,c = b1[0],b1[1]
ind = bisect_left(B2,(-b,0))
if B2[ind][0]==-b:
cmin = min(cmin,c+B2[ind][1])
if cmin>=10000:
print(-1)
else:
print(cmin) |
s937235151 | p03806 | u223904637 | 1580703336 | Python | Python (3.4.3) | py | Runtime Error | 28 | 10868 | 327 | n,ma,mb=map(int,input().split())
dpn=10**6
dp=[10**6]*dpn
dp[0]=0
for i in range(n):
a,b,c=map(int,input().split())
for j in range(dpn,-1,-1):
if j==0 or dp[j]!=10**6:
dp[j+a*1000+b]=min(dp[j+a*1000+b],dp[j]+c)
ans=10**6
s=1000*ma+mb
p=1
while s*p<10**6:
ans=min(ans,dp[s*p])
p+=1
print(ans) |
s144028710 | p03806 | u087776048 | 1580498979 | Python | Python (3.4.3) | py | Runtime Error | 17 | 3064 | 1636 | from math import gcd
import numpy as np
# Y = XU
# X^T * Y = X^T * XU
# U = (X^T * X)^(-1) * X^T * Y
epsilon = 0.001
def get_stdin():
return list(map(int, input().split()))
N, M_a, M_b = get_stdin()
a = []
b = []
c = []
for _ in range(N):
a_i, b_i, c_i = get_stdin()
a.append(a_i)
b.append(b_i)
c.append(c_i)
# dummy_input_array = [list(map(int, s.split())) for s in dummy_input.split('\n')]
# N = dummy_input_array[0][0]
# M_a = dummy_input_array[0][1]
# M_b = dummy_input_array[0][2]
# Y = np.array([M_a, M_b]).reshape((2, -1))
# a = []
# b = []
# c = []
# for i in range(N):
# a.append(dummy_input_array[i+1][0])
# b.append(dummy_input_array[i+1][1])
# c.append(dummy_input_array[i+1][2])
a = np.array(a)
b = np.array(b)
c = np.array(c)
s_idx = [[i for j in range(N) if a[i] % a[j] != 0 and b[i] % b[j] != 0] for i in range(N)]
s_idx = [i for i in range(N) if len(s_idx[i]) > 0]
a_s = a[s_idx]
b_s = b[s_idx]
c_s = c[s_idx]
N_s = len(a_s)
Y_s = Y[s_idx]
Xt = np.array(list(zip(a_s, b_s)))
XtX = np.array([
[
a_s[i] * a_s[j] + b_s[i] * b_s[j]
for i in range(N_s)
] for j in range(N_s)
])
XtX_det = np.linalg.det(XtX)
if np.abs(XtX_det) < epsilon:
print(-1)
XtX_inv = np.linalg.inv(XtX)
U = (XtX_inv @ Xt) @ Y_s
U_u = np.mean(U)
ans = U - U_u
err = np.sum(np.abs(ans))
if err > epsilon or np.any(U < 0.0):
print(-1)
ans_idx = U > epsilon
a_ans = np.sum(a_s.reshape(2, 1)[ans_idx])
b_ans = np.sum(b_s.reshape(2, 1)[ans_idx])
if np.abs(M_a / M_b - a_ans / b_ans) < epsilon:
sum_c = np.sum(c_s)
print(sum_c)
else:
print(-1)
|
s339496041 | p03806 | u296518383 | 1580408072 | Python | PyPy3 (2.4.0) | py | Runtime Error | 187 | 41072 | 696 | import sys
input = sys.stdin.buffer.readline
N, Ma, Mb = map(int, input().split())
ABC = [list(map(int, input().split())) for _ in range(N)]
sumA = sum([ABC[i][0] for i in range(N)])
sumB = sum([ABC[i][1] for i in range(N)])
INF = 10 ** 15
dp = [[INF for j in range(sumA + 1)] for i in range(sumB + 1)]
dp[0][0] = 0
for a, b, c in ABC:
for i in range(sumA, -1, -1):
for j in range(sumB, -1, -1):
if dp[i][j] != INF:
dp[i + a][j + b] = min(dp[i + a][j + b], dp[i][j] + c)
answer = INF
for i in range(1, sumA + 1):
for j in range(1, sumB + 1):
if dp[i][j] != INF and i / j == Ma / Mb:
answer = min(answer, dp[i][j])
print(answer if answer != INF else -1) |
s658287870 | p03806 | u303039933 | 1580398370 | Python | PyPy3 (2.4.0) | py | Runtime Error | 177 | 38384 | 3405 | #include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
using ll = long long;
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define SIZE(obj) (int)(obj).sizeT()
#define YESNO(cond, yes, no) \
{ cout << ((cond) ? (yes) : (no)) << endl; }
#define SORT(list) sort(ALL((list)));
#define RSORT(list) sort((list).rbegin(), (list).rend())
#define ASSERT(cond, mes) assert(cond&& mes)
constexpr int MOD = 1'000'000'007;
constexpr int INF = 1'050'000'000;
template <typename T>
T round_up(const T& a, const T& b) {
return (a + (b - 1)) / b;
}
template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
is >> p.first >> p.second;
return is;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2>& p) {
os << p.first << p.second;
return os;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
REP(i, (int)v.size())
is >> v[i];
return is;
}
template <typename T>
T clamp(T& n, T a, T b) {
if (n < a) n = a;
if (n > b) n = b;
return n;
}
template <typename T>
static T GCD(T u, T v) {
T r;
while (v != 0) {
r = u % v;
u = v;
v = r;
}
return u;
}
template <typename T>
static T LCM(T u, T v) {
return u / GCD(u, v) * v;
}
template <typename T>
std::vector<T> enum_div(T n) {
std::vector<T> ret;
for (T i = 1; i * i <= n; ++i) {
if (n % i == 0) {
ret.push_back(i);
if (i * i != n) { ret.push_back(n / i); }
}
}
return ret;
}
template <typename T>
bool chmin(T& a, const T& b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template <typename T>
bool chmax(T& a, const T& b) {
if (a < b) {
a = b;
return true;
}
return false;
}
struct ToUpper {
char operator()(char c) {
return toupper(c);
}
};
struct ToLower {
char operator()(char c) {
return tolower(c);
}
};
int dp[41][401][401];
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int N, Ma, Mb;
cin >> N >> Ma >> Mb;
vector<int> A(N), B(N), C(N);
REP(i, N) {
cin >> A[i] >> B[i] >> C[i];
}
vector<vector<vector<int>>> dp(
41, vector<vector<int>>(401, vector<int>(401, INF)));
dp[0][0][0] = 0;
REP(i, N) {
REP(j, 401) {
REP(k, 401) {
if (dp[i][j][k] == INF) continue;
chmin(dp[i + 1][j][k], dp[i][j][k]);
chmin(dp[i + 1][j + A[i]][k + B[i]], dp[i][j][k] + C[i]);
}
}
}
int ans = INF;
FOR(i, 1, 401) {
FOR(j, 1, 401) {
if (dp[N][i][j] == INF) continue;
if (Ma * j == Mb * i) { chmin(ans, dp[N][i][j]); }
}
}
if (ans == INF) ans = -1;
cout << ans << endl;
return 0;
}
|
s883166282 | p03806 | u702786238 | 1580307289 | Python | PyPy3 (2.4.0) | py | Runtime Error | 181 | 38256 | 905 | N, Ma, Mb = map(int, input().split())
items = []
mas = []
mbs = []
costs = []
for i in range(N):
ma, mb, c = map(int, input().split())
mas.append(ma)
mbs.append(mb)
costs.append(c)
max_cost = 5000
dp = [[[max_cost for k in range(410)] for j in range(410)] for i in range(41)]
for i in range(N):
for j in range(400):
for k in range(400):
if dp[i][j][k] == max_cost:
continue
if dp[i][j][k] > dp[i+1][j][k]:
dp[i][j][k] = dp[i+1][j][k]
if dp[i+1][j+mas[i]][k+mbs[i]] > dp[i][j][k] + costs[i]:
dp[i+1][j+mas[i]][k+mbs[i]] = dp[i][j][k] + costs[i]
min_cost = 1000000000000000000
for (int wa = 1; wa < 400; ++wa) :
for (int wb = 1; wb < 400; ++wb) :
if (wa * mb != wb * ma):
continue;
if min_cost > dp[N][wa][wb]:
min_cost = dp[N][wa][wb]
if min_cost == 1000000000000000000:
print("-1")
else:
print(min_cost) |
s680730525 | p03806 | u702786238 | 1580304164 | Python | PyPy3 (2.4.0) | py | Runtime Error | 168 | 38640 | 443 | import numpy as np
N, Ma, Mb = map(int, input().split())
items = []
for i in range(N):
items.append(list(map(int, input().split())))
items = np.array(items)
import itertools
l = list(itertools.product([False,True], repeat=N))
min_cost = 1000000000000000000
for comb in l[1:]:
idx = np.where(comb)
cost = items[idx,2].sum()
if (items[idx,0].sum() == items[idx,1].sum()) & (cost < min_cost):
min_cost = cost
print(int(min_cost)) |
s010514741 | p03806 | u702786238 | 1580304132 | Python | PyPy3 (2.4.0) | py | Runtime Error | 170 | 38460 | 438 | import numpy as np
N, Ma, Mb = map(int, input().split())
items = []
for i in range(N):
items.append(list(map(int, input().split())))
items = np.array(items)
import itertools
l = list(itertools.product([False,True], repeat=N))
min_cost = 1000000000000000000
for comb in l[1:]:
idx = np.where(comb)
cost = items[idx,2].sum()
if (items[idx,0].sum() == items[idx,1].sum()) & (cost < min_cost):
min_cost = cost
print(min_cost) |
s404336664 | p03806 | u053699292 | 1580252196 | Python | Python (3.4.3) | py | Runtime Error | 91 | 5340 | 805 | cost_lst = [''] * 100000
def N_n(N):
return int((-1)**(N%2 + 1) * (N + N%2)/2)
def n_N(n):
if n <= 0:
return int((-2) * n)
else:
return int(2 * n - 1)
def UPDATE(n,c):
N = n_N(n)
if cost_lst[N] == '':
cost_lst[N] = c
elif cost_lst[N] > c:
cost_lst[N] = c
return 0
if __name__ == '__main__':
N, Ma, Mb = map(int, input().split())
drug_lst = []
for i in range(0,N):
drug_lst.append([int(p) for p in input().split()])
value_list = []
for i in range(0,N):
v = drug_lst[i][0] * Mb - drug_lst[i][1] * Ma
value_list.append([v,drug_lst[i][2]])
for l in value_list:
n, c = l[0], l[1]
UPDATE(n,c)
for i in range(0,100000):
if cost_lst[i] != '':
cc = c + cost_lst[i]
nn = n + N_n(i)
UPDATE(nn,cc)
if cost_lst[0] == '':
print(-1)
else:
print(cost_lst[0]) |
s708246456 | p03806 | u190086340 | 1580151010 | Python | PyPy3 (2.4.0) | py | Runtime Error | 465 | 86996 | 1077 | import fractions
def args():
N, MA, MB = list(map(int, input().split()))
A, B, C = [], [], []
for i in range(N):
a, b, c = list(map(int, input().split()))
A.append(a)
B.append(b)
C.append(c)
# print(N, MA, MB)
# print(A)
# print(B)
# print(C)
return N, MA, MB, A, B, C
def solve(N, MA, MB, A, B, C):
def recur(i, a, b):
if not MEMO[i][a][b] is None:
return MEMO[i][a][b]
if not i < N:
if 0 < a and 0 < b:
g = fractions.gcd(a, b)
if (MA, MB) == (a / g, b / g):
return 0
return float("inf")
res = min(recur(i + 1, a, b) + 0, recur(i + 1, a + A[i], b + B[i]) + C[i])
MEMO[i][a][b] = res
return res
x, y, z = 40 + 10, 10 * 10 + 10, 10 * 10 + 10
MEMO = [[[None] * z for _ in range(y)] for _ in range(x)]
ans = recur(0, 0, 0)
return ans
if __name__ == '__main__':
res = solve(*args())
print(-1) if res == float("inf") else print(res) |
s019575638 | p03806 | u190086340 | 1580150847 | Python | PyPy3 (2.4.0) | py | Runtime Error | 443 | 84692 | 1060 | import fractions
def args():
N, MA, MB = list(map(int, input().split()))
A, B, C = [], [], []
for i in range(N):
a, b, c = list(map(int, input().split()))
A.append(a)
B.append(b)
C.append(c)
# print(N, MA, MB)
# print(A)
# print(B)
# print(C)
return N, MA, MB, A, B, C
def solve(N, MA, MB, A, B, C):
def recur(i, a, b):
if not MEMO[i][a][b] is None:
return MEMO[i][a][b]
if not i < N:
if 0 < a and 0 < b:
g = fractions.gcd(a, b)
if (MA, MB) == (a / g, b / g):
return 0
return float("inf")
res = min(recur(i + 1, a, b) + 0, recur(i + 1, a + A[i], b + B[i]) + C[i])
MEMO[i][a][b] = res
return res
x, y, z = 40 + 1, 10 * 10 + 1, 10 * 10 + 1
MEMO = [[[None] * z for _ in range(y)] for _ in range(x)]
ans = recur(0, 0, 0)
return ans
if __name__ == '__main__':
res = solve(*args())
print(-1) if res == float("inf") else print(res)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.