【HDU1162】Eddy's picture(MST基礎題)

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   for   re   c   

很基礎的點座標MST,一不留神就AC了, - - !!

 

 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cstdio> 5 #include <cmath> 6 #include <cctype> 7 #include <algorithm> 8 #include <numeric> 9 #include <limits.h>10 11 #define typec double12 using namespace std;13 14 const int inf = 0xffff;15 const int V = 105;16 int vis[V];17 typec lowc[V], Map[V][V], point[V][2];18 19 typec prim (typec cost[][V], int n) {20     int i, j, p;21     typec minc, res = 0;22     memset(vis, 0, sizeof(vis));23     vis[0] = 1;24     for (i = 1; i < n; ++ i) lowc[i] = cost[0][i];25     for (i = 1; i < n; ++ i) {26         minc = inf;27         p = -1;28         for (j = 0 ; j < n; ++ j) {29             if (0 == vis[j] && minc > lowc[j]) {30                 minc = lowc[j];31                 p = j;32             }33         }34         if (inf == minc) return -1;35         res += minc;36         vis[p] = 1;37         for (j = 0 ; j < n; ++ j) {38             if (0 == vis[j] && lowc[j] > cost[p][j]) {39                 lowc[j] = cost[p][j];40             }41         }42     }43     return res;44 }45 46 double cal_dis (double x1, double y1, double x2, double y2) {47     return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));48 }49 50 int main () {51     int n;52     while (~scanf("%d", &n)) {53 54         for (int i = 0 ; i < n; ++ i) {55             scanf("%lf %lf", &point[i][0], &point[i][1]);56         }57 58         for (int i = 0 ; i < V; ++ i) {59             for (int j = 0; j < V; ++ j) {60                 if (i == j) Map[i][j] = 0;61                 else Map[i][j] = inf;62             }63         }64 65         for (int i = 0; i < n; ++ i) {66             for (int j = i + 1; j < n; ++ j) {67                 Map[i][j] = Map[j][i] =68                     cal_dis(point[i][0], point[i][1], point[j][0], point[j][1]);69             }70         }71 72         //cout << prim(Map, n) << endl;73         printf("%.2lf\n", prim(Map, n));74     }75     return 0;76 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.