hdoj1875

//prim算法
#include<iostream>
#include
<stdio.h>
#include
<string.h>
#include
<math.h>
using namespace std;
int T, C, heap_size;
double map[100][100];
double V[100][100];
pair 
< doubleint >heap[100];    //first保存距离,second保存岛的编号
void up(int i)
{
    
while (i > 1 && heap[i].first < heap[i / 2].first) {
    swap(heap[i], heap[i 
/ 2]);
    i 
/= 2;
    }
}

void down(int i)
{
    
int j = i * 2;
    
while (j <= heap_size) {
    
if (j < heap_size && heap[j].first > heap[j + 1].first)
        j
++;
    
if (heap[i].first <= heap[j].first)
        
break;
    swap(heap[i], heap[j]);
    i 
= j;
    j 
*= 2;
    }
}

int main()
{
    scanf(
"%d"&T);
    
while (T--) {
    scanf(
"%d"&C);
    
double sum = 0;
    heap_size 
= C - 1;
    
for (int i = 0; i < C; ++i) {
        scanf(
"%lf%lf"&V[i][0], &V[i][1]);
        
for (int j = 0; j < i; ++j) {
        
double distmp =
            sqrt((V[i][
0- V[j][0]) * (V[i][0- V[j][0]) +
             (V[i][
1- V[j][1]) * (V[i][1- V[j][1]));
        
if (distmp >= 10 && distmp <= 1000)
            map[i][j] 
= map[j][i] = distmp;
        
else
            map[i][j] 
= map[j][i] = 1000000;
        }
    }
//init heap
    for (int i = 1; i < C; ++i) {
        heap[i].first 
= map[0][i];
        heap[i].second 
= i;
    }
    
for (int i = C / 2; i; --i)
        down(i);
    
for (int i = 1; i < C; ++i) {
        
int u = heap[1].second;
        sum 
+= heap[1].first;
        heap[
1= heap[heap_size];
        heap_size
--;
        down(
1);
        
for (int j = 1; j <= heap_size; ++j) {
        
if (heap[j].first > map[u][heap[j].second]) {
            heap[j].first 
= map[u][heap[j].second];
            up(j);
        }
        }
    }
    
if (sum > (C - 1* 1000)
        printf(
"oh!\n");
    
else
        printf(
"%.1lf\n", sum * 100);
    }
}


posted @ 2010-05-04 19:39  open source  阅读(182)  评论(0编辑  收藏  举报