zoukankan      html  css  js  c++  java
  • zoj 2966 Build The Electric System(最小生成树)

    Build The Electric System

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    In last winter, there was a big snow storm in South China. The electric system was damaged seriously. Lots of power lines were broken and lots of villages lost contact with the main power grid. The government wants to reconstruct the electric system as soon as possible. So, as a professional programmer, you are asked to write a program to calculate the minimum cost to reconstruct the power lines to make sure there's at least one way between every two villages.

    Input

    Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. And it will be followed by T consecutive test cases.

    In each test case, the first line contains two positive integers N and E (2 <= N <= 500, N <= E <= N * (N - 1) / 2), representing the number of the villages and the number of the original power lines between villages. There follow E lines, and each of them contains three integers, ABK (0 <= AB < N, 0 <= K < 1000). A and B respectively means the index of the starting village and ending village of the power line. If K is 0, it means this line still works fine after the snow storm. If K is a positive integer, it means this line will cost K to reconstruct. There will be at most one line between any two villages, and there will not be any line from one village to itself.

    Output

    For each test case in the input, there's only one line that contains the minimum cost to recover the electric system to make sure that there's at least one way between every two villages.

    Sample Input

    1
    3 3
    0 1 5
    0 2 0
    1 2 9

    Sample Output

    5
    

    Author: ZHOU, Ran
    Source: The 5th Zhejiang Provincial Collegiate Programming Contest

    #include <iostream>
    #include<cstdio>
    #include<queue>
    #include<vector>
    using namespace std;
    struct node
    {
        int x,y,w;
        node(int a,int b,int c){x=a;y=b;w=c;}
    };
    int t,n,e;
    int team[505];
    struct cmp
    {
        bool operator()(node a,node b)
        {
            return a.w>b.w;
        }
    };
    int findteam(int k)
    {
        if (k!=team[k]) return team[k]=findteam(team[k]);
          else return k;
    }
    int main()
    {
        while(~scanf("%d",&t))
        {
        for(;t>0;t--)
        {
            scanf("%d%d",&n,&e);
            priority_queue<node,vector<node>,cmp> Q;
            for(int i=0;i<n;i++) team[i]=i;
            for(int i=1;i<=e;i++)
            {
                int x,y,z;
                scanf("%d%d%d",&x,&y,&z);
                Q.push(node(x,y,z));
            }
          int sum=0;
          while(!Q.empty())
          {
              node u=Q.top();
              Q.pop();
              int fx=findteam(u.x);
              int fy=findteam(u.y);
              if (fx!=fy)
              {
                  sum+=u.w;
                  team[fx]=fy;
              }
          }
          printf("%d
    ",sum);
        }
        }
        return 0;
    }
  • 相关阅读:
    Gretna2.0 使用过程中遇到的问题
    在外星人电脑上安装windows10和ubuntu16.04双系统小记
    Mac OS下PHP开发环境的搭建——基于XAMPP和IntelliJ IDEA
    在Kali上安装打印机
    Rails中关联数据表的添加操作(嵌套表单)
    痛苦的人生——JRuby on Rails的开发与部署小记
    Word技巧杂记(二)——批量修改修订格式并接受
    Ruby学习(三)——类与对象(1)
    Ruby学习笔记(二)——从管道读取数据
    Word技巧杂记(一)——去掉页眉上方的黑线
  • 原文地址:https://www.cnblogs.com/stepping/p/6406673.html
Copyright © 2011-2022 走看看