首页 > 代码库 > UVA, 11733 (kruscal,最小生成树)
UVA, 11733 (kruscal,最小生成树)
Airports
From:UVA, 11733
Submit
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
|
#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,最小生成树)