提出 #22923735
ソースコード 拡げる
# local test max score: 947159214
from heapq import heappush, heappop
inf=10**18
move = {"U":(-1,0), "R":(0,1), "D":(1,0), "L":(0,-1)}
def dijkstra(d,p,sy,sx,pos_dir,loop):
hq = [(0, (sy,sx))] # (distance, node)
seen = [[False]*W for _ in range(H)]
while hq:
y,x = heappop(hq)[1]
seen[y][x] = True
for dir in move:
if loop < SWITCH_NUM*2 and dir not in pos_dir:
continue
ty, tx = y+move[dir][0], x+move[dir][1]
if not (0 <= ty <= H-1 and 0 <= tx <= W-1):
continue
cost = graph[ty][tx][dir]
if seen[ty][tx] == False and d[y][x] + cost < d[ty][tx]:
d[ty][tx] = d[y][x] + cost
heappush(hq, (d[ty][tx], (ty, tx)))
p[ty][tx] = [y,x]
def get_path(p,ty,tx):
path = []
total_cost = 0
while not (ty == -1 and tx == -1):
sy,sx = p[ty][tx]
dy = ty-sy
dx = tx-sx
tmp = [k for k, v in move.items() if v == (dy,dx)]
if tmp:
dir = tmp[0]
path.append(dir)
total_cost += graph[ty][tx][dir]
ty,tx = sy,sx
path.reverse()
return [path, total_cost]
def calc_possible_dir(sy,sx,ty,tx):
dy = ty-sy
dx = tx-sx
if dy < 0 and dx == 0: #U
return ["U","R","L"]
elif dy < 0 and dx > 0: #UR
return ["U","R"]
elif dy == 0 and dx > 0: #R
return ["U","R","D"]
elif dy > 0 and dx > 0: #DR
return ["R","D"]
elif dy > 0 and dx == 0: #D
return ["R","D","L"]
elif dy > 0 and dx < 0: #DL
return ["D","L"]
elif dy == 0 and dx < 0: #L
return ["D","L","U"]
elif dy < 0 and dx < 0: #UL
return ["L","U"]
def reverse_dir(dir):
if dir == "U":
dir = "D"
elif dir == "R":
dir = "L"
elif dir == "D":
dir = "U"
elif dir == "L":
dir = "R"
return dir
def update_graph_weight_ini(actual_cost,path,ty,tx,loop):
weighted_cost = actual_cost//len(path)
for dir in path[::-1]:
sy = ty - move[dir][0]
sx = tx - move[dir][1]
if graph[ty][tx][dir] == 1:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = weighted_cost
else:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir]*3 + weighted_cost*2)//5
ty,tx = sy,sx
def update_graph_weight_last(actual_cost,tmp_total_cost,path,ty,tx,loop):
for dir in path[::-1]:
sy = ty - move[dir][0]
sx = tx - move[dir][1]
weighted_cost = actual_cost * graph[ty][tx][dir]//tmp_total_cost
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir]*2 + weighted_cost*3)//5
ty,tx = sy,sx
def calc_avg_actual_cost(actual_cost,path,sy,sx,ty,tx):
sy_idx,sx_idx = sy*2//H,sx*2//W
ty_idx,tx_idx = ty*2//H,tx*2//W
edge_cost = actual_cost//len(path)
if False:#[sy_idx, sx_idx] == [ty_idx, tx_idx]:
avg_actual_cost[sy_idx][sx_idx] = edge_cost if avg_actual_cost[sy_idx][sx_idx] == 0 else (avg_actual_cost[sy_idx][sx_idx] + edge_cost)//2
avg_actual_cost[ty_idx][tx_idx] = edge_cost if avg_actual_cost[ty_idx][tx_idx] == 0 else (avg_actual_cost[ty_idx][tx_idx] + edge_cost)//2
else:
for y_idx in range(2):
for x_idx in range(2):
avg_actual_cost[y_idx][x_idx] = edge_cost if avg_actual_cost[y_idx][x_idx] == 0 else (avg_actual_cost[y_idx][x_idx] + edge_cost)//2
H = W = 30
RECALC_NUM = 100
SWITCH_NUM = 100
# {direction:cost}
graph = [[{"U":1, "R":1, "D":1, "L":1} for _ in range(W)] for _ in range(H)]
avg_actual_cost = [[0]*2 for _ in range(2)]
initial_path = []
for i in range(1000):
# input position of start and goal
sy,sx,ty,tx = map(int,input().split())
# calculate minimum path with dijkstra
dist = [[inf]*W for _ in range(H)]
dist[sy][sx] = 0
prev =[[[-1,-1]]*W for _ in range(H)]
possible_dir = calc_possible_dir(sy,sx,ty,tx)
dijkstra(dist,prev,sy,sx,possible_dir,i)
l, tmp_total_cost = get_path(prev,ty,tx)
path = "".join(l)
print(path, flush=True)
# update weights of the graph based on the given cost and the used path
actual_cost = int(input())
if i <= RECALC_NUM*2:
calc_avg_actual_cost(actual_cost,path,sy,sx,ty,tx)
# replace all graph weights which have the initial value(1)
# with the average of actual costs of initial RECALC_NUM loops
if i == RECALC_NUM:
for y in range(H):
for x in range(W):
for dir in move:
graph[y][x][dir] = (graph[y][x][dir]*2 + avg_actual_cost[y*2//H][x*2//W])//3 if graph[y][x][dir] != 1 else avg_actual_cost[y*2//H][x*2//W]
# use initial RECALC_NUM inputs for increasing the accuracy of graph weight parameters
# if i <= RECALC_NUM:
# initial_path.append(((sy,sx,ty,tx), actual_cost, prev, path))
# if i and i == RECALC_NUM*2:
# for j in range(RECALC_NUM):
# sy2,sx2,ty2,tx2 = initial_path[j][0]
# l2,tmp_total_cost2 = get_path(initial_path[j][2],ty2,tx2)
# update_graph_weight_last(initial_path[j][1],tmp_total_cost2,initial_path[j][3],ty2,tx2,i)
# change the paramter tuning process for the first SWITCH_NUM loops and after that
if i < SWITCH_NUM:
update_graph_weight_ini(actual_cost,path,ty,tx,i)
else:
update_graph_weight_last(actual_cost,tmp_total_cost,path,ty,tx,i)
提出情報
ジャッジ結果
| セット名 |
test_ALL |
| 得点 / 配点 |
92821497168 / 100000000000 |
| 結果 |
|
| セット名 |
テストケース |
| test_ALL |
test_0000.txt, test_0001.txt, test_0002.txt, test_0003.txt, test_0004.txt, test_0005.txt, test_0006.txt, test_0007.txt, test_0008.txt, test_0009.txt, test_0010.txt, test_0011.txt, test_0012.txt, test_0013.txt, test_0014.txt, test_0015.txt, test_0016.txt, test_0017.txt, test_0018.txt, test_0019.txt, test_0020.txt, test_0021.txt, test_0022.txt, test_0023.txt, test_0024.txt, test_0025.txt, test_0026.txt, test_0027.txt, test_0028.txt, test_0029.txt, test_0030.txt, test_0031.txt, test_0032.txt, test_0033.txt, test_0034.txt, test_0035.txt, test_0036.txt, test_0037.txt, test_0038.txt, test_0039.txt, test_0040.txt, test_0041.txt, test_0042.txt, test_0043.txt, test_0044.txt, test_0045.txt, test_0046.txt, test_0047.txt, test_0048.txt, test_0049.txt, test_0050.txt, test_0051.txt, test_0052.txt, test_0053.txt, test_0054.txt, test_0055.txt, test_0056.txt, test_0057.txt, test_0058.txt, test_0059.txt, test_0060.txt, test_0061.txt, test_0062.txt, test_0063.txt, test_0064.txt, test_0065.txt, test_0066.txt, test_0067.txt, test_0068.txt, test_0069.txt, test_0070.txt, test_0071.txt, test_0072.txt, test_0073.txt, test_0074.txt, test_0075.txt, test_0076.txt, test_0077.txt, test_0078.txt, test_0079.txt, test_0080.txt, test_0081.txt, test_0082.txt, test_0083.txt, test_0084.txt, test_0085.txt, test_0086.txt, test_0087.txt, test_0088.txt, test_0089.txt, test_0090.txt, test_0091.txt, test_0092.txt, test_0093.txt, test_0094.txt, test_0095.txt, test_0096.txt, test_0097.txt, test_0098.txt, test_0099.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| test_0000.txt |
AC |
1013 ms |
87292 KiB |
| test_0001.txt |
AC |
1056 ms |
88692 KiB |
| test_0002.txt |
AC |
1066 ms |
89504 KiB |
| test_0003.txt |
AC |
1087 ms |
88988 KiB |
| test_0004.txt |
AC |
1137 ms |
91208 KiB |
| test_0005.txt |
AC |
1091 ms |
89740 KiB |
| test_0006.txt |
AC |
1085 ms |
89744 KiB |
| test_0007.txt |
AC |
1113 ms |
91144 KiB |
| test_0008.txt |
AC |
1110 ms |
90016 KiB |
| test_0009.txt |
AC |
1099 ms |
90912 KiB |
| test_0010.txt |
AC |
1053 ms |
89068 KiB |
| test_0011.txt |
AC |
1069 ms |
88532 KiB |
| test_0012.txt |
AC |
1094 ms |
88968 KiB |
| test_0013.txt |
AC |
1031 ms |
87652 KiB |
| test_0014.txt |
AC |
1083 ms |
88132 KiB |
| test_0015.txt |
AC |
1072 ms |
88964 KiB |
| test_0016.txt |
AC |
1101 ms |
89712 KiB |
| test_0017.txt |
AC |
1055 ms |
88816 KiB |
| test_0018.txt |
AC |
1077 ms |
89296 KiB |
| test_0019.txt |
AC |
1119 ms |
90172 KiB |
| test_0020.txt |
AC |
1115 ms |
90224 KiB |
| test_0021.txt |
AC |
1051 ms |
88664 KiB |
| test_0022.txt |
AC |
1079 ms |
89668 KiB |
| test_0023.txt |
AC |
1052 ms |
89464 KiB |
| test_0024.txt |
AC |
1077 ms |
89988 KiB |
| test_0025.txt |
AC |
1054 ms |
88772 KiB |
| test_0026.txt |
AC |
1057 ms |
88100 KiB |
| test_0027.txt |
AC |
1061 ms |
89636 KiB |
| test_0028.txt |
AC |
1067 ms |
89080 KiB |
| test_0029.txt |
AC |
1091 ms |
90096 KiB |
| test_0030.txt |
AC |
1060 ms |
89052 KiB |
| test_0031.txt |
AC |
1086 ms |
88672 KiB |
| test_0032.txt |
AC |
1124 ms |
91008 KiB |
| test_0033.txt |
AC |
1107 ms |
90872 KiB |
| test_0034.txt |
AC |
1093 ms |
90920 KiB |
| test_0035.txt |
AC |
1073 ms |
89236 KiB |
| test_0036.txt |
AC |
1074 ms |
88644 KiB |
| test_0037.txt |
AC |
1033 ms |
90024 KiB |
| test_0038.txt |
AC |
1087 ms |
88388 KiB |
| test_0039.txt |
AC |
1086 ms |
89152 KiB |
| test_0040.txt |
AC |
1058 ms |
88988 KiB |
| test_0041.txt |
AC |
1061 ms |
88972 KiB |
| test_0042.txt |
AC |
1083 ms |
90284 KiB |
| test_0043.txt |
AC |
1068 ms |
88768 KiB |
| test_0044.txt |
AC |
1100 ms |
89788 KiB |
| test_0045.txt |
AC |
1062 ms |
88248 KiB |
| test_0046.txt |
AC |
1093 ms |
90620 KiB |
| test_0047.txt |
AC |
1081 ms |
89500 KiB |
| test_0048.txt |
AC |
1100 ms |
88864 KiB |
| test_0049.txt |
AC |
1036 ms |
90620 KiB |
| test_0050.txt |
AC |
1103 ms |
91244 KiB |
| test_0051.txt |
AC |
1046 ms |
88276 KiB |
| test_0052.txt |
AC |
1125 ms |
89568 KiB |
| test_0053.txt |
AC |
1110 ms |
91252 KiB |
| test_0054.txt |
AC |
1080 ms |
90720 KiB |
| test_0055.txt |
AC |
1026 ms |
89008 KiB |
| test_0056.txt |
AC |
1067 ms |
89944 KiB |
| test_0057.txt |
AC |
1101 ms |
88892 KiB |
| test_0058.txt |
AC |
1078 ms |
88292 KiB |
| test_0059.txt |
AC |
1043 ms |
89736 KiB |
| test_0060.txt |
AC |
1087 ms |
89844 KiB |
| test_0061.txt |
AC |
1032 ms |
87864 KiB |
| test_0062.txt |
AC |
1112 ms |
90704 KiB |
| test_0063.txt |
AC |
1154 ms |
90324 KiB |
| test_0064.txt |
AC |
1116 ms |
90784 KiB |
| test_0065.txt |
AC |
1061 ms |
88692 KiB |
| test_0066.txt |
AC |
1112 ms |
90252 KiB |
| test_0067.txt |
AC |
1039 ms |
88508 KiB |
| test_0068.txt |
AC |
1102 ms |
90784 KiB |
| test_0069.txt |
AC |
1061 ms |
87668 KiB |
| test_0070.txt |
AC |
1081 ms |
90736 KiB |
| test_0071.txt |
AC |
1052 ms |
90432 KiB |
| test_0072.txt |
AC |
1063 ms |
89796 KiB |
| test_0073.txt |
AC |
1063 ms |
89684 KiB |
| test_0074.txt |
AC |
1086 ms |
88996 KiB |
| test_0075.txt |
AC |
1060 ms |
89340 KiB |
| test_0076.txt |
AC |
1103 ms |
89420 KiB |
| test_0077.txt |
AC |
1052 ms |
88328 KiB |
| test_0078.txt |
AC |
1059 ms |
89120 KiB |
| test_0079.txt |
AC |
1109 ms |
89676 KiB |
| test_0080.txt |
AC |
1106 ms |
89668 KiB |
| test_0081.txt |
AC |
1070 ms |
89344 KiB |
| test_0082.txt |
AC |
1059 ms |
88924 KiB |
| test_0083.txt |
AC |
1092 ms |
89004 KiB |
| test_0084.txt |
AC |
1084 ms |
90100 KiB |
| test_0085.txt |
AC |
1083 ms |
88404 KiB |
| test_0086.txt |
AC |
1063 ms |
88444 KiB |
| test_0087.txt |
AC |
1084 ms |
89460 KiB |
| test_0088.txt |
AC |
1053 ms |
88400 KiB |
| test_0089.txt |
AC |
1112 ms |
90384 KiB |
| test_0090.txt |
AC |
1119 ms |
90420 KiB |
| test_0091.txt |
AC |
1053 ms |
89988 KiB |
| test_0092.txt |
AC |
1065 ms |
89900 KiB |
| test_0093.txt |
AC |
1075 ms |
88168 KiB |
| test_0094.txt |
AC |
1068 ms |
89184 KiB |
| test_0095.txt |
AC |
1086 ms |
90252 KiB |
| test_0096.txt |
AC |
1106 ms |
92068 KiB |
| test_0097.txt |
AC |
1078 ms |
89132 KiB |
| test_0098.txt |
AC |
1070 ms |
90128 KiB |
| test_0099.txt |
AC |
1065 ms |
88836 KiB |