Submission #22131725
Source Code Expand
Copy
def main():N = int(input())city = [[0] * 3 for i in range(N)]for i in range(N):city[i][0],city[i][1],city[i][2] = map(int, input().split())inf = 10**20dp = [[inf] * N for i in range(1<<N)]#各状態と最終都市を表したリストdp[1][0] = 0 #スタート地点のコストを0にfor s in range(1,1<<N):#集合を添字の小さい順に試すfor i in range(N):#現地点の都市if dp[s][i]!=inf:#到達しているパターンであれば、現地点から到達地点へのコストを求めるfor j in range(N):#行先都市t = s|(1<<j)if s==t:#現地点と到達地点が同じであれば計算不要なためcontinuecontinuea,b,c = city[i]p,q,r = city[j]cost = abs(p - a) + abs(q - b) + max(0, r - c)if dp[t][j] > dp[s][i]+cost:dp[t][j] = dp[s][i]+cost
def main(): N = int(input()) city = [[0] * 3 for i in range(N)] for i in range(N): city[i][0],city[i][1],city[i][2] = map(int, input().split()) inf = 10**20 dp = [[inf] * N for i in range(1<<N)]#各状態と最終都市を表したリスト dp[1][0] = 0 #スタート地点のコストを0に for s in range(1,1<<N):#集合を添字の小さい順に試す for i in range(N):#現地点の都市 if dp[s][i]!=inf:#到達しているパターンであれば、現地点から到達地点へのコストを求める for j in range(N):#行先都市 t = s|(1<<j) if s==t:#現地点と到達地点が同じであれば計算不要なためcontinue continue a,b,c = city[i] p,q,r = city[j] cost = abs(p - a) + abs(q - b) + max(0, r - c) if dp[t][j] > dp[s][i]+cost: dp[t][j] = dp[s][i]+cost ans = 10**20 #上記処理で全ての地点を回った時のコストが分かる #その場所から1の地点に戻った際のトータルコストが低いものが答え for i in range(N): a,b,c = city[i] p,q,r = city[0] cost = abs(p - a) + abs(q - b) + max(0, r - c) ans = min(ans,dp[-1][i]+cost) return ans print(main())
Submission Info
Submission Time | |
---|---|
Task | E - Traveling Salesman among Aerial Cities |
User | H20 |
Language | PyPy3 (7.3.0) |
Score | 500 |
Code Size | 1444 Byte |
Status | AC |
Exec Time | 616 ms |
Memory | 107720 KB |
Judge Result
Set Name | Sample | All | ||||
---|---|---|---|---|---|---|
Score / Max Score | 0 / 0 | 500 / 500 | ||||
Status |
|
|
Set Name | Test Cases |
---|---|
Sample | sample_01.txt, sample_02.txt, sample_03.txt |
All | random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, sample_01.txt, sample_02.txt, sample_03.txt |
Case Name | Status | Exec Time | Memory |
---|---|---|---|
random_01.txt | AC | 588 ms | 107500 KB |
random_02.txt | AC | 51 ms | 62176 KB |
random_03.txt | AC | 561 ms | 107376 KB |
random_04.txt | AC | 590 ms | 107576 KB |
random_05.txt | AC | 583 ms | 107244 KB |
random_06.txt | AC | 50 ms | 62104 KB |
random_07.txt | AC | 590 ms | 107420 KB |
random_08.txt | AC | 148 ms | 72780 KB |
random_09.txt | AC | 616 ms | 107548 KB |
random_10.txt | AC | 54 ms | 62132 KB |
random_11.txt | AC | 604 ms | 107584 KB |
random_12.txt | AC | 97 ms | 69204 KB |
random_13.txt | AC | 599 ms | 107164 KB |
random_14.txt | AC | 53 ms | 62108 KB |
random_15.txt | AC | 606 ms | 107520 KB |
random_16.txt | AC | 117 ms | 71052 KB |
random_17.txt | AC | 581 ms | 107248 KB |
random_18.txt | AC | 118 ms | 71120 KB |
random_19.txt | AC | 569 ms | 107720 KB |
random_20.txt | AC | 139 ms | 73168 KB |
sample_01.txt | AC | 57 ms | 62044 KB |
sample_02.txt | AC | 53 ms | 62060 KB |
sample_03.txt | AC | 580 ms | 107260 KB |