#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fixbug false
void SETIO(string name = ""){
if (name=="") return;
freopen((name+".inp").c_str(),"r",stdin);
// freopen((name+".out").c_str(),"w",stdout);
return;
}
#define fi first
#define se second
#define MASK(x) ((ll)(1)<<(x))
#define BIT(mask,x) (((mask)>>(x))&(1))
const int maxn = 20;
ll dp[maxn+2][MASK(maxn)];
int n , x[maxn+2] , y[maxn+2];
vector<pair<int,int>> p;
int cost(int x1 , int y1 , int x2 , int y2){
return abs(x1 - x2) + abs(y1 - y2);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
SETIO("");
cin >> n;
for (int i = 1; i <= 2*n; ++i) cin >> x[i] >> y[i];
memset(dp,0x3f,sizeof dp);
for (int i = 1; i <= n; ++i) for (int j = 1 ; j <= 2; ++j) p.push_back({i,j});
dp[0][0] = 0;
for (int t = 1; t <= 2*n; ++t) {
for (int mask = 0; mask < MASK(2*n); ++mask){
if (__builtin_popcount(mask)!=t) continue;
for (int i = 0; i < 2 * n; ++i) if (BIT(mask,i)){
dp[t][mask] = min(dp[t][mask] , dp[t - 1][mask^MASK(i)] + cost(p[i].fi , p[i].se , x[t] , y[t]));
}
}
}
if (fixbug){
cout << "(DEBUG)\n";
for (int layer = 1; layer <= 2 * n; ++layer){
for (int mask = 0; mask < MASK(2*n); ++mask){
cout << dp[layer][mask] << ' ';
}
cout << '\n';
}
}
cout << dp[2 * n][MASK(2 * n) - 1];
}
Compilation message
joi2019_ho_t4.cpp: In function 'void SETIO(std::string)':
joi2019_ho_t4.cpp:8:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen((name+".inp").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
180772 KB |
Output is correct |
2 |
Correct |
75 ms |
180816 KB |
Output is correct |
3 |
Correct |
75 ms |
180832 KB |
Output is correct |
4 |
Correct |
209 ms |
180820 KB |
Output is correct |
5 |
Correct |
209 ms |
180820 KB |
Output is correct |
6 |
Correct |
206 ms |
180964 KB |
Output is correct |
7 |
Correct |
211 ms |
180820 KB |
Output is correct |
8 |
Correct |
216 ms |
180816 KB |
Output is correct |
9 |
Correct |
205 ms |
180828 KB |
Output is correct |
10 |
Correct |
210 ms |
180844 KB |
Output is correct |
11 |
Correct |
211 ms |
180920 KB |
Output is correct |
12 |
Correct |
206 ms |
180820 KB |
Output is correct |
13 |
Correct |
198 ms |
180916 KB |
Output is correct |
14 |
Correct |
207 ms |
180816 KB |
Output is correct |
15 |
Correct |
200 ms |
180816 KB |
Output is correct |
16 |
Correct |
202 ms |
180816 KB |
Output is correct |
17 |
Correct |
212 ms |
180820 KB |
Output is correct |
18 |
Correct |
199 ms |
180896 KB |
Output is correct |
19 |
Correct |
201 ms |
180820 KB |
Output is correct |
20 |
Correct |
208 ms |
181192 KB |
Output is correct |
21 |
Correct |
213 ms |
180820 KB |
Output is correct |
22 |
Correct |
210 ms |
180820 KB |
Output is correct |
23 |
Correct |
201 ms |
180816 KB |
Output is correct |
24 |
Correct |
206 ms |
180832 KB |
Output is correct |
25 |
Correct |
210 ms |
180816 KB |
Output is correct |
26 |
Correct |
198 ms |
180816 KB |
Output is correct |
27 |
Correct |
206 ms |
180908 KB |
Output is correct |
28 |
Correct |
207 ms |
180924 KB |
Output is correct |
29 |
Correct |
195 ms |
180816 KB |
Output is correct |
30 |
Correct |
206 ms |
180816 KB |
Output is correct |
31 |
Correct |
218 ms |
180820 KB |
Output is correct |
32 |
Correct |
229 ms |
180816 KB |
Output is correct |
33 |
Correct |
226 ms |
180892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
180772 KB |
Output is correct |
2 |
Correct |
75 ms |
180816 KB |
Output is correct |
3 |
Correct |
75 ms |
180832 KB |
Output is correct |
4 |
Correct |
209 ms |
180820 KB |
Output is correct |
5 |
Correct |
209 ms |
180820 KB |
Output is correct |
6 |
Correct |
206 ms |
180964 KB |
Output is correct |
7 |
Correct |
211 ms |
180820 KB |
Output is correct |
8 |
Correct |
216 ms |
180816 KB |
Output is correct |
9 |
Correct |
205 ms |
180828 KB |
Output is correct |
10 |
Correct |
210 ms |
180844 KB |
Output is correct |
11 |
Correct |
211 ms |
180920 KB |
Output is correct |
12 |
Correct |
206 ms |
180820 KB |
Output is correct |
13 |
Correct |
198 ms |
180916 KB |
Output is correct |
14 |
Correct |
207 ms |
180816 KB |
Output is correct |
15 |
Correct |
200 ms |
180816 KB |
Output is correct |
16 |
Correct |
202 ms |
180816 KB |
Output is correct |
17 |
Correct |
212 ms |
180820 KB |
Output is correct |
18 |
Correct |
199 ms |
180896 KB |
Output is correct |
19 |
Correct |
201 ms |
180820 KB |
Output is correct |
20 |
Correct |
208 ms |
181192 KB |
Output is correct |
21 |
Correct |
213 ms |
180820 KB |
Output is correct |
22 |
Correct |
210 ms |
180820 KB |
Output is correct |
23 |
Correct |
201 ms |
180816 KB |
Output is correct |
24 |
Correct |
206 ms |
180832 KB |
Output is correct |
25 |
Correct |
210 ms |
180816 KB |
Output is correct |
26 |
Correct |
198 ms |
180816 KB |
Output is correct |
27 |
Correct |
206 ms |
180908 KB |
Output is correct |
28 |
Correct |
207 ms |
180924 KB |
Output is correct |
29 |
Correct |
195 ms |
180816 KB |
Output is correct |
30 |
Correct |
206 ms |
180816 KB |
Output is correct |
31 |
Correct |
218 ms |
180820 KB |
Output is correct |
32 |
Correct |
229 ms |
180816 KB |
Output is correct |
33 |
Correct |
226 ms |
180892 KB |
Output is correct |
34 |
Incorrect |
89 ms |
181072 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
180772 KB |
Output is correct |
2 |
Correct |
75 ms |
180816 KB |
Output is correct |
3 |
Correct |
75 ms |
180832 KB |
Output is correct |
4 |
Correct |
209 ms |
180820 KB |
Output is correct |
5 |
Correct |
209 ms |
180820 KB |
Output is correct |
6 |
Correct |
206 ms |
180964 KB |
Output is correct |
7 |
Correct |
211 ms |
180820 KB |
Output is correct |
8 |
Correct |
216 ms |
180816 KB |
Output is correct |
9 |
Correct |
205 ms |
180828 KB |
Output is correct |
10 |
Correct |
210 ms |
180844 KB |
Output is correct |
11 |
Correct |
211 ms |
180920 KB |
Output is correct |
12 |
Correct |
206 ms |
180820 KB |
Output is correct |
13 |
Correct |
198 ms |
180916 KB |
Output is correct |
14 |
Correct |
207 ms |
180816 KB |
Output is correct |
15 |
Correct |
200 ms |
180816 KB |
Output is correct |
16 |
Correct |
202 ms |
180816 KB |
Output is correct |
17 |
Correct |
212 ms |
180820 KB |
Output is correct |
18 |
Correct |
199 ms |
180896 KB |
Output is correct |
19 |
Correct |
201 ms |
180820 KB |
Output is correct |
20 |
Correct |
208 ms |
181192 KB |
Output is correct |
21 |
Correct |
213 ms |
180820 KB |
Output is correct |
22 |
Correct |
210 ms |
180820 KB |
Output is correct |
23 |
Correct |
201 ms |
180816 KB |
Output is correct |
24 |
Correct |
206 ms |
180832 KB |
Output is correct |
25 |
Correct |
210 ms |
180816 KB |
Output is correct |
26 |
Correct |
198 ms |
180816 KB |
Output is correct |
27 |
Correct |
206 ms |
180908 KB |
Output is correct |
28 |
Correct |
207 ms |
180924 KB |
Output is correct |
29 |
Correct |
195 ms |
180816 KB |
Output is correct |
30 |
Correct |
206 ms |
180816 KB |
Output is correct |
31 |
Correct |
218 ms |
180820 KB |
Output is correct |
32 |
Correct |
229 ms |
180816 KB |
Output is correct |
33 |
Correct |
226 ms |
180892 KB |
Output is correct |
34 |
Incorrect |
89 ms |
181072 KB |
Output isn't correct |
35 |
Halted |
0 ms |
0 KB |
- |