BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】
1083: [SCOI2005]繁忙的都市
Time Limit: 10 Sec??Memory Limit: 162 MBSubmit: 2925??Solved: 1927
[Submit][Status][Discuss]
Description
城市C是一個非常繁忙的大都市,城市中的道路十分的擁擠,于是市長決定對其中的道路進行改造。城市C的道
路是這樣分布的:城市中有n個交叉路口,有些交叉路口之間有道路相連,兩個交叉路口之間最多有一條道路相連
接。這些道路是雙向的,且把所有的交叉路口直接或間接的連接起來了。每條道路都有一個分值,分值越小表示這
個道路越繁忙,越需要進行改造。但是市政府的資金有限,市長希望進行改造的道路越少越好,于是他提出下面的
要求: 1. 改造的那些道路能夠把所有的交叉路口直接或間接的連通起來。 2. 在滿足要求1的情況下,改造的
道路盡量少。 3. 在滿足要求1、2的情況下,改造的那些道路中分值最大的道路分值盡量小。任務:作為市規劃
局的你,應當作出最佳的決策,選擇那些道路應當被修建。
Input
第一行有兩個整數n,m表示城市有n個交叉路口,m條道路。接下來m行是對每條道路的描述,u, v, c表示交叉
路口u和v之間有道路相連,分值為c。(1≤n≤300,1≤c≤10000)
Output
兩個整數s, max,表示你選出了幾條道路,分值最大的那條道路的分值是多少。
Sample Input
4 51 2 3
1 4 5
2 4 7
2 3 6
3 4 8
Sample Output
3 6HINT
Source
題目鏈接:http://www.lydsy.com/JudgeOnline/problem.php?id=1083
題意
給定一張圖,求其最小生成樹中權值最大的邊
要是學習過最小生成樹的相關概念,就會發現這道題就是直接考察的最小生成樹,只不過題目沒有問你最小生成樹的邊權和,而是讓你輸出最小生成樹有幾條邊(點數-1)和權值最大的那條邊的權值。
那么什么是生成樹呢?
In the mathematical field of graph theory, a spanning tree T of an undirected graph G is a subgraph that is a tree which includes all of the vertices of G. In general, a graph may have several spanning trees, but a graph that is not connected will not contain a spanning tree (but see Spanning forests below). If all of the edges of G are also edges of a spanning tree T of G, then G is a tree and is identical to T (that is, a tree has a unique spanning tree and it is itself).
Paste_Image.png
如上圖所示,生成樹就是在給定的圖中選取最少的邊使所有頂點連通,那么最小生成樹就是選取的邊的權值和最小。
了解了生成樹的概念,就很容易能明白生成樹只有n-1條邊,其中n表示頂點數。
那么怎么求最小生成樹呢?
這里我介紹kruscal算法。
克魯斯卡爾算法
該算法用到的是貪心思想,將所有的邊按權值排序,每次都選權值最小的邊,然后判斷這條邊的兩個頂點是否屬于同一個連通塊,如果不屬于同一個連通塊,那么這條邊就應屬于最小生成樹,逐漸進行下去,直到連通塊只剩下一個。
kruscal算法的模板代碼如下:
1 const int maxn=400;//最大點數 2 const int maxm=10000;//最大邊數 3 int n,m;//n表示點數,m表示邊數 4 struct edge{int u,v,w;} e[maxm];//u,v,w分別表示該邊的兩個頂點和權值 5 bool cmp(edge a,edge b) 6 { 7 return a.w<b.w; 8 } 9 int fa[maxn];//因為需要用到并查集來判斷兩個頂點是否屬于同一個連通塊 10 int find(int x) 11 { 12 if(x==fa[x]) return x; 13 else return fa[x]=find(fa[x]); 14 } 15 int kruscal() 16 { 17 int ans=-1; 18 sort(e+1,e+1+m,cmp); 19 for(int i=1;i<=n;++i) fa[i]=i;//初始化并查集 20 int cnt=n; 21 for(int i=1;i<=m;++i) 22 { 23 int t1=find(e[i].u); 24 int t2=find(e[i].v); 25 if(t1!=t2) 26 { 27 if(cnt==1) break; 28 fa[t1]=t2; 29 ans=max(ans,e[i].w); 30 cnt--; 31 } 32 } 33 return ans; 34 }針對這道題,我們只需要把ans+=e[i].w改為ans=max(ans,e[i].w)就好了,至此問題得到了解決。
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=400;///最大點數 4 const int maxm=10000;///最大邊數 5 int n,m;///n表示點數,m表示邊數 6 struct edge 7 { 8 int u,v,w;///u,v,w分別表示該邊的兩個頂點和權值 9 }e[maxm]; 10 bool cmp(edge a,edge b) 11 { 12 return a.w<b.w; 13 } 14 int fa[maxn];///判斷兩個點是否屬于同一個連通塊 15 int find(int x) 16 { 17 if(x==fa[x]) 18 return x; 19 else return fa[x]=find(fa[x]); 20 } 21 int kruscal() 22 { 23 int ans=-1; 24 sort(e+1,e+1+m,cmp); 25 for(int i=1;i<=n;i++) 26 fa[i]=i;///初始化并查集 27 int cnt=n; 28 for(int i=1;i<=m;i++) 29 { 30 int t1=find(e[i].u); 31 int t2=find(e[i].v); 32 if(t1!=t2) 33 { 34 if(cnt==1) 35 break; 36 fa[t1]=t2; 37 ///ans+=e[i].w; 38 ans=max(ans,e[i].w); 39 cnt--; 40 } 41 } 42 return ans; 43 } 44 int main() 45 { 46 cin>>n>>m; 47 for(int i=1;i<=m;i++) 48 { 49 cin>>e[i].u>>e[i].v>>e[i].w; 50 } 51 cout<<n-1<<" "; 52 cout<<kruscal()<<endl; 53 return 0; 54 }?
轉載于:https://www.cnblogs.com/ECJTUACM-873284962/p/7141078.html
總結
以上是生活随笔為你收集整理的BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 两点之间最短路径:弗洛伊德算法
- 下一篇: monkeyrunner脚本的录制和回放