日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU 1875 畅通工程再续 (最小生成树)

發布時間:2025/1/21 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU 1875 畅通工程再续 (最小生成树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

暢通工程再續

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17913????Accepted Submission(s): 5593


Problem Description 相信大家都聽說一個“百島湖”的地方吧,百島湖的居民生活在不同的小島中,當他們想去其他的小島時都要通過劃小船來實現?,F在政府決定大力發展百島湖,發展首先要解決的問題當然是交通問題,政府決定實現百島湖的全暢通!經過考察小組RPRush對百島湖的情況充分了解后,決定在符合條件的小島間建上橋,所謂符合條件,就是2個小島之間的距離不能小于10米,也不能大于1000米。當然,為了節省資金,只要求實現任意2個小島之間有路通即可。其中橋的價格為 100元/米。

?

Input 輸入包括多組數據。輸入首先包括一個整數T(T <= 200),代表有T組數據。
每組數據首先是一個整數C(C <= 100),代表小島的個數,接下來是C組坐標,代表每個小島的坐標,這些坐標都是 0 <= x, y <= 1000的整數。

?

Output 每組輸入數據輸出一行,代表建橋的最小花費,結果保留一位小數。如果無法實現工程以達到全部暢通,輸出”oh!”.

?

Sample Input 2 2 10 10 20 20 3 1 1 2 2 1000 1000

?

Sample Output 1414.2 oh!

?

?

?

1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #include <queue> 5 #include <vector> 6 #include <map> 7 #include <algorithm> 8 #include <cstring> 9 #include <cctype> 10 #include <cstdlib> 11 #include <cmath> 12 #include <ctime> 13 using namespace std; 14 15 const int SIZE = 105; 16 int FATHER[SIZE],N,M,NUM; 17 int MAP[SIZE][SIZE]; 18 struct Node 19 { 20 int from,to; 21 double cost; 22 }G[SIZE * SIZE]; 23 struct 24 { 25 int x,y; 26 }TEMP[SIZE]; 27 28 void ini(void); 29 int find_father(int); 30 void unite(int,int); 31 bool same(int,int); 32 void kruskal(void); 33 bool comp(const Node &,const Node &); 34 double dis(int,int,int,int); 35 int main(void) 36 { 37 int t; 38 double temp; 39 40 scanf("%d",&t); 41 while(t --) 42 { 43 scanf("%d",&N); 44 ini(); 45 for(int i = 1;i <= N;i ++) 46 scanf("%d%d",&TEMP[i].x,&TEMP[i].y); 47 for(int i = 1;i <= N;i ++) 48 for(int j = i + 1;j <= N;j ++) 49 { 50 temp = sqrt(dis(TEMP[i].x,TEMP[i].y,TEMP[j].x,TEMP[j].y)); 51 if(temp >= 10 && temp <= 1000) 52 { 53 G[NUM].from = i; 54 G[NUM].to = j; 55 G[NUM].cost = temp * 100; 56 NUM ++; 57 } 58 } 59 sort(G,G + NUM,comp); 60 kruskal(); 61 } 62 63 return 0; 64 } 65 66 void ini(void) 67 { 68 NUM = 0; 69 for(int i = 1;i <= N;i ++) 70 FATHER[i] = i; 71 } 72 73 int find_father(int n) 74 { 75 if(FATHER[n] == n) 76 return n; 77 return FATHER[n] = find_father(FATHER[n]); 78 } 79 80 void unite(int x,int y) 81 { 82 x = find_father(x); 83 y = find_father(y); 84 85 if(x == y) 86 return ; 87 FATHER[x] = y; 88 } 89 90 bool same(int x,int y) 91 { 92 return find_father(x) == find_father(y); 93 } 94 95 bool comp(const Node & a,const Node & b) 96 { 97 return a.cost < b.cost; 98 } 99 100 void kruskal(void) 101 { 102 int count = 0; 103 double ans = 0; 104 105 for(int i = 0;i < NUM;i ++) 106 if(!same(G[i].from,G[i].to)) 107 { 108 unite(G[i].from,G[i].to); 109 count ++; 110 ans += G[i].cost; 111 if(count == N - 1) 112 break; 113 } 114 if(count == N - 1) 115 printf("%.1f\n",ans); 116 else 117 puts("oh!"); 118 } 119 120 double dis(int x_1,int y_1,int x_2,int y_2) 121 { 122 return pow(x_1 - x_2,2) + pow(y_1 - y_2,2); 123 }

?

轉載于:https://www.cnblogs.com/xz816111/p/4549533.html

總結

以上是生活随笔為你收集整理的HDU 1875 畅通工程再续 (最小生成树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。