Unblocked Project (MST, where MST is not available)

Source: Internet
Author: User

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 through the highway). 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 Description:

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 Description:
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 "?".
Example 1 input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Output
3
?
Note: This example does not have a minimum spanning tree, so finally we judge whether all nodes belong to the same collection.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include < vector> #include <map> #include <set> #include <queue> #include <stack> #include <
Algorithm> using namespace std;
#define INF 0x3f3f3f3f #define N-th 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)//Find root Node {if (tree[x]==-1) return x;
        else{int tmp=findroot (tree[x]);
        tree[x]=tmp;
    return TMP;
    }} int main () {int n,m,i,j; while (~SCANF ("%d%d", &n,&m) &&n!=0) {for (i=1;i<=n;i++) {scanf ("%d%d%d
        ", &edge[i].a,&edge[i].b,&edge[i].cost);
        } sort (edge+1,edge+1+n);
		
        for (i=1;i<=m;i++) tree[i]=-1; int ans=0;
        int A, B;
           for (i=1;i<=n;i++) {a=findroot (EDGE[I].A); B=findroot (EDGE[I].B);
                if (a!=b) {tree[a]=b;
            Ans+=edge[i].cost; 
		}} if (m==1) printf ("%d\n", ans);
			else {a=findroot (1); 
				for (i=2;i<=m;i++) {b=findroot (i);
			if (a!=b) {printf ("\ n"); break;}  
		} if (i==m+1) printf ("%d\n", ans);
}} return 0; }



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.