在一個無向連通圖中,如果存在一個連通子圖包含原圖中所有的結點和部分邊,且這個子圖不存在回路,那么我們稱這個子圖為原圖的一棵生成樹。在帶權圖中,所有的生成樹中邊權的和最小的那棵(或幾棵)被稱為最小生成樹。
最小生成樹Kruskal算法的算法原理,它按照如下步驟求解最小生成樹:
(1)初始時所有結點屬於孤立的集合。
(2)按照邊權遞增順序遍歷所有的邊,若遍歷到的邊兩個頂點仍分屬不同的集合(該邊即為聯通這兩個集合的邊中權值最小的那條)則確定該邊為最小生成樹上的一條邊,並將這兩個頂點分屬的集合合並。
(3)遍歷完所有邊后,原圖上所有結點屬於同一個集合則被選取的邊和原圖中所有結點構成最小生成樹;否則原圖不連通,最小生成樹不存在。
例 5.3 還是暢通工程 (1017)
- 題目描述:某省調查鄉村交通狀況,得到的統計表中列出了任意兩村庄間的距離。“暢通工程”的目標是使全省任何兩個村庄間都可以實現公路交通(但不一定有直接的公路相連,只要能間接通過公路可達即可),並要求鋪設的公路總長度為最小。請計算最小的公路總長度。
- 輸入:測試輸入包含若干測試用例。每個測試用例的第1行給出村庄數目N ( < 100 );隨后的N(N-1)/2行對應村庄間的距離,每行給出一對正整數,分別是兩個村庄的編號,以及此兩村庄間的距離。為簡單起見,村庄從1到N編號。當N為0時,輸入結束,該用例不被處理。
- 輸出:對每個測試用例,在1行里輸出最小的公路總長度。
樣例輸入: 3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0 樣例輸出: 3 5
#include<cstdio> #include<algorithm> #define N 101 using namespace std; int Tree[N]; int findRoot(int x){ if(Tree[x]==-1) return x; else{ int tmp=findRoot(Tree[x]); Tree[x]=tmp; return tmp; } } struct E{ int a,b; int cost; bool operator < (const E &A) const{ return cost<A.cost; } }edge[6000]; int main(){ int n; while(scanf("%d",&n)!=EOF&&n!=0){ for(int i=1;i<=n*(n-1)/2;i++) scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].cost); sort(edge+1,edge+1+n*(n-1)/2); for(int i=1;i<=n;i++) Tree[i]=-1; int ans=0; for(int i=1;i<=n*(n-1)/2;i++){ int a=findRoot(edge[i].a); int b=findRoot(edge[i].b); if(a!=b){ Tree[a]=b; ans+=edge[i].cost; } } printf("%d\n",ans); } return 0; }
例 5.4 Freckles (1144)
題目描述:In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad’s back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley’s engagement falls through.
Consider Dick’s back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.
輸入:The first line contains 0 < n <= 100, the number of freckles on Dick’s back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.
輸出:Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.
樣例輸入: 3 1.0 1.0 2.0 2.0 2.0 4.0 1 2 3 4 5 樣例輸出: 3.41
#include<cstdio> #include<algorithm> #include<cmath> using namespace std; int Tree[101]; struct Edge{ int a; int b; double c; bool operator <(const Edge &A) const{ return c<A.c; } }edge[6000]; struct Point{ double x; double y; double distance(Point A){ return pow((x-A.x)*(x-A.x)+(y-A.y)*(y-A.y),0.5); } }list[101]; int getRoot(int x){ if(Tree[x]==-1) return x; else{ int tmp=getRoot(Tree[x]); Tree[x]=tmp; return tmp; } } int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++){ scanf("%lf%lf",&list[i].x,&list[i].y); } int t=1; for(int i=1;i<=n;i++){ for(int j=i+1;j<=n;j++){ edge[t].a=i; edge[t].b=j; edge[t].c=list[i].distance(list[j]); t++; } } sort(edge+1,edge+1+n*(n-1)/2); for(int i=1;i<=n;i++) Tree[i]=-1; double tot=0.0; for(int i=1;i<=n*(n-1)/2;i++){ int a=getRoot(edge[i].a); int b=getRoot(edge[i].b); if(a!=b){ Tree[a]=b; tot+=edge[i].c; } } printf("%.2f\n",tot); } return 0; }