ZOJ-2966-Build The Electric System【5th浙江省赛】【最小生成树】

ZOJ-2966-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, 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

题目链接:ZOJ-2966

题目大意:给出城市数量和连通的道路。以及每条道路维修的价格。问最少的价格使得任意两个城市连通

题目思路:很裸的最小生成树,但是需要注意数组的范围。(2 <= N <= 500, N <= E <= N * (N - 1) / 2)

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
#define MAXN 250005
int fa[MAXN] = {0};
int ranks[MAXN] = {0};
long long ans = 0;
void initialise(int n)          //初始化
{       
    for (int i = 0; i <= n; i++) fa[i] = i;
}
int getfather(int v)            //父节点
{
    return (fa[v] == v) ? v : fa[v] = getfather(fa[v]);
}
void merge(int x,int y,int c)           //合并
{
    x = getfather(x);
    y = getfather(y);
    if (x != y || c == 0)
    {
        fa[x] = y;
        ans += c;
    }   
}

struct node
{
    int a,b,k;
}p[250005];

bool cmp(node x,node y)
{
    return x.k < y.k;
}
int main(){
    int t;
    cin >> t;
    while(t--)
    {
        memset(fa,-1,sizeof(fa));
        memset(ranks,0,sizeof(ranks));
        ans = 0;
        int n,e;
        cin >> n >> e;
        initialise(n);
        for (int i = 0; i < e; i++)
        {
            cin >> p[i].a >> p[i].b >> p[i].k; 
        }
        sort(p,p + e,cmp);
        for (int i = 0; i < e; i++)
        {
            merge(p[i].a,p[i].b,p[i].k);
        }
        cout << ans << endl;
    } 
    return 0;
}

你可能感兴趣的:(ZOJ,2966)