Skip to content

Commit 691816d

Browse files
authored
Merge pull request #515 from xirc/atcoder/abc180E
AtCoder/ABC180E
2 parents 03d9443 + 4d69560 commit 691816d

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

atcoder/abc180/E/main.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
using ll = int64_t;
5+
using ff = long double;
6+
7+
int N;
8+
vector<int> X, Y, Z;
9+
10+
inline int distance(int s, int d) {
11+
return abs(X[s] - X[d]) + abs(Y[s] - Y[d]) + max(0, Z[d] - Z[s]);
12+
}
13+
14+
int solve() {
15+
int const inf = 1e8;
16+
int const M = 1 << N;
17+
vector<vector<int>> DP(N, vector<int>(M, inf));
18+
DP[0][1] = 0;
19+
for (int s = 0; s < M; ++s) {
20+
for (int i = 0; i < N; ++i) {
21+
if ((s&(1<<i)) == 1) continue;
22+
for (int j = 0; j < N; ++j) {
23+
if(i == j) continue;
24+
if ((s&(1<<j)) == 0) continue;
25+
int ns = s|(1<<i);
26+
DP[i][ns] = min(DP[i][ns], DP[j][s] + distance(j,i));
27+
}
28+
}
29+
}
30+
int ans = inf;
31+
for (int i = 1; i < N; ++i) {
32+
ans = min(ans, DP[i][M-1] + distance(i,0));
33+
}
34+
return ans;
35+
}
36+
37+
38+
int main() {
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(0); cout.tie(0);
41+
42+
cin >> N;
43+
X.assign(N, 0);
44+
Y.assign(N, 0);
45+
Z.assign(N, 0);
46+
for (int i = 0; i < N; ++i) {
47+
cin >> X[i] >> Y[i] >> Z[i];
48+
}
49+
cout << solve() << endl;
50+
51+
return 0;
52+
}

0 commit comments

Comments
 (0)