2011-12-16 08:40:04Morris

[UVA] 11733 - Airports

A

Airports

 

 

The government of a certain developing nation wants to improve transportation in one of its most inaccessible areas, in an attempt to attract investment. The region consists of several important locations that must have access to an airport.

 

Of course, one option is to build an airport in each of these places, but it may turn out to be cheaper to build fewer airports and have roads link them to all of the other locations. Since these are long distance roads connecting major locations in the country (e.g. cities, large villages, industrial areas), all roads are two-way. Also, there may be more than one direct road possible between two areas. This is because there may be several ways to link two areas (e.g. one road tunnels through a mountain while the other goes around it etc.) with possibly differing costs.

 

A location is considered to have access to an airport either if it contains an airport or if it is possible to travel by road to another location from there that has an airport.

 

You are given the cost of building an airport and a list of possible roads between pairs of locations and their corresponding costs. The government now needs your help to decide on the cheapest way of ensuring that every location has access to an airport. The aim is to make airport access as easy as possible, so if there are several ways of getting the minimal cost, choose the one that has the most airports.

 

Note: The input file is large; make sure your I/O code is fast.

 

 

Input

 

The first line of input contains the integer T(T<25), the number of test cases. The rest of the input consists of T cases.

 

Each case starts with three integers N, M and A (0<N<=10,000, 0<=M<=100,000, 0<A<=10,000) separated by white space. N is the number of locations, M is the number of possible roads that can be built, and A is the cost of building an airport.

 

The following M lines each contain three integers X, Y and C (1<=X, Y<=N, 0<C<=10,000), separated by white space. X and Y are two locations, and C is the cost of building a road between X and Y.

 

Output

 

Your program should output exactly T lines, one for each case. Each line should be of the form "Case #X: Y Z", where X is the case number Y is the minimum cost of making roads and airports so that all locations have access to at least one airport, and Z is the number of airports to be built. As mentioned earlier, if there are several answers with minimal cost, choose the one that maximizes the number of airports.

 

 

Sample Input

Sample Output

2

4 4 100

1 2 10

4 3 12

4 1 41

2 3 23

5 3 1000

1 2 20

4 5 40

3 2 30

Case #1: 145 1

Case #2: 2090 2

 




作法 : MST (最小生成樹),  Kruskal算法
使用"并查集" Disjoint Sets

#include<stdio.h>
#include<stdlib.h>
typedef struct {
    int x, y, v;
}Edge;
int cmp(const void *i, const void *j) {
    Edge *a, *b;
    a = (Edge *)i, b = (Edge *)j;
    return a->v - b->v;
}
int P[10001], R[10001];
void MakeSet(int n) {
    static int i;
    for(i = 0; i <= n; i++)
        P[i] = i, R[i] = 1;
}
int FindP(int x) {
    if(P[x] != x)
        P[x] = FindP(P[x]);
    return P[x];
}
void Link(int x, int y) {
    if(R[x] < R[y])
        P[x] = y, R[y] += R[x];
    else
        P[y] = x, R[x] += R[y];
}
int Union(int x, int y, int *val) {
    x = FindP(x), y = FindP(y);
    if(x != y) {
        Link(x, y);
        (*val) = (*val) - 1;
        return 1;
    }
    return 0;
}
inline int readchar() {
    const int N = 1048576;
    static char buf[N];
    static char *p = buf, *end = buf;
    if(p == end) {
        if((end = buf + fread(buf, 1, N, stdin)) == buf) return EOF;
        p = buf;
    }
    return *p++;
}
inline int ReadInt(int *x) {
    static char c, neg;
    while((c = readchar()) < '-')    {if(c == EOF) return EOF;}
    neg = (c == '-') ? -1 : 1;
    *x = (neg == 1) ? c-'0' : 0;
    while((c = readchar()) >= '0')
        *x = (*x << 3) + (*x << 1) + c-'0';
    *x *= neg;
    return 1;
}
int main() {
    int t, N, M, A, i, C = 0, tM;
    Edge D[100000];
    ReadInt(&t);
    while(t--) {
        ReadInt(&N), ReadInt(&M), ReadInt(&A);
        for(i = 0, tM = 0; i < M; i++) {
            ReadInt(&D[tM].x), ReadInt(&D[tM].y), ReadInt(&D[tM].v);
            if(D[tM].v < A)    tM++;
        }
        qsort(D, tM, sizeof(Edge), cmp);
        /*Disjoint Sets*/
        MakeSet(N);
        int val = N, cost = 0;
        for(i = 0; i < tM; i++)
            cost += D[i].v*Union(D[i].x, D[i].y, &val);
        printf("Case #%d: %d %d\n", ++C,cost+val*A, val);
    }
    return 0;
}