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

    Escape Time II

    Time Limit: 20 Sec  Memory Limit: 256 MB

    题目连接

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2966

    Description

    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

    HINT

    题意

    给你个无向边带权图,让你输出最小生成树

    题解:

    Kruskal or prim

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)  
    #define maxn 200001
    #define mod 10007
    #define eps 1e-9
    int Num;
    char CH[20];
    //const int inf=0x7fffffff;   //нчоч╢С
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    inline void P(int x)
    {
        Num=0;if(!x){putchar('0');puts("");return;}
        while(x>0)CH[++Num]=x%10,x/=10;
        while(Num)putchar(CH[Num--]+48);
        puts("");
    }
    //**************************************************************************************
    
    
    int u[maxn],v[maxn],w[maxn],r[maxn];
    int pa[maxn],n,m;
    bool cmp(int i,int j)
    {
        return w[i]<w[j];
    }
    int fi(int x)
    {
        return pa[x]==x?x:pa[x]=fi(pa[x]);
    }
    int kruskal()
    {
        int ans=0;
        for(int i=0;i<n;i++)
            pa[i]=i;
        for(int i=0;i<m;i++)
            r[i]=i;
        sort(r,r+m,cmp);
        for(int i=0;i<m;i++)
        {
            int e=r[i];
            int x=fi(u[e]);
            int y=fi(v[e]);
            if(x!=y)
                ans+=w[e],pa[x]=y;
        }
        return ans;
    }
    int main()
    {
        //test;
        int t=read();
        while(t--)
        {
            n=read(),m=read();
            for(int i=0;i<m;i++)
            {
                int a=read(),b=read(),c=read();
                u[i]=a,v[i]=b,w[i]=c;
            }
            printf("%d
    ",kruskal());
        }
    }
  • 相关阅读:
    8.8全民健身日,扒一扒音视频互动与健身的那些事儿
    游戏视频开发平台的特点
    音视频开发技术之自主集成第三方编解码器
    视频开发之外部音视频输入
    视频开发技术之文件传输限速
    视频开发技术之文件传输
    ubuntu16.04LTS+pycharm kuaijiefangshi
    install cuda
    Tensorflow实现MNIST
    TensorFlow导入mnist数据集
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4540149.html
Copyright © 2011-2022 走看看