from itertools import permutations import sys from time import time from random import random, randrange, choice, choices, randint from heapq import heappop, heappush from math import exp START = time() INF=10**9 def get_time(START): return time() - START def dist(p1,p2): x1,y1=p1 x2,y2=p2 return (x1-x2)**2 + (y1-y2)**2 def cost(i: int, j: int): d = dist(Terminals[i], Terminals[j]) if i=TerminalsStation[0]:s+=1 station_list.append(s+N) station_list.append(TerminalsStation[0]+N) d = calc_score2(station_list) if d0: best_score = current_score update_cnt += 1 #print("swap",current_score, file=sys.stderr) else: ans[v1],ans[v2] = ans[v2],ans[v1] else: #randomでクラスターを選んでその中でSwap v1=randint(1,n-1) v2 = choice(StationCluster[TerminalsStation[ans[v1]]]) while ans[v1]==v2 or v2 == 0: v2 = choice(StationCluster[TerminalsStation[ans[v1]]]) ans[v1],ans[Tind[v2]] = ans[Tind[v2]],ans[v1] current_score = calc_score2(ans) diff = best_score - current_score if diff>0: best_score = current_score update_cnt += 1 #print("swap",current_score, file=sys.stderr) else: ans[v1],ans[Tind[v2]] = ans[Tind[v2]],ans[v1] # アウトプット ans.append(0) output=[0] for i in range(len(ans)-1): output+=dijkstra(ans[i],ans[i+1],Ecost[ans[i]][ans[i+1]]) loopcnt2=0 while get_time(START)<0.85: loopcnt2+=1 v = randrange(N,N+M) original = Terminals[v][:] best_i = -1 sub_best_score = best_score for i in range(8): Terminals[v] = (original[0]+dx[i],original[1]+dy[i]) current_score = calc_score(output) if current_score < sub_best_score: sub_best_score = current_score best_i = i if best_i != -1: best_score = sub_best_score Terminals[v] = (original[0]+dx[best_i], original[1]+dy[best_i]) #print("move",best_score, file=sys.stderr) else: Terminals[v]=original[:] for pos in Terminals[N:]: print(*pos) print(len(output)) for v in output: if v