QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#198055#3858. Systematic salesmanForever_YoungML 40ms194868kbC++143.6kb2023-10-03 00:30:462023-10-03 00:30:46

Judging History

你现在查看的是最新测评结果

  • [2023-10-03 00:30:46]
  • 评测
  • 测评结果:ML
  • 用时:40ms
  • 内存:194868kb
  • [2023-10-03 00:30:46]
  • 提交

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);
}
D tmp[N][N];
int tmpu[N][N];
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{mnp[x], x};
					py = pii{y, mnp[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];
				D d = mp[dep + 1][x][y] + dis[y][z];
				if(d < tmp[x][z]) {
					tmp[x][z] = d;
					tmpu[x][z] = y;
				}
			}
		}
	}

	for(int i = le; i < mid; i++) {
		int x = o[i];
		for(int k = mid; k < ri; k++) {
			int z = o[k];
			for(int l = mid; l < ri; l++) {
				int w = o[l];
				D d = tmp[x][z] + mp[dep + 1][z][w];
				//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{tmpu[x][z], z};
					mp[dep][w][x] = d;
					u[dep][w][x] = pii{z, tmpu[x][z]};
				}
			}
		}
	}
	for(int i = le; i < mid; i++) {
		int x = o[i];
		for(int k = mid; k < ri; k++) {
			int z = o[k];
			tmp[x][z] = 1e50;
		}
	}

}
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;
				tmp[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;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 38724kb

input:

8
7 3
2 0
4 5
1 4
8 2
9 9
0 8
6 1

output:

26.383325771613
7 3 4 2 8 5 1 6

result:

ok correct!

Test #2:

score: 0
Accepted
time: 0ms
memory: 44764kb

input:

20
4 71
52 7
49 15
59 83
12 9
46 6
74 44
89 50
32 10
82 58
11 33
78 72
27 49
64 75
97 0
38 46
91 54
8 70
18 61
79 92

output:

374.883681117480
1 18 19 13 16 11 5 9 3 6 2 7 15 8 17 10 12 20 4 14

result:

ok correct!

Test #3:

score: 0
Accepted
time: 6ms
memory: 69564kb

input:

100
192 64
839 68
846 688
911 133
110 439
592 226
355 364
418 487
402 466
436 425
509 847
542 78
648 404
954 313
726 906
777 922
596 550
159 172
507 651
720 932
575 805
889 193
246 206
175 326
897 464
108 70
790 2
548 624
867 222
743 269
41 98
348 173
49 915
35 939
404 571
371 625
363 758
317 155
90...

output:

8491.452160233977
31 26 86 1 98 18 93 79 5 45 90 24 23 38 42 57 32 56 85 49 67 7 10 9 41 63 83 62 96 47 39 33 34 48 72 91 60 61 88 11 92 80 99 37 89 36 35 8 82 19 75 28 44 17 71 78 55 97 69 50 58 21 15 20 81 16 74 66 76 87 3 46 54 77 95 68 73 25 52 43 14 84 4 22 29 40 2 27 100 53 12 65 6 30 64 51 13...

result:

ok correct!

Test #4:

score: 0
Accepted
time: 40ms
memory: 194868kb

input:

500
380 190
314 738
973 705
875 517
593 638
132 681
714 411
400 589
139 353
339 771
832 883
610 170
925 431
351 96
884 683
674 817
5 386
710 99
348 173
996 812
533 453
851 10
877 142
86 361
860 168
489 50
641 766
158 118
989 745
823 559
374 971
605 158
432 307
931 863
719 635
73 341
726 906
536 372
...

output:

22821.888978355535
268 48 322 189 168 140 264 178 400 430 112 8 162 407 355 111 329 269 472 242 123 195 102 361 311 478 245 413 491 452 261 434 397 460 151 344 485 240 193 489 218 391 464 97 342 494 145 31 373 190 471 403 169 251 385 422 382 395 10 50 155 2 461 60 199 126 236 91 401 276 324 222 328 ...

result:

ok correct!

Test #5:

score: -100
Memory Limit Exceeded

input:

1000
818 537
491 340
916 881
776 67
368 978
888 853
8 349
561 929
604 8
349 828
874 894
257 757
667 962
242 746
3 614
931 863
235 578
516 580
61 177
13 821
949 165
231 732
970 21
711 731
392 374
878 672
106 596
82 166
149 539
944 485
481 675
845 636
352 185
326 4
229 472
617 972
622 175
83 554
447 7...

output:

32684.782659301327
763 794 126 659 675 593 108 402 34 998 45 259 781 715 943 896 220 520 961 459 410 590 700 477 383 33 446 845 834 359 428 461 247 690 78 362 418 805 233 701 955 216 806 403 429 57 176 608 460 363 162 582 324 159 124 509 371 823 737 473 239 542 725 237 513 932 971 934 565 113 512 72...

result: