8.4 bzoj1083 [SCOI2005]繁忙的都市

来源:互联网 发布:引入js文件加上随机数 编辑:程序博客网 时间:2024/05/08 12:10

<span style="font-family: arial, verdana, helvetica, sans-serif;">题目描述</span>
城市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,表示你选出了几条道路,分值最大的那条道路的分值是多少。

样例输入

4 5
1 2 3
1 4 5
2 4 7
2 3 6
3 4 8

样例输出

3 6

题解:很裸的最小生成树,1知最终的图要联通,2知边数最少,1+2知边数最少只能为n-1条边,3知最大边

最小,1+2+3知裸的最小生成树,于是就乱搞过去了

第一遍交编译错误,居然是因为sort在algorithm库中,比赛时要注意了,iostream大法好

#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <algorithm>#include <memory.h>#include <math.h>#include <queue>#include <stack>#include <map>#include <vector>#include <limits.h>#include <malloc.h>#include <ctype.h>#include <float.h>using namespace std;int i,n,m,cur,u,v,c;int b[10005];struct node{    int u,v,c;    node(){u=v=c=0;}    bool operator < (const node& a) const{        if(c<a.c) return true;        return false;    }} e[1000005];int find(int x){return x==b[x]?x:find(b[x]);}void unit(int x,int y){b[find(x)]=find(y);}int main(){    scanf("%d %d",&n,&m);    printf("%d ",n-1);    for(i=1;i<=m;i++){        scanf("%d %d %d",&u,&v,&c);        cur++;        e[cur].u=u;        e[cur].v=v;        e[cur].c=c;    }    sort(e+1,e+cur+1);    for(i=1;i<=n;i++)        b[i]=i;    n--;    i=0;    while(n>0){        i++;        if(find(e[i].u)==find(e[i].v))            continue;        n--;        unit(e[i].u,e[i].v);    }    printf("%d\n",e[i].c);    return 0;}



1 0
原创粉丝点击