uva11733 Airports

两点之间,要么建飞机场,要么建路,求怎么做花费最小。

最小生成树模板。只不过图不一定连通,做完kruskal后看下有几个连通分量,再用"飞机场"连起来,最后看之前铺的路的花费有没有比修飞机场来的高,如果费用更高,则不修路改为建飞机场。


 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define N 10010
#define M 100010
using namespace std;

int t,a ,n, m, cnt, ans;
int cost[M] ;
struct edge{
    int u, v, w;
}e[M];
struct node{
    int fa;
}nod[N];
bool cmp( edge a, edge b ) {
    return a.w < b.w;
}
bool cmp1( int a, int b ) {
    return a > b;
}
void Initial(){
    for ( int i = 0; i <= n; ++i ) {
            nod[i].fa=i;
    }
}
int find( int x )
{
    if(x!=nod[x].fa){
        nod[x].fa=find(nod[x].fa);
    }
    return nod[x].fa;
}
void Kruskal() {
    for ( int i = 0; i < m; ++i ) {
        int x = e[i].u, y = e[i].v;
        int a = find(x), b = find(y);
        if ( a != b ) {
            nod[a].fa = b;
            cost[cnt++] = e[i].w;
            ans += e[i].w;
        }
    }
}
int main()
{
    int cased = 1;
    scanf("%d", &t);
    while ( t-- ) {
        scanf("%d%d%d", &n, &m, &a);
        for ( int i = 0; i < m; ++i ){
                scanf("%d%d%d", &e[i].u, &e[i].v, &e[i].w);
        }
        ans = cnt = 0;
        Initial();
        sort(e, e+m, cmp);
        Kruskal();
        int air = 0;
        for ( int i = 1; i <= n; i++ ){
                if ( i == find(i) ) {
                        air++;
                }
        }
        ans += a*air;
        sort( cost, cost+cnt, cmp1);
        for ( int i = 0; i < cnt; ++i ) {
            if ( cost[i] >= a ) {
                    ans = ans + a - cost[i];
                    air++;
            }
        }
        printf("Case #%d: %d %d\n", cased++, ans, air);
    }
}



           


 

 

转载于:https://www.cnblogs.com/Scale-the-heights/p/4322346.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值