hdu_1879 unblocked Project (minimum spanning tree)

Source: Internet
Author: User

continue to smooth the project Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16631 Accepted Submission (s): 7149


The goal of the problem Description 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 via highways). A table of urban roads is available, showing the cost of building roads between any two towns and whether the road has been repaired. You are now asked to write a program to calculate the minimum cost of the province's smooth need.

The input test inputs contain several test cases. The 1th row of each test case gives the number of villages N (1< N < 100), and the subsequent N (N-1)/2 lines correspond to the cost and construction state of the Inter-village road, each of which gives 4 positive integers, the number of two villages (from 1 to N), the cost of the road between the two villages, and construction Status: 1 is built, 0 means not built.

When n is 0 o'clock, the input ends.
Output takes one row for each test case, and the lowest cost is required to smooth out the province.
Sample Input

3 1 2 1 0 1 3 2 0 2 3 4 0 3 1 2 1 0 1 3 2 0 2 3 4 1 3 1 2 1 0 1 3 2 1 2 3 4 1 0
Sample Output
3 1 0

Test Instructions:

From the test instructions can be seen as a minimum spanning tree, with kruscal feel will be more convenient, but for the road has been repaired to special treatment, equivalent to a weight of 0, so that the first choice has been repaired road to complete the connected diagram.

Code implementation:

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <

cstring>//#include <bits/stdc++.h> #define MAX #define INF 0xfffffff using namespace std;
int N;
int weight;
    struct edge{int from,to;
int cost;
};              int Rank[max];            Height of the tree int Father[max];        Father Edge Edges[max*max];
Edge set Array int Find (int x);
void ini (int V);
int kruscal ();
BOOL Same (int x,int y);
void Unite (int x,int y);
BOOL Comp (const edge& E1, const edge& E2);
        int main () {while (scanf ("%d", &n)!=eof && N! = 0) {int a,b,cos,flag;
        INI (N);
            for (int i = 0; I < n (N-1)/2; i++) {scanf ("%d%d%d%d", &a,&b,&cos,&flag);
            if (flag) cos = 0;
            Edges[i].from = A;
            Edges[i].to = b;
        edges[i].cost = cos;
        } weight = kruscal ();
    printf ("%d\n", weight); } RetuRN 0;
    } int kruscal () {sort (edges,edges+n* (N-1)/2,comp);
    int res = 0;
    int num = 0;
    int times = n (N-1)/2;
        for (int i = 0; I < times; i++) {Edge e = edges[i];
            if (!same (E.from, e.to)) {Unite (e.from,e.to);
            Res + = E.cost;
        num++;
    }//optimization, the number of sides of the connected graph is N-1 if (num = = N-1) break;
} return res;

} BOOL Comp (const edge& E1, const edge& E2) {return e1.cost < e2.cost;}

BOOL Same (int x,int y) {return find (x) = = Find (y);}
    int Find (int x) {int k,j,r;
    R = x;
    while (r! = Father[r]) R = Father[r];
    k = x;
        while (k! = r) {j = father[k];
        Father[k] = r;
    K = J;
} return R;
    } void Unite (int x,int y) {x = Find (x);
    y = Find (y);
    if (x = = y) return;
    if (Rank[x] < rank[y]) father[x] = y;
        else {father[y] = x; if (rank[x] = = RaNk[y]) rank[x]++;
} return;
    } void ini (int V) {weight = 0;
        for (int i = 1; I <= V; i++) {father[i] = i;
    Rank[i] = 1;
} return;
 }



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.