QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#324248 | #3858. Systematic salesman | OrezTsim | WA | 0ms | 12068kb | C++20 | 2.5kb | 2024-02-10 17:22:42 | 2024-02-10 17:22:42 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1010,M=N<<5,inf=1e18;
int n,m,typ;
struct Node {
int x,y,id;
friend bool operator <(const Node &fi,const Node &se) {
if(!typ) return fi.x<se.x;
return fi.y<se.y;
}
} a[N];
double f[N][N],dp[N][N],dis[N][N];
int fc[N][N],dpc[N][N];
auto sq(int x) -> int { return x*x; }
void solve(int l,int r) {
if(l==r) return; int mid=(l+r-1)>>1;
nth_element(a+l,a+mid,a+r+1);
typ^=1,solve(l,mid),solve(mid+1,r),typ^=1;
for(int i=l;i<=mid;++i)
for(int j=mid+1;j<=r;++j)
dis[i][j]=sqrt(sq(a[i].x-a[j].x)+sq(a[i].y-a[j].y));
for(int i=l;i<=mid;++i)
for(int j=mid+1;j<=r;++j)
f[i][j]=dp[i][j]=inf;
if(l<mid) {
int nxt=(l+mid-1)>>1;
for(int i=l;i<=nxt;++i)
for(int j=nxt+1;j<=mid;++j)
for(int k=mid+1;k<=r;++k) {
double val=dp[i][j]+dis[j][k];
if(f[i][k]>val) f[i][k]=val,fc[i][k]=j;
val=dp[i][j]+dis[i][k];
if(f[j][k]>val) f[j][k]=val,fc[j][k]=i;
}
} else for(int j=mid+1;j<=r;++j)
f[l][j]=dis[l][j],fc[l][j]=l;
if(mid+1<r) {
int nxt=(mid+r)>>1;
for(int i=l;i<=mid;++i)
for(int j=mid+1;j<=nxt;++j)
for(int k=nxt+1;k<=r;++k) {
double val=dp[j][k]+f[i][j];
if(dp[i][k]>val) dp[i][k]=val,dpc[i][k]=j;
val=dp[j][k]+f[i][k];
if(dp[i][j]>val) dp[i][j]=val,dpc[i][j]=i;
}
} else for(int i=l;i<=mid;++i)
dp[i][r]=dis[i][r],dpc[i][r]=r;
}
auto pr(int l,int r,int x,int y) -> vector<int> {
if(l==r) return {a[l].id};
bool ok=(x>y); if(ok) swap(x,y);
int mid=(l+r-1)>>1;
int fir=dpc[x][y],sec=fc[x][fir];
vector<int>res=pr(l,mid,x,sec);
for(auto it: pr(mid+1,r,fir,y)) res.push_back(it);
if(ok) reverse(res.begin(),res.end());
return res;
}
auto main() -> signed {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i].x>>a[i].y,a[i].id=i;
if(n==1) return cout<<"0.0000\n1",0;
solve(1,n); int x=1,y=n,mid=n>>1;
for(int i=1;i<=mid;++i)
for(int j=mid+1;j<=n;++j)
if(dp[i][j]<dp[x][y]) x=i,y=j;
cout<<fixed<<setprecision(10)<<dp[x][y]<<"\n";
for(auto it: pr(1,n,x,y)) cout<<it<<' ';
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 12068kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.3833257716 3 7 4 2 8 5 1 6
result:
wrong answer you reported 26.383325772 but the real length of your path is 27.344153737