zoukankan      html  css  js  c++  java
  • uva 10369(最小生成树)

    和上一题基本一致。求解除了用了无限发射的城市最小生成树中最长的那一条边。

    代码如下:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <cmath>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <queue>
     9 #include <stack>
    10 #define INF 0x7fffffff
    11 #define LEN 1010
    12 using namespace std;
    13 
    14 typedef struct{double x, y;}POINT;
    15 typedef struct {int a, b;double v;}ARC;
    16 
    17 int T, n, q, top, parent[LEN];
    18 POINT p[LEN];
    19 ARC Arc[LEN*LEN];
    20 
    21 inline double dis(POINT a, POINT b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
    22 bool cmp(ARC a, ARC b){return a.v<b.v;}
    23 //UFSet
    24 void init() { for(int i=0; i<LEN; i++) parent[i] = i;}
    25 int Find(int x){return parent[x]==x?x:parent[x]=Find(parent[x]);}
    26 void Union(int a, int b){int pa = Find(a), pb = Find(b);if(pa!=pb)parent[pa] = pb;}
    27 
    28 int main()
    29 {
    30 //    freopen("in.txt", "r", stdin);
    31 
    32     scanf("%d", &T);
    33     while(T--){
    34         scanf("%d%d", &q, &n);
    35         for(int i=1; i<=n; i++) scanf("%lf%lf", &p[i].x, &p[i].y);
    36         top = 0;
    37         for(int i=1; i<=n; i++)for(int j=i+1; j<=n; j++)
    38         {Arc[top].a = i;Arc[top].b = j;Arc[top].v = dis(p[i],p[j]);top++;}
    39         sort(Arc, Arc+top, cmp);init();
    40         int cnt = 0;double ans = 0;
    41         for(int i=0; i<top; i++){
    42             if(cnt==n-q)break;
    43             if(Find(Arc[i].a)!=Find(Arc[i].b))
    44             {Union(Arc[i].a, Arc[i].b);ans= Arc[i].v;cnt++;}
    45         }
    46         printf("%.2lf
    ", ans);
    47     }
    48     return 0;
    49 }
    View Code
    奔跑吧!少年!趁着你还年轻
  • 相关阅读:
    @ResponseBody和@RequestBoby的作用
    项目Alpha冲刺Day9
    项目Alpha冲刺Day8
    项目Alpha冲刺Day7
    设计模式第三次作业
    项目Alpha冲刺Day6
    树莓派历程笔记
    项目Alpha冲刺Day5
    django获取ip与数据重复性判定
    在django模板中添加jquery
  • 原文地址:https://www.cnblogs.com/shu-xiaohao/p/3442078.html
Copyright © 2011-2022 走看看