Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

超时一百年,求大神帮解决

Posted by LXY5201314 at 2017-12-16 21:58:06 on Problem 2349
In Reply To:最小生成树水过~~~~~~~~~~~~~~~~ Posted by:nvliba at 2015-08-12 09:16:21
#include<iostream>
#include<algorithm>
#include<math.h>
#include<stdio.h>
using namespace std;
double c[510][510],d[510];
int a[510][2],b[510],k1,min1=1000000,p,s;
void hh(int i,int j)
{
    if(c[i][j]!=-1&&b[j]!=0&&min1>c[i][j])
    {
        min1=c[i][j];
        k1=j;
    }
    else if(c[j][i]!=-1&&b[j]!=0&&min1>c[j][i])
    {
        min1=c[j][i];
        k1=j;
    }
    if(j<p)return;
    hh(i,j+1);
}
void rrrr()
{
    int i,j,x,y;
    for(i=1;i<p;i++)
        for(j=i+1;j<=p;j++)
    {
        if(a[i][0]==a[j][0])c[i][j]=(a[i][1]>a[j][1])?a[i][1]-a[j][1]:a[j][1]-a[i][1];
        else {if(a[i][0]>a[j][0])x=a[i][0]-a[j][0];
            else x=a[j][0]-a[i][0];
             y=(a[i][1]>a[j][1])?a[i][1]-a[j][1]:a[j][1]-a[i][1];
            c[i][j]=(double)sqrt((double)(x*x+y*y));
            }
    }
}
void add()
{
    int i,j,k,sum;
    for(i=1;i<=p;i++)
        for(j=1;j<=p;j++)
        c[i][j]=-1;
    rrrr();
    b[1]=0;k=0;
    while(1)
    {min1=1000000;
        for(j=1;j<=p;j++)
            if(b[j]==0)hh(j,1);
        if(min1!=1000000)
        {
            d[k++]=min1;
            b[k1]=0;
        }
        sum=0;
        for(i=1;i<=p;i++)
            sum+=b[i];
        if(sum==0)break;
    }
    sort(d,d+k);
    k=k-s/2;
    printf("%.2f\n",d[k-1]);
}
int main()
{int n,i,j;
    cin>>n;
    while(n--)
    {
        cin>>s>>p;
        for(i=1;i<=p;i++)
        {
            cin>>a[i][0]>>a[i][1];
            b[i]=1;
        }
        add();
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator