"Nine degrees OJ" topic 1024: Smooth Engineering Problem Solving report

Source: Internet
Author: User
"Nine degrees OJ" topic 1024: Smooth Engineering problem Solving report

Label (Space delimited): nine degrees OJ

Original title address: http://ac.jobdu.com/problem.php?pid=1024 title Description:

The goal of the provincial government's "unblocked project" is to enable road traffic between any of the two villages in the province (but not necessarily directly connected roads, as long as they can be reached indirectly by road). After a survey, the resulting statistical tables list the costs of several roads that are likely to build roads. You are now asked to write a program to calculate the minimum cost of the province's smooth need. Input:

The test input contains several test cases. The 1th line of each test case gives the number of road strips evaluated N, the number of villages m (n, m < = 100), and the subsequent N rows corresponding to the cost of the inter-village road, each with a pair of positive integers, the number of two villages, and the cost of the road between the two villages (also a positive integer). For simplicity, the village is numbered from 1 to M. When n is 0 o'clock, all input ends and the corresponding result is not output. Output:

For each test case, output the minimum cost for the province to be unblocked in line 1. If the statistics are not sufficient to ensure smooth, then output "?". Sample Input:

3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample output:
3
?
Ways

Minimum spanning tree problem. For the minimum spanning tree, sort the edges first, and then traverse all the edges to see if the side has been added to the spanning tree, and if not, add the edge, and the final cost of generating the tree is updated.

Pay attention to several equal signs, or you will not finish all the edges.

This question is how to judge the next, whether there is a minimum spanning tree problem, the previous several problems are fused, according to the known conditions can be established several trees, there will be how many-1 appear.

I write the code in the process, the main error is to n,m not distinguish, resulting in a loop error. Note that n is the number of bars, that is, the number of edges, and M is the number of points. These two must distinguish correctly.

Sort is the order of the edges, and the first feeling of findroot is to traverse the vertices, actually traversing the edges, and then finding each vertex of the edge to operate. The two places are wrong and there will be a big problem, pay attention.

#include <stdio.h> #include <algorithm> using namespace std;

#define N 101 int tree[n];
    struct Edge {int A, B;

    int cost;
    BOOL operator< (const Edge &a) Const {return cost < A.cost;

}} edge[6000];
    int findRoot (int x) {if (tree[x] = =-1) {return x;
        } else {int temp = FindRoot (tree[x]);
        TREE[X] = temp;
    return temp;
    }} int main () {int n, m;
            while (scanf ("%d%d", &n, &m)! = EOF && n! = 0) {for (int i = 1; I <= n; i++) {//= equals
        scanf ("%d%d%d", &edge[i].a, &edge[i].b, &edge[i].cost);
        } for (int i = 1; I <= N; i++) {//equals tree[i] = 1;
        } int answer = 0; Sort (Edge + 1, Edge + n + 1);//is n, not m for (int i = 1; I <= n; i++) {//is n, not m int aroot = FindRoot (E
            DGE[I].A);
            int broot = FindRoot (edge[i].b);
        if (aroot! = broot) {        Tree[aroot] = Broot;
            Answer + = Edge[i].cost;
        }} int count = 0;
            for (int i = 1; I <= m; i++) {if (tree[i] = = 1) {count++;
        }} if (count = = 1) {printf ("%d\n", answer);
        } else {printf ("? \ n");
}} return 0; }
Date

March 10, 2017

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.