zoukankan      html  css  js  c++  java
  • There is No Alternative~最小生成树变形

    Description

    ICPC (Isles of Coral Park City) consist of several beautiful islands.

    The citizens requested construction of bridges between islands to resolve inconveniences of using boats between islands, and they demand that all the islands should be reachable from any other islands via one or more bridges.

    The city mayor selected a number of pairs of islands, and ordered a building company to estimate the costs to build bridges between the pairs. With this estimate, the mayor has to decide the set of bridges to build, minimizing the total construction cost.

    However, it is difficult for him to select the most cost-efficient set of bridges among those connecting all the islands. For example, three sets of bridges connect all the islands for the Sample Input 1. The bridges in each set are expressed by bold edges in Figure F.1.

    Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

    Figure F.1. Three sets of bridges connecting all the islands for Sample Input 1

    As the first step, he decided to build only those bridges which are contained in all the sets of bridges to connect all the islands and minimize the cost. We refer to such bridges as no alternative bridges. In Figure F.2, no alternative bridges are drawn as thick edges for the Sample Input 1, 2 and 3.

    Write a program that advises the mayor which bridges are no alternative bridges for the given input.

    Input

    The input consists of several tests case.

    Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

    Figure F.2. No alternative bridges for Sample Input 1, 2 and 3

    N MS1 D1 C1SM DM CMN MS1 D1 C1⋮SM DM CM

    For each test, the first line contains two positive integers N and M . N represents the number of islands and each island is identified by an integer 1 through NM represents the number of the pairs of islands between which a bridge may be built.

    Each line of the next M lines contains three integers SiDi and Ci (1 ≤ i ≤ M) which represent that it will cost Ci to build the bridge between islands Si and Di. You may assume 3 ≤ N ≤ 500, N − 1 ≤ M ≤ min(50000, N(N − 1)/2), 1 ≤ Si < Di ≤ N, and 1 ≤ Ci ≤ 10000. No two bridges connect the same pair of two islands, that is, if i ≠ j and Si = Sj , then Di ≠ Dj. If all the candidate bridges are built, all the islands are reachable from any other islands via one or more bridges.

    Output

    Output two integers, which mean the number of no alternative bridges and the sum of their construction cost, separated by a space.

    Sample Input

    4 4
    1 2 3
    1 3 3
    2 3 3
    2 4 3
    
    4 4
    1 2 3
    1 3 5
    2 3 3
    2 4 3
    
    4 4
    1 2 3
    1 3 1
    2 3 3
    2 4 3
    
    3 3
    1 2 1
    2 3 1
    1 3 1

    Sample Output

    1 3
    3 9
    2 4
    0 0

    可以组成多种最小生成树,求他们的公共边,和权值和;
    这个n ,可以直接暴力枚举;
    暴力出奇迹
    暴力枚举一下就好了;
    先求出一个最小生成树,记录边;
    依次删边,看新的最小生成树的权值是否相等
    不相等则证明,必须有的边,

     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <vector>
     4 #include <queue>
     5 #include <cstring>
     6 #include <string>
     7 #include <cmath>
     8 #include <map>
     9 using namespace std;
    10 const int maxn = 5e4 + 10;
    11 const int INF = 1e9 + 7;
    12 int fa[510], vis[maxn];
    13 struct node {
    14     int u, v, w;
    15 } qu[maxn];
    16 int cmp(node a, node b) {
    17     return  a.w < b.w;
    18 }
    19 int find(int x) {
    20     return fa[x] == x ? x : fa[x] = find(fa[x]);
    21 }
    22 int combine(int x, int y) {
    23     int nx = find(x);
    24     int ny = find(y);
    25     if(nx != ny) {
    26         fa[nx] = ny;
    27         return 1;
    28     }
    29     return 0;
    30 }
    31 int kruskal(int num, int flag, int x) {
    32     int sum = 0, k = 0;
    33     for(int i = 0; i < num; i++) {
    34         if(x == i)  continue;
    35         if(combine(qu[i].u, qu[i].v)) {
    36             sum += qu[i].w;
    37             if(flag)  vis[k++] = i;
    38         }
    39     }
    40     return sum;
    41 }
    42 int main() {
    43  //   freopen("DATA.txt", "r", stdin);
    44     int n, m;
    45     while(scanf("%d%d", &n, &m) != EOF) {
    46         for (int i = 0 ; i < m ; i++) {
    47             scanf("%d%d%d", &qu[i].v, &qu[i].u, &qu[i].w);
    48         }
    49         sort(qu, qu + m, cmp);
    50         int temp = kruskal(m, 1, -1);
    51         int ans1 = 0, ans2 = 0;
    52         for (int i = 1 ; i <= n ; i++) fa[i] = i;
    53         for (int i = 0 ; i < n - 1 ; i++ ) {
    54             for (int j = 0 ; j <= n ; j++) fa[j] = j;
    55             int sum = kruskal(m, 0, vis[i]);
    56             if (sum != temp) {
    57                 ans1++;
    58                 ans2 += qu[vis[i]].w;
    59             }
    60         }
    61         printf("%d %d
    ", ans1, ans2 );
    62     }
    63     return 0;
    64 }


  • 相关阅读:
    《深度剖析CPython解释器》25. 解密Python中的多线程(第一部分):初识GIL、以及多个线程之间的调度机制
    《深度剖析CPython解释器》24. Python运行时环境的初始化、源码分析Python解释器在启动时都做了哪些事情?
    《深度剖析CPython解释器》23. 剖析Python中模块的导入机制、Python是如何加载模块的
    《深度剖析CPython解释器》22. 解密Python中的生成器对象,从字节码的角度分析生成器的底层实现以及执行逻辑
    《深度剖析CPython解释器》21. Python类机制的深度解析(第五部分): 全方位介绍Python中的魔法方法,一网打尽
    《深度剖析CPython解释器》20. Python类机制的深度解析(第四部分): 实例对象的创建、以及属性访问
    redis 集群
    redis 主从
    文件的上传与下载
    Jsp
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9009265.html
Copyright © 2011-2022 走看看