BZOJ1083: [SCOI2005]繁忙的都市

来源:互联网 发布:淘宝二手书出售流程 编辑:程序博客网 时间:2024/04/27 17:30
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1083

题目大意:城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市C的道
     路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路相连
     接。这些道路是双向的,且把所有的交叉路口直接或间接的连接起来了。每条道路都有一个分值,分值越小表示这
     个道路越繁忙,越需要进行改造。但是市政府的资金有限,市长希望进行改造的道路越少越好,于是他提出下面的
     要求: 1. 改造的那些道路能够把所有的交叉路口直接或间接的连通起来。 2. 在满足要求1的情况下,改造的
     道路尽量少。 3. 在满足要求1、2的情况下,改造的那些道路中分值最大的道路分值尽量小。任务:作为市规划
     局的你,应当作出最佳的决策,选择那些道路应当被修建。

题解:最小生成树裸题

代码:

 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 #define N 50000 7 using namespace std; 8 int n,m,ans,cnt; 9 int fa[N];10 struct data{11     int u,v,val;12 }a[N];13 int read()14 {15     int x=0; char ch; bool bo=0;16     while (ch=getchar(),ch<'0'||ch>'9') if (ch=='-') bo=1;17     while (x=x*10+ch-'0',ch=getchar(),ch>='0'&&ch<='9');18     if (bo) return -x; return x;19 }20 bool cmp(data a,data b)21 {22     return a.val<b.val;23 }24 int find(int x)25 {26     if (fa[x]!=x) fa[x]=find(fa[x]);27     return fa[x];28 }29 int main()30 {31     n=read(); m=read();32     for (int i=1; i<=m; i++)33     {34         a[i].u=read(),a[i].v=read(),a[i].val=read();35     }36     for (int i=1; i<=n; i++) fa[i]=i;37     sort(a+1,a+m+1,cmp);38     for (int i=1; i<=m; i++)39     {40         int q=find(a[i].u),p=find(a[i].v);41         if (p==q) continue;42         ans=a[i].val; cnt++;43         fa[q]=p;44         if (cnt==n-1) break;45     }46     printf("%d %d\n",n-1,ans);47 }
View Code

 

0 0
原创粉丝点击