QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#187491 | #3858. Systematic salesman | wind_whisper# | WA | 2ms | 14008kb | C++14 | 4.6kb | 2023-09-24 17:47:50 | 2023-09-24 17:47:51 |
Judging History
answer
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
using namespace std;
#define MN 600010
#define MM 3000010
int X[1010],Y[1010];double di[1010][1010];
double dis(int a,int b)
{
return sqrt(1ll*a*a+1ll*b*b);
}
struct lin
{
int x,y,i;
void sw(){int t=x;x=y;y=t;}
}sz[1010];
bool operator<(const lin&a,const lin&b)
{
return a.x<b.x;
}
int cal(int n)
{
if(n==1)return 1;
int a=n/2,b=n-a;
return 2*(cal(a)+cal(b))+a*b+a+b;
}
int fr[MN],ne[MM],v[MM],bs=0,N;double w[MM];
void addb(int a,int b,double c)
{
//printf("(%d %d %.2lf)\n",a,b,c);
v[bs]=b;
w[bs]=c;
ne[bs]=fr[a];
fr[a]=bs++;
}
int wz[MN];vector<pair<int,int> > v3,v4;
void dfs(int l,int r,vector<pair<int,int> >&v1,vector<pair<int,int> >&v2,vector<int>&nd)
{
sort(sz+l,sz+r+1);
if(l==r)
{
int i=sz[l].i;fr[++N]=-1;
v1.push_back(make_pair(i,N));
v2.push_back(make_pair(i,N));
nd.push_back(N);return;
}
if(l+1==r)
{
int i=sz[l].i,j=sz[l+1].i;
fr[++N]=-1;fr[++N]=-1;
v1.push_back(make_pair(i,N-1));
v2.push_back(make_pair(j,N));
nd.push_back(N);
nd.push_back(N-1);
addb(N-1,N,di[i][j]);
return;
}
int m=(l+r+1)>>1;
for(int i=l;i<=r;i++)sz[i].sw();
vector<pair<int,int> > a,b,c,d;
vector<int> nl,nr;
dfs(l,m-1,a,b,nl);dfs(m,r,c,d,nr);
int s=nl.size();
for(int i=1;i<=s;i++)fr[N+i]=-1,wz[nl[i-1]]=i;
for(int i=1;i<=s;i++)
{
int u=nl[i-1];
for(int j=fr[u];j!=-1;j=ne[j])
addb(N+wz[v[j]],N+i,w[j]);
}
for(auto t:a)v1.push_back(t);
for(auto t:b)v1.push_back(make_pair(t.first,N+wz[t.second]));
v3.clear();
for(auto t:b)v3.push_back(t);
for(auto t:a)v3.push_back(make_pair(t.first,N+wz[t.second]));
for(int t:nl)nd.push_back(t);
for(int i=1;i<=s;i++)nd.push_back(N+i);
N+=s;s=nr.size();
for(int i=1;i<=s;i++)fr[N+i]=-1,wz[nr[i-1]]=i;
for(int i=1;i<=s;i++)
{
int u=nr[i-1];
for(int j=fr[u];j!=-1;j=ne[j])
addb(N+wz[v[j]],N+i,w[j]);
}
for(auto t:d)v2.push_back(t);
for(auto t:c)v2.push_back(make_pair(t.first,N+wz[t.second]));
v4.clear();
for(auto t:c)v4.push_back(t);
for(auto t:d)v4.push_back(make_pair(t.first,N+wz[t.second]));
for(int t:nr)nd.push_back(t);
for(int i=1;i<=s;i++)nd.push_back(N+i);
N+=s;
int s1=v1.size(),s2=v2.size();
/*for(int i=1;i<=s1+s2;i++)fr[N+i]=-1;
for(int i=1;i<=s1;i++)
addb(v3[i-1].second,N+i,0);
for(int i=1;i<=s2;i++)
addb(N+s1+i,v4[i-1].second,0);*/
for(int i=1;i<=s1;i++)
{
for(int j=1;j<=s2;j++)
{
auto tl=v3[i-1],tr=v4[j-1];
double c=di[tl.first][tr.first];
addb(tl.second,tr.second,c);
}
}
//for(int i=1;i<=s1+s2;i++)nd.push_back(N+i);
//N+=s1+s2;
}
bool bk[MN];double jl[MN];int S,T;
double dij()
{
for(int i=1;i<=N;i++)bk[i]=0,jl[i]=1e100;
typedef pair<double,int> Pr;
priority_queue<Pr,vector<Pr>,greater<Pr> >pq;
pq.push(make_pair(0,S));jl[S]=0;
while(pq.size())
{
auto t=pq.top();pq.pop();
int u=t.second;
if(u==T)return jl[u];
if(bk[u])continue;
bk[u]=1;
for(int i=fr[u];i!=-1;i=ne[i])
{
double s=jl[u]+w[i];
if(s<jl[v[i]])
{
jl[v[i]]=s;
pq.push(make_pair(s,v[i]));
}
}
}
return -1;
}
double dfs(int u)
{
//printf("u=%d\n",u);
if(u==T)return 0;
if(bk[u])return jl[u];
double mi=1e100;
for(int i=fr[u];i!=-1;i=ne[i])
{
double t=w[i]+dfs(v[i]);
if(t<mi)mi=t;
}
bk[u]=1;
return jl[u]=mi;
}
int main()
{
//printf("%d",cal(2));
int n;
scanf("%d",&n);
//for(int i=1;i<=100;i++)fr[i]=-1;
for(int i=0;i<n;i++)
{
//X[i]=i;Y[i]=i;
scanf("%d%d",&X[i],&Y[i]);
sz[i].x=X[i];sz[i].y=Y[i];sz[i].i=i;
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
di[i][j]=dis(X[i]-X[j],Y[i]-Y[j]);
vector<pair<int,int> >v1,v2;vector<int> nd;
dfs(0,n-1,v1,v2,nd);
S=N+1;T=N+2;fr[S]=fr[T]=-1;N+=2;
for(auto t:v1)addb(S,t.second,0);
for(auto t:v2)addb(t.second,T,0);
//printf("N=%d bs=%d\n",N,bs);
//double ans=dij();
double ans=dfs(S);
printf("%.10lf",ans);
//printf("[%d]",n);
//for(int i=1;i<=N;i++)printf("[%d]\n",fr[i]);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 2ms
memory: 14008kb
input:
8 7 3 2 0 4 5 1 4 8 2 9 9 0 8 6 1
output:
26.3833257716
result:
wrong output format Unexpected end of file - int32 expected