"Nine Degrees" topic 1024: Unblocked Works

Source: Internet
Author: User

Title Address: http://ac.jobdu.com/problem.php?pid=1024
Title Description:
The provincial government "unimpeded project" goal is to enable any two villages across the province can achieve road traffic (but not necessarily directly connected to the road, as long as the indirect through the highway can be reached). 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 lowest cost of the province in 1 rows. 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
?
Source:2007 Research on computer and software engineering of Zhejiang University: A tentative idea of real problems
and find the deformation problem of the set. And topic 1017: Still Smooth engineeringIn fact exactly the same, the difference is the length of the road, a cost size. Of course, you can use prim to do it. This is reflected in another topic 1417: Variant King Kong.
1, the storage of all nodes and the cost of spending.
2, sort. According to the cost from small to large.
3, according to value from small to large to do and check the set operation, found the number of roads count = = n-1 when the exit.
4, judge whether the count is less than n-1, if established, it means that all roads can not be connected. Otherwise, the road length is output.
C + + is used by the prim, Java uses the algorithm is to give the code. C + + AC
#include <stdio.h> #include <algorithm> const int MAXN = 102;
const int MAXM = 102;
int PARENT[MAXN];
using namespace Std;
    struct node{int start;
    int end;
int value;
    
}NODES[MAXM];
    int Compare (node Node1, node Node2) {if (Node1.value < Node2.value) {return 1;
} return 0;
    } int findparent (int f) {while (parent[f]! = f) {f = parent[f];
} return F;
    } void Uniontwo (int f, int t) {int a = findparent (f);
    int B = findparent (t);
    if (a = = b) return;
    if (a > B) {parent[a] = b;
    } else {parent[b] = A;
    }} int main () {int n;
    int m;
        while (scanf ("%d%d", &m,&n)!=eof) {if (M = = 0) {break;
        } int i = 0;
        for (i=0; i < m; i++) {scanf ("%d%d%d", &nodes[i].start, &nodes[i].end, &nodes[i].value);
        } sort (Nodes,nodes+m,compare); for (i = 1; i < n+1;
        i++) {Parent[i] = i;
        } int minValue = 0;
        int count = 0; for (i = 0; i < m; i++) {if (Findparent (nodes[i].start)! = Findparent (nodes[i].end)) {Uni
                Ontwo (Nodes[i].start,nodes[i].end);
                MinValue + = Nodes[i].value;
                count++;
                if (count = = n-1) {break;
        }}} if (Count < n-1) {printf ("? \ n");  
        }else{printf ("%d\n", MinValue);
}} return 0; }/************************************************************** problem:1024 user:wangzhenqing Languag E:c++ result:accepted time:10 Ms memory:1024 KB ************************************************************ ****/

Java AC

Import Java.io.BufferedReader;
Import Java.io.InputStreamReader;
Import Java.io.StreamTokenizer; public class Main {/* * 1024x768 * * public static void Main (string[] args) throws Exception {Strea
        Mtokenizer st = new Streamtokenizer (new BufferedReader (New InputStreamReader (system.in)));
            while (St.nexttoken ()! = streamtokenizer.tt_eof) {int n = (int) st.nval;
            if (n = = 0) {break; 
            } st.nexttoken ();  
            int m = (int) st.nval;
            int cost[][] = new int[m+1][m+1]; for (int i = 1, i < m+1; i++) {for (int j = 1; j < M+1; J + +) {Cost[i][j] = int Eger.
                Max_value; 
                }} for (int i = 0; i < n; i++) {st.nexttoken (); 
                int a = (int) st.nval; 
                St.nexttoken (); 
                int b = (int) st.nval; 
      St.nexttoken ();          int d = (int) st.nval;
                    if (Cost[a][b] > D) {cost[a][b] = D;
                Cost[b][a] = D;
            }} int mincost[] = new int[m+1];
            int visit[] = new int[m+1];
            for (int i = 1; i < m+1; i++) {mincost[i] = Cost[1][i];
        } prime (cost, Mincost, visit, M);
        }} private static void prime (int[][] cost, int[] mincost, int[] visit, int m) {mincost[1] = 0;
        VISIT[1] = 1;
        int Minj = 1;
        int rescost = 0;
            for (int i = 1; i < m; i++) {int min = Integer.max_value;
                    for (int j = 1; j < M+1; J + +) {if (visit[j] = = 0 && mincost[j] < min) {
                    min = Mincost[j];
                Minj = j;
            }} Visit[minj] = 1;
            Rescost + = min; for (int j = 1; J < M+1; J + +) {if (visit[j] = = 0 && mincost[j] > Cost[minj][j]) {Mincost[j] = cost[
                MINJ][J];
        }}} int num = 0;
            for (int i = 1; i < m + 1; i++) {if (visit[i] = = 1) {num + +;
    }} System.out.println (num = = m-rescost: "?"); }}/************************************************************** problem:1024 user:wzqwsrf Language : Java result:accepted time:160 Ms memory:23512 KB ********************************************************** ******/



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.