Số bài đã giải: 332
Hạng điểm: #269
Tổng điểm:
1600,69
Đóng góp:
-3
6
ngày liên tiếp
Kỷ lục
6 ngày
Từ Trường THCS Yên Lạc, Tổ chức do vua tạo ra
Thông tin
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const double PI=3.141592653589793,R=100.0,EPS=1e-3;
struct Pt{double x,y;};
struct Edge{int u,v;};
int K;
vector<int>alpha_deg;
double A;
vector<double>px,py;
double distPt(double x1,double y1,double x2,double y2){
return hypot(x1-x2,y1-y2);
}
void clampDisk(double&x,double&y){
double d=hypot(x,y);
if(d>R){
x=x*R/d;
y=y*R/d;
}
}
double evalTree(int n,vector<Edge>&ed,vector<double>&x,vector<double>&y){
double s=0;
for(auto&e:ed)s+=distPt(x[e.u],y[e.u],x[e.v],y[e.v]);
return s;
}
vector<Edge>buildMST(int n,vector<double>&x,vector<double>&y){
vector<int>vis(n+1,0),par(n+1,-1);
vector<double>mn(n+1,1e100);
vector<Edge>res;
mn[1]=0;
for(int step=1;step<=n;step++){
int u=-1;
for(int i=1;i<=n;i++){
if(!vis[i]&&(u==-1||mn[i]<mn[u]))u=i;
}
vis[u]=1;
if(par[u]!=-1)res.push_back({par[u],u});
for(int v=1;v<=n;v++){
if(!vis[v]){
double d=distPt(x[u],y[u],x[v],y[v]);
if(d<mn[v]){
mn[v]=d;
par[v]=u;
}
}
}
}
return res;
}
void optimizeSteiner(int id,int n,vector<Edge>&ed,vector<double>&x,vector<double>&y){
vector<int>nb;
for(auto&e:ed){
if(e.u==id)nb.push_back(e.v);
else if(e.v==id)nb.push_back(e.u);
}
if(nb.empty())return;
double dx[8]={1,-1,0,0,1,1,-1,-1};
double dy[8]={0,0,1,-1,1,-1,1,-1};
double step=50.0;
while(step>EPS){
double best=0;
double bx=x[id],by=y[id];
for(int j=0;j<8;j++){
double tx=x[id]+dx[j]*step;
double ty=y[id]+dy[j]*step;
clampDisk(tx,ty);
double cost=0;
for(int v:nb)cost+=distPt(tx,ty,x[v],y[v]);
double old=0;
for(int v:nb)old+=distPt(x[id],y[id],x[v],y[v]);
if(cost<old-1e-10&&(best==0||cost<best)){
best=cost;
bx=tx;
by=ty;
}
}
if(best!=0){
x[id]=bx;
y[id]=by;
}else step/=2.0;
}
}
void printAns(int m,vector<double>&x,vector<double>&y,vector<Edge>&ed){
cout<<m<<"\n";
cout<<fixed<<setprecision(4);
for(int i=1;i<=m;i++)
cout<<x[K+i]<<" "<<y[K+i]<<"\n";
for(auto&e:ed)
cout<<e.u<<" "<<e.v<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
freopen("camp.inp","r",stdin);
freopen("camp.out","w",stdout);
cin>>K;
alpha_deg.resize(K+1);
px.resize(K+1);
py.resize(K+1);
for(int i=1;i<=K;i++){
cin>>alpha_deg[i];
double rad=alpha_deg[i]*PI/180.0;
px[i]=R*cos(rad);
py[i]=R*sin(rad);
}
cin>>A;
vector<double>bestX=px,bestY=py;
vector<Edge>bestEd=buildMST(K,bestX,bestY);
double bestCost=evalTree(K,bestEd,bestX,bestY);
int bestM=0;
// Neu MST da du day thi khong can toi uu them
if(bestCost<=A){
printAns(bestM,bestX,bestY,bestEd);
return 0;
}
// Thu nhieu so luong diem Steiner
for(int m=1;m<=K;m++){
for(int mode=0;mode<3;mode++){
if(bestCost<=A)break;
int n=K+m;
vector<double>x(n+1),y(n+1);
for(int i=1;i<=K;i++){
x[i]=px[i];
y[i]=py[i];
}
// Khoi tao cac diem Steiner
for(int j=1;j<=m;j++){
if(mode==0){
int a=(j-1)*K/m+1;
int b=j*K/m;
double sx=0,sy=0;
for(int t=a;t<=b;t++){
sx+=px[t];
sy+=py[t];
}
x[K+j]=sx/(b-a+1)*0.5;
y[K+j]=sy/(b-a+1)*0.5;
}else if(mode==1){
double ang=2.0*PI*(j-1)/m;
x[K+j]=35.0*cos(ang);
y[K+j]=35.0*sin(ang);
}else{
x[K+j]=0;
y[K+j]=0;
}
clampDisk(x[K+j],y[K+j]);
}
vector<Edge>ed=buildMST(n,x,y);
// Toi uu toa do va cap nhat lai MST
for(int pass=1;pass<=6;pass++){
for(int s=K+1;s<=n;s++)
optimizeSteiner(s,n,ed,x,y);
ed=buildMST(n,x,y);
}
double cur=evalTree(n,ed,x,y);
if(cur<bestCost){
bestCost=cur;
bestM=m;
bestEd=ed;
bestX=x;
bestY=y;
}
}
if(bestCost<=A)break;
}
printAns(bestM,bestX,bestY,bestEd);
return 0;
}