QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#198050 | #3858. Systematic salesman | Forever_Young# | WA | 0ms | 36732kb | C++14 | 3.2kb | 2023-10-03 00:07:52 | 2023-10-03 00:07:53 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
typedef long double D;
typedef pair<int, int> pii;
int nc = 0;
const int N = 1011;
const int LOG = 12;
D mp[LOG][N][N];
pii u[LOG][N][N];
struct H {
size_t operator()(const pii & k) const {
return hash<int>()(k.fi * N + k.se);
}
};
int x[N][2], o[N], ls[N], rs[N];
D dis[N][N];
vector<int> vec;
void print(pii p, int dep) {
if(p.fi == p.se) {
vec.pb(p.fi);
return;
}
pii u = ::u[dep][p.fi][p.se];
pii q{p.fi, u.fi}, r{u.se, p.se};
print(q, dep + 1);
print(r, dep + 1);
}
void solve(int le, int ri, int d, int dep) {
//printf("%d %d %d %d\n", le, ri, d, dep);
if(le == ri - 1) {
mp[dep][o[le]][o[le]] = 0;
//printf("!%d\n", o[le]);
return;
}
sort(o + le, o + ri, [&](int a, int b) { return x[a][d] < x[b][d]; });
int mid = (ri + le) / 2;
solve(le, mid, d ^ 1, dep + 1);
solve(mid, ri, d ^ 1, dep + 1);
if(dep == 0) {
static D mn[N];
static int mnp[N];
for(int i = le; i < ri; i++) {
int x = o[i];
mn[x] = 1e50;
mnp[x] = -1;
for(int j = le; j < ri; j++) {
int y = o[j];
if(mp[1][x][y] < mn[x]) {
mn[x] = mp[1][x][y];
mnp[x] = y;
}
}
}
D ans = 1e50;
pii px, py;
for(int i = le; i < mid; i++) {
for(int j = mid; j < ri; j++) {
int x = o[i];
int y = o[j];
if(mn[x] + mn[y] + dis[x][y] < ans) {
ans = mn[x] + mn[y] + dis[x][y];
px = pii{x, mnp[x]};
py = pii{mnp[y], y};
}
}
}
printf("%.12f\n", (double)ans);
print(px, 1);
print(py, 1);
return;
}
//if(dep >= 9) printf("!!!!!!!!%d %d %d %d %d\n", le, mid, ri, o[le], o[mid]);
for(int i = le; i < mid; i++) {
int x = o[i];
for(int j = le; j < mid; j++) {
int y = o[j];
for(int k = mid; k < ri; k++) {
int z = o[k];
for(int l = mid; l < ri; l++) {
int w = o[l];
D d = mp[dep + 1][x][y] + mp[dep + 1][z][w] + dis[y][z];
//if(dep >= 9) printf("%d %d %d %d %lf %lf\n", x, y, z, w, (double)d, (double)mp[dep][x][w]);
//printf("%lf %lf %lf %lf %lf\n", (double)mp[dep][x][w], (double)mp[dep + 1][x][y], (double)mp[dep + 1][z][w], (double)dis[y][z], (double)d);
if(d < mp[dep][x][w]) {
//if(dep >= 9) printf("%d %d->%d = %lf\n", dep, x, w, (double)d);
mp[dep][x][w] = d;
u[dep][x][w] = pii{y, z};
mp[dep][w][x] = d;
u[dep][w][x] = pii{z, y};
}
}
}
}
}
}
int main() {
int n;
scanf("%d", &n);
//n = 1000;
for(int i = 0; i < LOG; i++) {
for(int j = 0; j < n; j++) {
for(int k = 0; k < n; k++) {
mp[i][j][k] = 1e50;
}
}
}
if(n == 1) { printf("0\n1\n"); exit(0); }
for(int i = 0; i < n; i++) {
scanf("%d%d", &x[i][0], &x[i][1]);
//x[i][0] = i;
//x[i][1] = i;
o[i] = i;
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
dis[i][j] = (x[i][0] - x[j][0]) * (x[i][0] - x[j][0]) + (x[i][1] - x[j][1]) * (x[i][1] - x[j][1]);
dis[i][j] = sqrt(dis[i][j]);
}
}
solve(0, n, 0, 0);
for(int i = 0; i < n; i++) {
printf("%d%c", vec[i] + 1, i == n - 1 ? '\n' : ' ');
}
//cout << clock() << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 36732kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.383325771613 2 4 3 7 6 1 5 8
result:
wrong answer you reported 26.383325772 but the real length of your path is 31.315605284