zoukankan      html  css  js  c++  java
  • 2455 繁忙的都市

    题目描述 Description

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

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

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

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

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

    输入描述 Input Description

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

    输出描述 Output Description

    两个整数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
    数据范围及提示 Data Size & Hint

    见题面

    分类标签 Tags

    直接上Kruskal暴力

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 struct node
     7 {
     8     int u;
     9     int v;
    10     int w;
    11 }edge[1000001];
    12 int num=1;
    13 int f[1000001];
    14 int find(int x)
    15 {
    16     if(x!=f[x])
    17     f[x]=find(f[x]);
    18     return f[x];
    19 }
    20 void unionn(int x,int y)
    21 {
    22     int fx=find(x);
    23     int fy=find(y);
    24     f[fx]=fy;
    25 }
    26 int cmp(const node &a,const node &b)
    27 {
    28     return a.w<b.w;
    29 }
    30 int main()
    31 {
    32     int n,m;
    33     scanf("%d%d",&n,&m);
    34     for(int i=1;i<=n;i++)f[i]=i;
    35     for(int i=1;i<=m;i++)
    36     {
    37         int x,y,z;
    38         scanf("%d%d%d",&x,&y,&z);
    39         edge[num].u=x;
    40         edge[num].v=y;
    41         edge[num].w=z;
    42         num++;
    43     }
    44     sort(edge+1,edge+num,cmp);
    45     int k=0;
    46     int maxn=-1;
    47     for(int i=1;i<=m;i++)
    48     {
    49         if(find(edge[i].u)!=find(edge[i].v))
    50         {
    51             if(edge[i].w>maxn)
    52             maxn=edge[i].w;
    53             k++;
    54             unionn(edge[i].u,edge[i].v);
    55         }
    56         if(k==n-1)break;
    57     }
    58     printf("%d %d",n-1,maxn);
    59     return 0;
    60 }
  • 相关阅读:
    Api记录
    选择排序、冒泡排序、二分查找(折半查找)
    inline关键字
    c++,虚函数,单继承,多继承虚表剖析
    关于类模版迭代器提出时的错误
    关于类模版迭代器提出时的错误2
    菱形继承,多继承,虚继承、虚表的内存结构全面剖析(逆向分析基础)
    VC++错误调试
    引用计数
    选择
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6705791.html
Copyright © 2011-2022 走看看