QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187798 | #3858. Systematic salesman | flyfeather6 | AC ✓ | 420ms | 226564kb | C++14 | 3.3kb | 2023-09-24 22:49:30 | 2023-09-24 22:49:30 |
Judging History
answer
#include <bits/stdc++.h>
#define mp make_pair
#define double long double
using namespace std;
typedef pair<int,int> pii;
const double INF=1000000000000000,eps=1e-8;
const int W=12,N=1024;
double tmp[N][N],f[W+1][N][N];
int n;
struct point{
int x,y,idn;
}a[N];
bool cmp0(point x,point y){
return x.x>y.x;
}
bool cmp1(point x,point y){
return x.y>y.y;
}
double dis(point x,point y){
return sqrt(1.0*(x.x-y.x)*(x.x-y.x)+1.0*(x.y-y.y)*(x.y-y.y));
}
void solve(int L,int R,int now){
if (L==R){
f[now][L][R]=0;
return;
}
if (now%2==0) std::sort(a+L,a+R+1,cmp0);
else std::sort(a+L,a+R+1,cmp1);
//printf("::%d %d %d\n",L,R,now);
//for (int i=L;i<=R;i++) printf("%d ",a[i].idn);
//puts("");
int mid=(L+R)>>1;
solve(L,mid,now+1);
solve(mid+1,R,now+1);
//printf("::%d %d %d\n",L,R,now);
//for (int i=L;i<=R;i++) printf("%d ",a[i].idn);
//puts("");
if (L+1==R){
f[now][L][R]=dis(a[L],a[R]);
return;
}
if (L+2==R){
f[now][L][R]=f[now+1][L][L+1]+dis(a[L+1],a[R]);
f[now][L+1][R]=f[now+1][L][L+1]+dis(a[L],a[R]);
return;
}
int midl=(L+mid)>>1,midr=(mid+1+R)>>1;
//f[i][j]+dis(j,k)+f[k][w]
//tmp[i][k]->min(f[i][j]+dis(j,k)
for (int i=L;i<=mid;i++)
for (int j=mid+1;j<=R;j++)
tmp[i][j]=INF;
for (int i=L;i<=midl;i++){
for (int j=midl+1;j<=mid;j++)
for (int k=mid+1;k<=R;k++){
double tt=f[now+1][i][j]+dis(a[j],a[k]);//i-j-k
if (tt<tmp[i][k]){
tmp[i][k]=tt;
}
tt=f[now+1][i][j]+dis(a[i],a[k]);//j-i-k
if (tt<tmp[j][k]){
tmp[j][k]=tt;
}
}
}
//f[type][i][j]=min(tmp[i][k]+f[k][w])
for (int i=L;i<=mid;i++)
for (int j=mid+1;j<=midr;j++)
for (int k=midr+1;k<=R;k++){
//i->tran[i][j]->j->k
double tt=tmp[i][j]+f[now+1][j][k];
if (tt<f[now][i][k]){
f[now][i][k]=tt;
}
//i->tran[i][k]->k->j
tt=tmp[i][k]+f[now+1][j][k];
if (tt<f[now][i][j]){
f[now][i][j]=tt;
}
}
}
double calc(int st,int k,int w,int ed,int now){
return f[now][st][k]+f[now][w][ed]+dis(a[k],a[w]);
}
void pri(int L,int R,int st,int ed,int now){
//cerr<<"?? "<<st<<" "<<ed<<"\n";
if (L==R){
printf("%d ",a[L].idn);
return;
}
if (L+1==R){
printf("%d %d ",a[st].idn,a[ed].idn);
return;
}
int mid=(L+R)>>1;
//st->k->w->ed
if (st<ed){
for (int i=L;i<=mid;i++)
for (int j=mid+1;j<=R;j++){
if (fabs(calc(st,i,j,ed,now+1)-f[now][st][ed])<eps){
pri(L,mid,st,i,now+1);
pri(mid+1,R,j,ed,now+1);
return;
}
}
}else{
for (int i=mid+1;i<=R;i++)
for (int j=L;j<=mid;j++){
if (fabs(calc(st,i,j,ed,now+1)-f[now][st][ed])<eps){
pri(mid+1,R,st,i,now+1);
pri(L,mid,j,ed,now+1);
return;
}
}
}
}
int main(){
scanf("%d",&n);
if(n==1)
{
puts("0");
puts("1");
return 0;
}
for (int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].idn=i;
}
for (int w=0;w<=W;w++)
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
f[w][i][j]=f[w][i][j]=INF;
solve(1,n,0);
double Ans=INF;
pii gans;
int mid=(1+n)>>1,st,ed;
for (int i=1;i<=mid;i++)
for (int j=mid+1;j<=n;j++)
if (f[0][i][j]<Ans){
Ans=f[0][i][j];
st=i,ed=j;
}
printf("%.8Lf\n",Ans);
for (int w=0;w<=W;w++)
for (int i=1;i<=n;i++)
for (int j=i+1;j<=n;j++)
f[w][j][i]=f[w][i][j];
pri(1,n,st,ed,0);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 59224kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.38332577 6 1 5 8 2 4 3 7
result:
ok correct!
Test #2:
score: 0
Accepted
time: 0ms
memory: 30632kb
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.88368112 14 4 20 12 10 17 8 15 7 2 6 3 9 5 11 16 13 19 18 1
result:
ok correct!
Test #3:
score: 0
Accepted
time: 0ms
memory: 59460kb
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.45216023 94 59 70 13 51 64 30 6 65 12 53 100 27 2 40 29 22 4 84 14 43 52 25 73 68 95 77 54 46 3 87 76 66 74 16 81 20 15 21 58 50 69 97 55 78 71 17 44 28 75 19 82 8 35 36 89 37 99 80 92 11 88 61 60 91 72 48 34 33 39 47 96 62 83 63 41 9 10 7 67 49 85 56 32 57 42 38 23 24 90 45 5 79 93 18 98 1 86 ...
result:
ok correct!
Test #4:
score: 0
Accepted
time: 70ms
memory: 145372kb
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.88897836 65 67 29 163 378 480 3 64 441 104 396 105 301 343 476 179 223 334 30 278 4 398 157 15 281 340 379 52 78 141 377 288 266 437 129 213 20 181 500 327 234 315 285 496 448 270 34 318 143 134 433 136 307 115 62 11 409 386 202 436 390 249 122 165 482 280 462 194 402 467 37 146 432 175 487 38...
result:
ok correct!
Test #5:
score: 0
Accepted
time: 395ms
memory: 226128kb
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.78265930 915 327 292 156 727 752 92 917 577 264 965 899 484 902 831 648 44 562 708 797 869 271 508 949 762 146 453 837 253 115 369 620 822 208 551 773 419 236 260 880 395 860 885 312 599 789 431 756 958 130 166 405 348 598 226 86 257 592 154 135 279 103 334 922 302 765 646 758 337 889 416 711 ...
result:
ok correct!
Test #6:
score: 0
Accepted
time: 420ms
memory: 225804kb
input:
1000 94954 408313 589670 644618 101544 170845 308094 798263 871557 182716 42389 153936 777317 429523 812471 38482 979047 249000 967597 351300 982071 356744 369070 837238 661606 876392 70400 544589 460840 381840 151672 220775 539578 774105 717079 505259 241023 619236 318139 186353 39127 718711 697393...
output:
32281914.25242373 996 161 990 503 22 836 748 567 420 209 837 955 702 689 571 338 255 882 72 285 915 632 232 656 910 814 373 819 201 178 549 98 850 776 429 487 299 878 623 779 323 514 124 107 376 2 80 513 591 381 844 189 659 508 252 706 442 998 62 344 17 709 211 932 77 392 968 34 515 195 473 451 400 ...
result:
ok correct!
Test #7:
score: 0
Accepted
time: 413ms
memory: 226192kb
input:
1000 1097 1097 1661 1661 1121 1121 1377 1377 1541 1541 1907 1907 1796 1796 1547 1547 1376 1376 1992 1992 1317 1317 1762 1762 1561 1561 1794 1794 1874 1874 1577 1577 1688 1688 1650 1650 1460 1460 1062 1062 1247 1247 1596 1596 1996 1996 1146 1146 1452 1452 1190 1190 1839 1839 1799 1799 1346 1346 1889 ...
output:
1412.79934881 138 146 382 23 755 773 788 10 596 576 318 305 551 825 40 220 941 838 225 691 149 290 371 346 854 58 258 271 411 871 534 265 157 151 720 65 858 376 975 161 741 673 54 634 434 262 400 569 59 126 757 456 514 260 712 283 944 958 875 663 86 342 356 946 452 474 824 114 219 850 409 747 839 77...
result:
ok correct!
Test #8:
score: 0
Accepted
time: 410ms
memory: 226184kb
input:
1000 1640 360 1469 531 1967 33 1800 200 1807 193 1265 735 1178 822 1747 253 1327 673 1164 836 1188 812 1623 377 1684 316 1806 194 1577 423 1915 85 1380 620 1033 967 1510 490 1213 787 1363 637 1751 249 1944 56 1252 748 1044 956 1158 842 1484 516 1242 758 1991 9 1212 788 1446 554 1576 424 1683 317 127...
output:
1412.79934881 414 749 122 507 128 855 558 576 29 517 876 958 721 69 487 61 363 719 351 834 190 957 992 87 418 150 851 873 385 376 36 954 3 449 527 492 817 138 202 237 273 597 63 809 726 662 783 886 619 635 524 935 880 901 952 23 500 807 164 763 405 106 286 861 357 555 355 365 521 767 944 928 995 450...
result:
ok correct!
Test #9:
score: 0
Accepted
time: 416ms
memory: 226564kb
input:
1000 921 1079 860 860 815 1185 821 1179 50 50 311 1689 244 244 788 788 508 508 934 934 845 1155 584 584 170 170 589 1411 605 1395 88 88 439 1561 593 1407 842 842 647 1353 64 64 93 1907 930 930 730 730 328 328 151 1849 354 354 599 1401 849 1151 457 1543 808 808 469 1531 119 1881 604 604 130 130 305 1...
output:
4400.59954671 660 512 580 247 427 664 557 518 905 720 311 208 793 652 43 817 538 834 293 937 285 928 476 144 945 974 849 493 674 382 300 124 108 931 626 387 506 741 348 936 194 308 717 276 14 400 18 611 635 28 699 333 15 546 565 82 363 211 804 478 698 473 973 657 112 633 895 288 923 926 370 179 784 ...
result:
ok correct!
Test #10:
score: 0
Accepted
time: 409ms
memory: 226252kb
input:
1000 208821 93534 971563 333783 973615 339722 964813 684252 218789 913425 751635 932065 816127 887381 657687 25516 515910 253 381949 14136 648326 977493 348288 976428 993310 581520 284456 48845 42 493513 828139 877260 6188 421581 118647 823372 989248 603131 927245 240266 459001 998316 434015 995627 ...
output:
3138446.52604647 748 755 333 379 358 314 869 440 304 498 898 343 227 414 352 425 899 622 246 648 378 984 577 994 815 585 353 507 412 624 417 884 348 893 282 291 983 730 373 606 210 337 382 476 836 496 53 11 688 796 182 264 625 870 519 997 555 465 84 617 167 248 265 940 750 663 903 260 190 326 583 24...
result:
ok correct!