提出 #22967618
ソースコード 拡げる
# local test max score: 942918561
import random
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*3 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(actual_cost,tmp_total_cost,path,ty,tx,loop):
if loop < SWITCH_NUM:
edge_cost = actual_cost/len(path)
for dir in path[::-1]:
sy = ty - move[dir][0]
sx = tx - move[dir][1]
if loop < SWITCH_NUM:
if graph[ty][tx][dir] == 1:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = edge_cost
else:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir]*2 + edge_cost*3)/5
else:
weighted_cost = actual_cost * graph[ty][tx][dir]/tmp_total_cost
if loop < SWITCH_NUM*10-1:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir] + weighted_cost*2)/3
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_adjust(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
if loop == SWITCH_NUM*8-1:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir] + weighted_cost*2)/3
elif loop == SWITCH_NUM*12-1:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir]*2 + weighted_cost*3)/5
else:
graph[ty][tx][dir] = graph[sy][sx][reverse_dir(dir)] = (graph[ty][tx][dir] + weighted_cost)/2
ty,tx = sy,sx
H = W = 30
RECALC_NUM = 50
SWITCH_NUM = 50
# {direction:cost}
graph = [[{"U":1, "R":1, "D":1, "L":1} for _ in range(W)] for _ in range(H)]
avg_actual_cost = 0
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 actual cost and the used path
actual_cost = int(input())
if i < RECALC_NUM*2:
edge_cost = actual_cost/len(path)
avg_actual_cost = edge_cost if avg_actual_cost == 0 else (avg_actual_cost + edge_cost)/2
# replace all graph weights which have the initial value(1)
# with the average of actual costs in initial RECALC_NUM loops
if i == RECALC_NUM-1 or i == RECALC_NUM*2-1:
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)/3 if graph[y][x][dir] != 1 else avg_actual_cost
# adjust the graph weight of already used path regularly
initial_path.append(((sy,sx,ty,tx), actual_cost, prev))
if i == SWITCH_NUM*8-1 or i == SWITCH_NUM*12-1 or i == SWITCH_NUM*16-1:
for j in range(len(initial_path)):
sy2,sx2,ty2,tx2 = initial_path[j][0]
prev2 = initial_path[j][2]
path2,tmp_total_cost2 = get_path(prev2,ty2,tx2)
update_graph_weight_adjust(initial_path[j][1],tmp_total_cost2,path2,ty2,tx2,i)
# change the paramter tuning process depending on the loop count
update_graph_weight(actual_cost,tmp_total_cost,path,ty,tx,i)
提出情報
ジャッジ結果
| セット名 |
test_ALL |
| 得点 / 配点 |
93382152783 / 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 |
1898 ms |
179268 KiB |
| test_0001.txt |
AC |
1810 ms |
177624 KiB |
| test_0002.txt |
AC |
1873 ms |
179688 KiB |
| test_0003.txt |
AC |
1846 ms |
178804 KiB |
| test_0004.txt |
AC |
1894 ms |
180492 KiB |
| test_0005.txt |
AC |
1909 ms |
180336 KiB |
| test_0006.txt |
AC |
1847 ms |
177684 KiB |
| test_0007.txt |
AC |
1889 ms |
179548 KiB |
| test_0008.txt |
AC |
1911 ms |
178604 KiB |
| test_0009.txt |
AC |
1864 ms |
179312 KiB |
| test_0010.txt |
AC |
1848 ms |
181420 KiB |
| test_0011.txt |
AC |
1817 ms |
180524 KiB |
| test_0012.txt |
AC |
1835 ms |
178876 KiB |
| test_0013.txt |
AC |
1860 ms |
179976 KiB |
| test_0014.txt |
AC |
1824 ms |
178472 KiB |
| test_0015.txt |
AC |
1829 ms |
180004 KiB |
| test_0016.txt |
AC |
1902 ms |
181316 KiB |
| test_0017.txt |
AC |
1876 ms |
180660 KiB |
| test_0018.txt |
AC |
1849 ms |
177572 KiB |
| test_0019.txt |
AC |
1897 ms |
180996 KiB |
| test_0020.txt |
AC |
1931 ms |
180328 KiB |
| test_0021.txt |
AC |
1828 ms |
179116 KiB |
| test_0022.txt |
AC |
1853 ms |
180852 KiB |
| test_0023.txt |
AC |
1829 ms |
179752 KiB |
| test_0024.txt |
AC |
1903 ms |
180992 KiB |
| test_0025.txt |
AC |
1888 ms |
179264 KiB |
| test_0026.txt |
AC |
1797 ms |
177984 KiB |
| test_0027.txt |
AC |
1857 ms |
180956 KiB |
| test_0028.txt |
AC |
1816 ms |
178496 KiB |
| test_0029.txt |
AC |
1905 ms |
179264 KiB |
| test_0030.txt |
AC |
1900 ms |
181564 KiB |
| test_0031.txt |
AC |
1915 ms |
180368 KiB |
| test_0032.txt |
AC |
1958 ms |
181220 KiB |
| test_0033.txt |
AC |
1882 ms |
180576 KiB |
| test_0034.txt |
AC |
1894 ms |
180888 KiB |
| test_0035.txt |
AC |
1819 ms |
177232 KiB |
| test_0036.txt |
AC |
1838 ms |
179076 KiB |
| test_0037.txt |
AC |
1808 ms |
177748 KiB |
| test_0038.txt |
AC |
1866 ms |
181344 KiB |
| test_0039.txt |
AC |
1872 ms |
181280 KiB |
| test_0040.txt |
AC |
1838 ms |
177788 KiB |
| test_0041.txt |
AC |
1869 ms |
181132 KiB |
| test_0042.txt |
AC |
1906 ms |
179968 KiB |
| test_0043.txt |
AC |
1873 ms |
181028 KiB |
| test_0044.txt |
AC |
1950 ms |
181160 KiB |
| test_0045.txt |
AC |
1868 ms |
179312 KiB |
| test_0046.txt |
AC |
1846 ms |
180256 KiB |
| test_0047.txt |
AC |
1862 ms |
180236 KiB |
| test_0048.txt |
AC |
1851 ms |
181608 KiB |
| test_0049.txt |
AC |
1787 ms |
179148 KiB |
| test_0050.txt |
AC |
1872 ms |
182008 KiB |
| test_0051.txt |
AC |
1827 ms |
179532 KiB |
| test_0052.txt |
AC |
1895 ms |
178136 KiB |
| test_0053.txt |
AC |
1882 ms |
179532 KiB |
| test_0054.txt |
AC |
1858 ms |
181472 KiB |
| test_0055.txt |
AC |
1846 ms |
178896 KiB |
| test_0056.txt |
AC |
1905 ms |
180832 KiB |
| test_0057.txt |
AC |
1873 ms |
179348 KiB |
| test_0058.txt |
AC |
1870 ms |
180252 KiB |
| test_0059.txt |
AC |
1832 ms |
179904 KiB |
| test_0060.txt |
AC |
1837 ms |
177768 KiB |
| test_0061.txt |
AC |
1845 ms |
178592 KiB |
| test_0062.txt |
AC |
1851 ms |
178816 KiB |
| test_0063.txt |
AC |
2000 ms |
180152 KiB |
| test_0064.txt |
AC |
1885 ms |
178496 KiB |
| test_0065.txt |
AC |
1881 ms |
179732 KiB |
| test_0066.txt |
AC |
1957 ms |
181236 KiB |
| test_0067.txt |
AC |
1779 ms |
175820 KiB |
| test_0068.txt |
AC |
1842 ms |
179924 KiB |
| test_0069.txt |
AC |
1877 ms |
178644 KiB |
| test_0070.txt |
AC |
1872 ms |
177308 KiB |
| test_0071.txt |
AC |
1839 ms |
180164 KiB |
| test_0072.txt |
AC |
1903 ms |
179980 KiB |
| test_0073.txt |
AC |
1886 ms |
179572 KiB |
| test_0074.txt |
AC |
1883 ms |
181172 KiB |
| test_0075.txt |
AC |
1851 ms |
180924 KiB |
| test_0076.txt |
AC |
1882 ms |
180704 KiB |
| test_0077.txt |
AC |
1828 ms |
178496 KiB |
| test_0078.txt |
AC |
1950 ms |
181596 KiB |
| test_0079.txt |
AC |
1890 ms |
181412 KiB |
| test_0080.txt |
AC |
1864 ms |
179948 KiB |
| test_0081.txt |
AC |
1884 ms |
178500 KiB |
| test_0082.txt |
AC |
1854 ms |
180560 KiB |
| test_0083.txt |
AC |
1975 ms |
180252 KiB |
| test_0084.txt |
AC |
1784 ms |
176728 KiB |
| test_0085.txt |
AC |
1811 ms |
178240 KiB |
| test_0086.txt |
AC |
1860 ms |
180436 KiB |
| test_0087.txt |
AC |
1802 ms |
175984 KiB |
| test_0088.txt |
AC |
1814 ms |
178932 KiB |
| test_0089.txt |
AC |
1900 ms |
181564 KiB |
| test_0090.txt |
AC |
1793 ms |
176376 KiB |
| test_0091.txt |
AC |
1815 ms |
180816 KiB |
| test_0092.txt |
AC |
1872 ms |
180452 KiB |
| test_0093.txt |
AC |
1877 ms |
180984 KiB |
| test_0094.txt |
AC |
1915 ms |
181244 KiB |
| test_0095.txt |
AC |
1923 ms |
180792 KiB |
| test_0096.txt |
AC |
1883 ms |
180968 KiB |
| test_0097.txt |
AC |
1847 ms |
179040 KiB |
| test_0098.txt |
AC |
1835 ms |
179164 KiB |
| test_0099.txt |
AC |
1872 ms |
179848 KiB |