POJ 2349 Arctic Network 最小產生樹題解

來源:互聯網
上載者:User

標籤:style   blog   使用   2014   os   cti   

本題也是使用Prime和Kruskal都可以的最小產生樹的題解。

本題一點新意就是:需要除去最大的S-1個距離,因為可以使用衛星覆蓋這些距離。

技巧:建圖建有向圖,速度快點,不用計算兩邊。

這裡使用Prime,因為是稠密圖。


#include <stdio.h>#include <math.h>#include <stdlib.h>#include <string.h>#include <limits.h>#include <algorithm>using std::sort;const int MAX_VEC = 501;struct Pos{int x, y;};float gra[MAX_VEC][MAX_VEC];Pos pos[MAX_VEC];float dist[MAX_VEC];bool vis[MAX_VEC];int N, S, P;float Prime(){memset(vis, 0, sizeof(bool) * P);vis[0] = true;for (int i = 0; i < P-1; i++){float m = (float)INT_MAX;int id = 0;for (int j = 0; j < P; j++){if (!vis[j] && gra[0][j] < m){m = gra[0][j];id = j;}}vis[id] = true;dist[i] = m;for (int j = 0; j < id; j++){if (!vis[j] && gra[0][j] > gra[j][id]) gra[0][j] = gra[j][id];;}for (int j = id+1; j < P; j++){if (!vis[j] && gra[0][j] > gra[id][j]) gra[0][j] = gra[id][j];}}sort(dist, dist+P-1);return dist[P-1-S];}int main(){scanf("%d", &N);while (N--){scanf("%d %d", &S, &P);for (int i = 0; i < P; i++){scanf("%d %d", &pos[i].x, &pos[i].y);}for (int i = 0; i < P; i++){for (int j = i+1; j < P; j++){float a = float(pos[i].x - pos[j].x);float b = float(pos[i].y - pos[j].y);gra[i][j] = sqrtf(a*a + b*b);}}printf("%.2f\n", Prime());}return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.