zoukankan      html  css  js  c++  java
  • zoj 2966 Build The Electric System

    ZOJ Problem Set - 2966
    Build The Electric System

    Time Limit: 1 Second      Memory Limit: 32768 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, A, B, K (0 <= A, B < 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
    Submit    Status
    //Wpl
    //1787279 2009-03-13 10:36:28 Wrong Answer  2966 C++ 10 184 Wpl 
    //1787280 2009-03-13 10:39:21 Accepted  2966 C++ 10 1172 Wpl 
    //ZOJ 2966 Build The Electric System 08年浙江省大学生程序设计竞赛
    #include <iostream>
    using namespace std;
    int edges[502][502],sum;
    struct node
    {
        
    int end;
        
    int len;
    }minedge[
    502];
    void Prim(int n)  //从0开始构建最小生成树
    {
        
    int v,k,j,min;
        
    for(v=1;v<n;v++)
        {
            minedge[v].end
    =0;
            minedge[v].len
    =edges[v][0];
        }
        minedge[
    0].len=-5;
        
    for(k=1;k<n;k++//找k-1条边
        {
            min
    =5000;
            v
    =k;
            
    for(j=1;j<n;j++)
                
    if(minedge[j].len>=0&&minedge[j].len<min)
                {
                    min
    =minedge[j].len;
                    v
    =j;
                }
            sum
    +=minedge[v].len;
            minedge[v].len
    =-5;
            
    for(j=1;j<n;j++)
            {
                    
    if(minedge[j].len==-1||(edges[j][v]<minedge[j].len&&edges[j][v]>=0))
                    {
                        minedge[j].len
    =edges[j][v];
                        minedge[j].end
    =v;
                    }
            }
        }

    }
    int main()
    {
        
    int t;
        scanf(
    "%d",&t);
        
    while(t--)
        {
            
    int n,e,i,j;
            scanf(
    "%d%d",&n,&e);
            
    for(i=0;i<n;i++)
                
    for(j=0;j<n;j++)
                    edges[i][j]
    =-1;
            
    int a,b,k;
            
    for(i=0;i<e;i++)
            {
                scanf(
    "%d%d%d",&a,&b,&k);
                edges[a][b]
    =k;
                edges[b][a]
    =k;
            }
            sum
    =0;
            Prim(n);
            printf(
    "%d\n",sum);
        }
        
    return 0;
    }
  • 相关阅读:
    基于C#和Asp.NET MVC开发部标视频和苏标主动安全监控平台
    基于Java Netty框架构建高性能的部标808协议的GPS服务器
    交通部796部标平台开发索引
    GPS部标监控平台的功能设计(一)-功能列表
    基于Java Mina框架的部标jt808服务器设计和开发
    基于Asp.NET MVC框架+SignalR +ActiveMQ + Ali OSS 服务构建苏标主动安全智能防控平台
    部标809协议2019版本与2011版本的区别
    出租车Jt/T 905协议与部标1078协议融合的网约车视频监控平台
    基于JT/T 1078协议设计和开发部标视频服务器
    基于Html5+HLS协议播放符合部标1078协议的实时流媒体视频
  • 原文地址:https://www.cnblogs.com/forever4444/p/1454653.html
Copyright © 2011-2022 走看看