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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

MST:Bad Cowtractors(POJ 2377)

發布時間:2023/12/1 编程问答 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MST:Bad Cowtractors(POJ 2377) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

              

                 壞的牛圈建筑

  題目大意:就是現在農夫又要牛修建牛欄了,但是農夫想不給錢,于是牛就想設計一個最大的花費的牛圈給他,牛圈的修理費用主要是用在連接牛圈上

  這一題很簡單了,就是找最大生成樹,把Kruskal算法改一下符號就好了,把邊從大到小排列,然后最后再判斷是否聯通(只要找到他們的根節點是否相同就可以了!)

  

1 #include <iostream> 2 #include <algorithm> 3 #include <functional> 4 #define MAX_N 1005 5 #define MAX 20005 6 7 using namespace std; 8 9 typedef int Position; 10 typedef struct _edge 11 { 12 Position from; 13 Position to; 14 int cost; 15 }Edge_Set; 16 int fcomp(const void *a, const void *b) 17 { 18 return (*(Edge_Set *)b).cost - (*(Edge_Set *)a).cost; 19 } 20 21 static Edge_Set edge[MAX]; 22 static Position Set[MAX_N]; 23 24 Position Find(Position); 25 void Union(Position, Position); 26 void Kruskal(const int, const int); 27 bool Is_Connected(const int); 28 29 int main(void) 30 { 31 int Node_Sum, Path_Sum, cost; 32 Position tmp_from, tmp_to; 33 34 while (~scanf("%d%d", &Node_Sum, &Path_Sum)) 35 { 36 for (int i = 0; i < Path_Sum; i++)//讀入邊 37 { 38 scanf("%d%d%d", &tmp_from, &tmp_to, &cost); 39 edge[i].from = tmp_from; edge[i].to = tmp_to; edge[i].cost = cost; 40 } 41 qsort(edge, Path_Sum, sizeof(Edge_Set), fcomp); 42 Kruskal(Node_Sum, Path_Sum); 43 } 44 return 0; 45 } 46 47 Position Find(Position x) 48 { 49 if (Set[x] < 0) 50 return x; 51 else return Set[x] = Find(Set[x]); 52 } 53 54 void Union(Position px, Position py) 55 { 56 if (Set[px] < Set[py]) 57 { 58 Set[px] += Set[py]; 59 Set[py] = px; 60 } 61 else 62 { 63 Set[py] += Set[px]; 64 Set[px] = py; 65 } 66 } 67 68 bool Is_Connected(const int Node_Sum) 69 { 70 Position p_u, p_tmp; 71 p_u = Find(1); 72 for (int i = 2; i <= Node_Sum; i++) 73 { 74 p_tmp = Find(i); 75 if (p_u != p_tmp) 76 return false; 77 } 78 return true; 79 } 80 81 void Kruskal(const int Node_Sum, const int Path_Sum) 82 { 83 Position px, py, from, to; 84 long long ans = 0; 85 86 fill(Set, Set + Node_Sum + 1, -1); 87 for (int i = 0; i < Path_Sum; i++) 88 { 89 from = edge[i].from; to = edge[i].to; 90 px = Find(from); py = Find(to); 91 92 if (px != py) 93 { 94 ans += edge[i].cost; 95 Union(px, py); 96 } 97 } 98 if (Is_Connected(Node_Sum)) 99 printf("%lld\n", ans); 100 else 101 printf("-1\n"); 102 }

?

轉載于:https://www.cnblogs.com/Philip-Tell-Truth/p/4955944.html

總結

以上是生活随笔為你收集整理的MST:Bad Cowtractors(POJ 2377)的全部內容,希望文章能夠幫你解決所遇到的問題。

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