Unblocking Project II (nine degree OJ1024)

Posted by izy on Sun, 01 Dec 2019 13:38:23 +0100

Unblocking Project II (nine degree OJ1024)

Time limit: 1 second memory limit: 32 megabytes special question: no

1. Title Description:

The goal of the provincial government's "unblocking project" is to make highway traffic possible between any two villages in the province (but there is not necessarily a direct highway connection, as long as it can be reached indirectly through the highway). After investigation and evaluation, the cost of several roads that may be constructed is listed in the statistical table. Now, please write a program to calculate the lowest cost for the smooth flow of the whole province.
Enter a description:
The test input contains several test cases. The first line of each test case shows the number of evaluated roads n, the number of villages m (n, m < = 100); the next N lines correspond to the cost of roads between villages, each line gives a pair of positive integers, which are the number of two villages, and the cost of roads between the two villages (also a positive integer). For simplicity, villages are numbered from 1 to M. When n is 0, all inputs are ended and the corresponding results are not output.
Output Description:
For each test case, output the lowest cost required for smooth operation of the whole province in one line. If the statistics are not enough to ensure smooth flow, output "?".
Example 1
input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
output
3
?

2. Basic ideas

This problem is also the cost problem of the minimum spanning tree, which is solved by using the parallel search set as the basic data structure. However, there is a detail to be noted in this problem, that is, whether there is a minimum spanning tree or not, which needs to be judged according to this situation. The method of judgment is to check whether the number of connected components is 1 after generation

3. Code implementation

#include <iostream>
#include <algorithm>
#define N 101
using namespace std;
int Tree[N];

struct Path{
    int city1;
    int city2;
    int cost;
}buf[N];

bool cmp(Path p1,Path p2){

    return p1.cost<p2.cost;
}

int findRoot(int x){
    if(Tree[x]==-1)return x;
    else {
        int tmp;
        tmp = findRoot(Tree[x]);
        Tree[x] = tmp;
        return tmp;
    }
}
int main()
{
    int n,m;
    int a,b;
    while(scanf("%d",&n)!=EOF){
        if(n==0)break;
        for(int i=0;i<N;i++){
            Tree[i]=-1;
        }
        scanf("%d",&m);
        for(int i=0;i<n;i++){
            scanf("%d%d%d",&buf[i].city1,&buf[i].city2,&buf[i].cost);
        }
        sort(buf,buf+n,cmp);
        int TotalCost = 0;
        for(int i=0;i<n;i++){
                int rootA = findRoot(buf[i].city1);
                int rootB = findRoot(buf[i].city2);
                if(rootA!=rootB){//If not in a set
                    Tree[rootA] = rootB;//Put set A in B
                    TotalCost+=buf[i].cost;
                }
        }

        int cnt=0;
        for(int i=1;i<=m;i++){
            if(Tree[i]==-1){
                cnt++;
            }
        }
        if(cnt<=1)
            printf("%d\n",TotalCost);
        else//Cannot form minimum spanning tree
            printf("?\n");
    }
    return 0;
}
/*
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
*/