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

      

    题目描述

    城市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 #include <cmath>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 
     7 using namespace std;
     8 
     9 struct node
    10 {
    11     int u,v,cost;    
    12 };
    13 
    14 node a[100005];
    15 int f[100005];
    16 int N;
    17 int Len=0;
    18 
    19 bool cmp(node i,node j)
    20 {
    21     return i.cost < j.cost;
    22 }
    23 
    24 int find(int X)
    25 {
    26     if (f[X] != X) f[X]=find(f[X]);
    27     return f[X];
    28 }
    29 
    30 int main()
    31 {
    32     int M;
    33     scanf("%d %d",&N,&M);
    34     for (int i=1; i<=N; i++)
    35     {
    36         f[i]=i;
    37     }
    38     for (int i=1; i<=M; i++)
    39     {
    40         scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].cost);
    41     }
    42     int ans=0; int total=0;
    43     sort(a+1,a+M+1,cmp);
    44     for (int i=1; i<=M; i++)
    45     {
    46         int fx=find(a[i].u);
    47         int fy=find(a[i].v);
    48         if (fx > fy) swap(fx,fy);
    49         if (fx == fy) continue;
    50         if (fx != fy)
    51         {
    52             f[fy]=fx;
    53             ans=max(ans,a[i].cost);
    54             total++;
    55             if (total == N) break;
    56         }
    57     }
    58     printf("%d %d\n",total,ans);
    59 }
    Show My Ugly Code
  • 相关阅读:
    《应用Yii1.1和PHP5进行敏捷Web开发》学习笔记(转)
    YII 小模块功能
    Netbeans代码配色主题大搜集
    opensuse 启动巨慢 解决方法 90s多
    opensuse 安装 网易云音乐 rpm netease music
    linux qq rpm deb opensuse
    openSUSE 安装 alien
    第一行代码 Android 第2版
    Android Studio AVD 虚拟机 联网 失败
    docker error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuratio
  • 原文地址:https://www.cnblogs.com/TUncleWangT/p/7064942.html
Copyright © 2011-2022 走看看