洛谷——P2330 [SCOI2005] 繁忙的都市

P2330 [SCOI2005] 繁忙的都市

题目描述

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

1.改造的那些道路能够把所有的交叉路口直接或间接的连通起来。

2.在满足要求1的情况下,改造的道路尽量少。

3.在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小。

任务:作为市规划局的你,应当作出最佳的决策,选择那些道路应当被修建。

输入输出格式

输入格式:

 

第一行有两个整数n,m表示城市有n个交叉路口,m条道路。接下来m行是对每条道路的描述,u, v, c表示交叉路口u和v之间有道路相连,分值为c。(1≤n≤300,1≤c≤10000)

 

输出格式:

 

两个整数s, max,表示你选出了几条道路,分值最大的那条道路的分值是多少。

 

输入输出样例

输入样例#1:
4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8
输出样例#1:
3 6
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 const int maxn=10001;
 7 struct jgt{
 8     int x;
 9     int y;
10     int w;
11 }a[maxn];
12 
13 int dad[maxn];
14 int n,k,tot,ans,tt;
15 
16 int find(int x)
17 {
18     if(x!=dad[x]) dad[x]=find(dad[x]);
19     return dad[x];
20 }
21 
22 int unionn(int a,int b)
23 {
24     int r1=find(a);
25     int r2=find(b);
26     if(r1!=r2)
27     return dad[r1]=r2;
28 }
29 
30 int cmp(const jgt &a,const jgt &b)
31 {
32     if(a.w<b.w) return 1;
33     else return 0;
34 }
35 
36 void search()
37 {
38     for(int i=1;i<=k;i++)
39     {
40         if(find(a[i].x)!=find(a[i].y)){
41             unionn(a[i].x,a[i].y);
42             tot=a[i].w;
43             tt++;
44         }
45         if(tt==n-1) break;
46     }
47     cout<<n-1<<" "<<tot<<endl;
48 }
49 
50 int main()
51 {
52     cin>>n>>k;
53     for(int i=1;i<=n;i++)
54     dad[i]=i;
55     for(int i=1;i<=k;i++){
56         cin>>a[i].x>>a[i].y>>a[i].w;
57     }
58     sort(a+1,a+k+1,cmp);
59     search();
60     return 0; 
61 }

 

posted @ 2017-04-13 21:36  橘生淮南终洛枳  阅读(138)  评论(0编辑  收藏  举报