BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】

1083: [SCOI2005]繁忙的都市

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 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 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

Sample Output

3 6

HINT

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 }
时间: 2024-08-26 17:29:28

BZOJ 1083: [SCOI2005]繁忙的都市【Kruscal最小生成树裸题】的相关文章

BZOJ 1083: [SCOI2005]繁忙的都市(MST)

裸的最小生成树..直接跑就行了 ---------------------------------------------------------------------- #include<cstdio> #include<algorithm> #include<vector> #define rep(i,n) for(int i=0;i<n;i++) #define addEdge(u,v,w) MST.edges.push_back((KRUSKAL::Ed

bzoj 1083: [SCOI2005]繁忙的都市 (最小生成树)

链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1083 思路:连接所有点,肯定最少是需要n-1条边的,也就是写个最小生成树,记得保存下最大的权值就好了 实现代码: #include<bits/stdc++.h> using namespace std; const int M = 3e4+10; int f[M],cnt; struct node{ int u,v,w; }e[100010]; bool cmp(node a,node

BZOJ 1083: [SCOI2005]繁忙的都市 裸的最小生成树

题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1083 代码: #include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int maxn = 333; const int maxm = maxn*maxn; struct Edge { int u, v, w; bool operator < (

bzoj 1083: [SCOI2005]繁忙的都市

1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #define M 10005 5 using namespace std; 6 struct data 7 { 8 int u,v,w; 9 }a[M]; 10 int n,m,fa[M],sum; 11 bool cmp(data a1,data a2) 12 { 13 return a1.w<a2.w; 14 } 15 int fi

BZOJ 1083:[SCOI2005]繁忙的都市(最小生成树)

1083: [SCOI2005]繁忙的都市 Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连接.这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了.每条道路都有一个分值,分值越小表示这个道路越繁忙,越需要进行改造.但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的要求: 1. 改造的那些道路能够把

1083: [SCOI2005]繁忙的都市

---恢复内容开始--- 1083: [SCOI2005]繁忙的都市 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1611  Solved: 1040[Submit][Status][Discuss] Description 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连接.这些道路是双向的,且

Bzoj1083 1083: [SCOI2005]繁忙的都市【MST】

大水题,真不知道出题者是怎么把这么水的题出的这么长的TAT 其实这题在于考语文水平,一共三个要求,前两个要求意思就是要选出的道路是树形的,最后一个要求就是要权值最小,于是整个题意说白了就是求一棵MST,以前向星的形式给出最容易想到kruskal算法,于是这题顺利结束,从看题一直到调试结束半个小时搞定…… #include<iostream> #include<cstdio> #include <math.h> using namespace std; intfather

bzoj1083: [SCOI2005]繁忙的都市(最小生成树)

1083: [SCOI2005]繁忙的都市 题目:传送门 题解: 一道大水题: 第一问明显输出n-1 第二问最小生成树 秒切 代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 struct node 8 { 9 int x,y,c; 10 }a

BZOJ1083: [SCOI2005]繁忙的都市

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1083 题目大意:城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道 路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连 接.这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了.每条道路都有一个分值,分值越小表示这 个道路越繁忙,越需要进行改造.但是市政府的资金有限,市长希望进行改造的道