一道ACM题 求代码!!做出者有奖!!!!!!!!

2024-12-18 20:09:36
推荐回答(2个)
回答1:

//枚举选择的点,将剩余的点到它的距离排序后求和即可,复杂度n^2log(n)
#include
#include
#include
using namespace std;
const int maxn=105;
struct point{
int x,y;
void input(){ scanf("%d%d",&x,&y);}
}list[105];
int n,m;
int dist(point a,point b){
return abs(a.x-b.x)+abs(a.y-b.y);
}
void solve(){
int d[maxn],ct,ans=0x7f7f7f7f;
for(int i=0;i ct=0;
int tmp=0;
for(int j=0;j if(j==i) continue;
d[ct++]=dist(list[i],list[j]);
}
sort(d,d+ct);//排序
for(int j=0;j ans=min(ans,tmp);//更新
}
printf("%d\n",ans);
}
int main(){
int ca;
scanf("%d",&ca);
while(ca--){
scanf("%d%d",&n,&m);
for(int i=0;i solve();
}
return 0;
}

回答2:

#include
#include
#include
main()
{
int x[103],y[103],i,j,k,dd[103],d[103],n,m,t,cas,min;
while(scanf("%d",&cas)!=EOF)
{
while(cas--)
{
scanf("%d%d",&n,&m);
for(i=0;i scanf("%d%d",&x[i],&y[i]);
for(i=0;i {
for(j=0;j
dd[j]=abs(x[i]-x[j])+abs(y[i]-y[j]);

for(j=0;j for(k=j+1;k if(dd[j]>dd[k])
{
t=dd[j];dd[j]=dd[k];dd[k]=t;
}
d[i]=0;
for(j=0;j d[i]+=dd[j];
}
min=d[0];
for(i=1;i if(d[i] min=d[i];
printf("%d\n",min);

}
}
}