UVA, 11733 (kruscal, minimum spanning tree)

Source: Internet
Author: User












Airports



From:uva, 11733



Submit





Time Limit: MS








The Government of a certain developing nation wants to improve transportation in one's its most inaccessible areas Attempt to attract investment. The region consists of several important locations that must has access to an airport.





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





A considered to has access to an airport either if it contains an airport or if it's possible to travel by R Oad to another location from there.





You is given the cost of building an airport and a list of possible roads between pairs of locations and their correspond ing costs. The government now needs your-to-decide on the cheapest-the-a-ensuring-a-to-every location have access to an airport. The aim is to do airport access as easy as possible, so if there was several ways of getting the minimal cost, choose t He one and 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& lt;=100,000, 0<A<=10,000) separated by white space. N is the number of locations, M is the number of possible roads so can be built, and A are the Cost of building an airport.






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





Output






Your program should output exactly T lines, one for each case. Each line should being of the form "case #X: y Z", where X was the case number Y is the minimum cos T of making roads and airports so it all locations has access to at least one airport, and Z is the number of Airports to be built. As mentioned earlier, if there is several answers with minimal cost, choose the one, 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




Main topic:
There are N towns, M roads,need to build an airfield,The cost of building the road and the airfield tells you that you can output the maximum number of airfields that the village can reach at the airport, with the minimum cost and the minimum cost to build.
Problem Solving Ideas:Kruscal algorithm, the order of the edge, when the cost of the side is greater than the cost of the airport, discard the edge, build the number of airfields cnt++, the final airport cnt=cnt+ set number. Figure out the cost and.
Code:
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;

struct edge{int u,v,cost;};

const int maxm=100010,maxn=10010;

bool cmp(const edge& e1,const edge& e2){
    return e1.cost<e2.cost;
}

edge ed[maxm];
int v,e,air,parent[maxn],cnt,res,rank[maxn];
vector <int> myve;

void input(){
    scanf("%d%d%d",&v,&e,&air);
    for(int i=0;i<e;i++){
        scanf("%d %d %d",&ed[i].u,&ed[i].v,&ed[i].cost);
    }
}

void inti_union_find(int v){
    for(int i=0;i<=v;i++){
        parent[i]=i;
        rank[i]=1;
    }
}

void Union(int a,int b){
    if(rank[a]<rank[b]){
        parent[a]=b;
    }
    else{
        if(rank[a]==rank[b]) rank[a]++;
        parent[b]=a;
    }
}

int find(int x){return parent[x]==x?x:parent[x]=find(parent[x]);}

void kruskal(){
    sort(ed,ed+e,cmp);
    inti_union_find(v);
    res=0;
    cnt=0;
    for(int i=0;i<e;i++){
        edge tempe=ed[i];
        int p=find(tempe.u);
        int q=find(tempe.v);
        if(q!=p){
            Union(p,q);
            res+=tempe.cost;
            if(tempe.cost>=air){
                res-=tempe.cost;
                cnt++;
            }
        }
    }
}

void solve(){
    for(int i=1;i<=v;i++){
        if(find(i)==i)
            cnt++;
    }
    res+=air*cnt;
}

int main(){
    int t,casen=0;
    scanf("%d",&t);
    while(t--){
        input();
        kruskal();
        solve();
        printf("Case #%d: %d %d\n",++casen,res,cnt);
    }
    return 0;
}




UVA, 11733 (kruscal, minimum spanning tree)


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.