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

新手用Prim飘过。。。

Posted by h8214111 at 2014-02-16 13:00:26 on Problem 2349
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF 0x7fffffff
#define maxn 550

struct node{
    int x;
    int y;
}point[maxn];
double map[maxn][maxn];
double dis[maxn];
bool used[maxn];
double bian[maxn];
int s, p, h;

double len(int x1, int x2, int y1, int y2)
{
    return sqrt((x1-x2) * (x1-x2) + (y1-y2) * (y1-y2));
}

void Prim()
{
    int i, j, k;
    h=0;
    memset(used, 0, sizeof(used));
    used[1] = 1;
    for(i=1; i<=p; i++)
    {
        dis[i] = map[1][i];
    }
    for(i=1; i<=p; i++)
    {

        double min = INF;
        for(j=1; j<=p; j++)
        {
            if(!used[j] && dis[j]<min)
            {
                k = j;
                min = dis[j];
            }
        }
        used[k] = 1;
        bian[h++]=min;
        for(j=1; j<=p; j++)
        {
            if(!used[j] && dis[j]>map[k][j])
            {
                dis[j] = map[k][j];
            }
        }
    }
}

int main()
{
    int t, i, j;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&s, &p);
        for(i=1; i<=p; i++)
        {
            scanf("%d%d", &point[i].x, &point[i].y);
        }
        for(i=1; i<=p; i++)
        {
            for(j=1; j<=p; j++)
            {
                if(i == j) map[i][j] = 0;
                else map[i][j] = INF;
            }
        }
        for(i=1; i<=p; i++)
        {
            for(j=1; j<=p; j++)
            {
                map[i][j] = len(point[i].x, point[j].x, point[i].y, point[j].y);
            }
        }
        Prim();
        sort(bian, bian+h);
        printf("%0.2f\n",bian[h-2-s+1]);
 //       for(i=0; i<h-1; i++)
 //           printf("  %.2lf", bian[i]);
 //       printf("\n");
    }
return 0;
}
/*不知道为什么用c++交显示语法错误,然后用G++交%.2lf就WA。。。。
说好的一遍过呢。。。5555555  T_T*/

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