?? 040320187.cpp
字號(hào):
#include<iostream.h>
#include <fstream>
#include<math.h>
#include<queue>
# include <time.h>
using namespace std ;
//double countTime(const char flag = 'e');
class CircleNode{
friend bool operator>(const CircleNode a,CircleNode b){
return a.cleng>b.cleng;
}
public:
void Center();//求圓心坐標(biāo)
void Compute();//求圓排列的長度
void Swap(int k);
double *r;//記錄當(dāng)前圓排列中所有圓的半徑
double *x;//記錄當(dāng)前圓排列中所有圓的圓心坐標(biāo)
int end;//記錄當(dāng)前圓排列中最后一個(gè)圓的位置
double cleng;//記錄當(dāng)前圓排列的長度
};
void CircleNode::Center()
{ double temp=0;
for(int i=1;i<end;i++){
double valuex=x[i]+2.0*sqrt(r[end]*r[i]);
if(valuex>temp) temp=valuex;
}
x[end]=temp;
}
void CircleNode::Compute()
{
double low=0,high=0;
for(int i=1;i<=end;i++){
if(x[i]-r[i]<low) low=x[i]-r[i];
if(x[i]+r[i]>high) high=x[i]+r[i];
}
cleng=(high-low);
}
void CircleNode::Swap(int k){
double temp=r[end];
r[end]=r[k];
r[k]=temp;
}
bool confine(CircleNode *N)//剪枝條件
{
if (N->end<=2) return false;
if ((N->r[N->end-2]<N->r[N->end-1]) && (N->r[N->end-1]<N->r[N->end])){
return true;
};
if ((N->r[N->end-2]>N->r[N->end-1]) && (N->r[N->end-1]>N->r[N->end])){
return true;
};
return false;
};
int main()
{
//countTime('s');
ifstream infile("input.txt");
if(!infile)
{cerr<<"open file input error!"<<endl;
return -1;
}
ofstream outfile("output.txt");
if(!outfile)
{cerr<<"open file output error!"<<endl;
return -1;
}
int n;
infile>>n;
double *r=new double[n+1];
for(int i=1;i<=n;i++)
infile>>r[i];
//求最佳排列的過程
priority_queue<CircleNode*,vector<CircleNode*>,greater<CircleNode*> > H;
CircleNode *E,*N;
double MINLENG=2147483647;
for(i=1;i<n;i++){//第一層結(jié)點(diǎn)入隊(duì)
E=new CircleNode;
E->x=new double[n+1];
E->r=new double[n+1];
E->x[1]=0;
E->end=1;
for(int j=1;j<=n;j++)
E->r[j]=r[j];
E->Swap(i);
E->cleng=2*E->r[1];
H.push(E);
}
E=H.top();
H.pop();
// int member=0;
while(true){
for(int i=E->end+1;i<=n;i++){//中間結(jié)點(diǎn)的兒子們?nèi)腙?duì)
N=new CircleNode;
N->r=new double[n+1];
N->x=new double[n+1];
for(int j=1;j<=n;j++){
N->x[j]=E->x[j];
N->r[j]=E->r[j];
}
N->end=E->end+1;
N->Swap(i);
N->Center();
N->Compute();
if(confine(N)) continue;
H.push(N);
}
E=H.top();
H.pop();
while(E->end==n){//到了葉結(jié)點(diǎn),比較求出最佳排列
/* ++member;
cout<<"第"<<member<<"個(gè)圓排列為: "<<endl;
for(int k=1;k<=n;k++)
cout<<E->r[k]<<" ";
cout<<endl;
cout<<"該圓排列的長度為:"<<E->cleng<<endl;*/
if(E->cleng<MINLENG)
MINLENG=E->cleng;
if(H.empty()){
outfile<<MINLENG;
// outfile<<"總用時(shí)為:"<<countTime('e') << endl;
return 0;
}
E=H.top();
H.pop();
}
}
}
/*double countTime(const char flag){
static clock_t startTime, endTime;
double timeUsed = -1;
if (flag == 's'){
startTime = clock();
}
else{
endTime = clock();
timeUsed = endTime - startTime;
}
return timeUsed;
} */
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -