7-11 Special minimum cost road repair (10 points)

There are some road connections between n towns, but the roads are all earth roads in disrepair. The government is now preparing to transform some of the earth roads into standard roads, hoping that standard roads can connect all towns and cities with the lowest total cost. However, one of the towns is special and limited by topography, and at most two standard roads can pass through the town. . Please write a program to find a transformation plan that meets the above conditions and has the smallest total cost. If there is no transformation plan, it can also be identified. Assume that the road is two-way.

Input format: The
input contains multiple sets of data. The first line of each group of data is 3 integers n, v, and e, all of which do not exceed 50. n is the number of towns, and the towns are numbered from 0 to n-1. v is the number of the town with the most two standard roads, e is the number of existing dirt roads, and then the e row represents the information of each road, and each row has 3 non-negative integers a, b, and c, representing the towns a and There is an existing road between town b. If it is transformed into a standard road, the cost is c.

Output format:
output one line for each group of data, which is an integer, indicating the minimum cost to meet the requirements, if there is no modification plan, output -1.

Input sample:

5 0 8
0 1 1
0 2 1
0 3 1
0 4 1
1 4 100
1 2 100
2 3 100
3 4 100
5 0 4
0 1 1
0 2 1
0 3 1
0 4 1

Sample output:

202
-1

The Kruskal algorithm is used. Greedy strategy, when selecting the edge with the smallest weight, it is necessary to consider that it cannot form a ring, and at the same time, it must meet the special requirements of the problem.
At that time, I just thought it was a minimum spanning tree problem. Maybe the test data in the background is also exactly this (there are only two test points, which is really a bit less.), a big guy in the training camp, and it happens to be our class. According to the test data, this group of tests should output 4 according to the meaning of the question, but the actual output is -1, indicating that the minimum spanning tree that meets the requirements is not necessarily the final one.

4 0 4
0 1 1
0 2 1
0 3 1
1 2 2

You can test the data code in the background.
#include <algorithm>
#include <iostream>
using namespace std;

typedef struct {
    
    
    int u;
    int v;
    int w;
} Road;

Road road[51];
int f[51];

bool cmp(Road r1, Road r2) {
    
     return r1.w < r2.w; }

void init() {
    
    
    for (int i = 0; i < 51; i++) {
    
    
        f[i] = i;
    }
}

int find(int x) {
    
    
    if (x == f[x])
        return x;
    return f[x] = find(f[x]);
}

int merge(int x, int y) {
    
    
    int a = find(x);
    int b = find(y);
    if (a != b) {
    
    
        f[b] = a;
        return 1;
    }
    return 0;
}

int main() {
    
    
    int n, v, e;
    while (scanf("%d %d %d", &n, &v, &e) != EOF) {
    
    
        init();
        for (int i = 0; i < e; i++) {
    
    
            scanf("%d %d %d", &road[i].u, &road[i].v, &road[i].w);
        }
        int cnt = 0, sum = 0, num = 0;
        sort(road, road + e, cmp);
        for (int i = 0; i < e; i++) {
    
    
            if ((road[i].u == v || road[i].v == v) && cnt < 2) {
    
    
                if (merge(road[i].u, road[i].v)) {
    
    
                    sum += road[i].w;
                    cnt++;
                    num++;
                }
            } else if (road[i].u != v && road[i].v != v) {
    
    
                if (merge(road[i].u, road[i].v)) {
    
    
                    sum += road[i].w;
                    num++;
                }
            }
            if (num == n - 1)
                break;
        }
        if (num == n - 1 && cnt <= 2)
            printf("%d\n", sum);
        else
            printf("-1\n");
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45845039/article/details/113478078