1083: [SCOI2005]繁忙的都市
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3590 Solved: 2247
[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
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8
Sample Output
HINT
Source
思考
沒什麼可說的,MST裸題。
#include<bits/stdc++.h>
using namespace std;
struct data{
int x,y,c;
bool operator < (const data &rhs)const{
return c<rhs.c;
}
}a[110000];
int fa[110000];
int find(int x){
return x==fa[x]?x:fa[x]=find(fa[x]);
}
inline void read(int &res){
static char ch;int flag=1;
while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}
int n,m;
int main(){
read(n),read(m);
for(register int i=1;i<=m;i++)
read(a[i].x),read(a[i].y),read(a[i].c);
sort(a+1,a+1+m);
for(register int i=1;i<=n;i++)fa[i]=i;
int ans=0;
for(register int i=1;i<=m;i++){
int fx=find(a[i].x),fy=find(a[i].y);
if(fy!=fx){
fa[fx]=fy,ans++;
if(ans==n-1){
printf("%d %d",n-1,a[i].c);
break;
}
}
}
return 0;
}