题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1233

题目描述

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。

思路

最小生成树裸题,这里提供Prim算法和Kruskal算法。

AC代码(Prim $O(V^2)$)

/*************************************************************************
    > File Name: 1233.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: 2015-08-15 Sat 15:03:47 CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;
const int MAX_V = 100;
const int INF = 0x3f3f3f3f;
int V;
int cost[MAX_V][MAX_V];
int mincost[MAX_V];
bool used[MAX_V];

int prim() {
    int res = 0;
    memset(used, 0, sizeof(used));
    memset(mincost, 0x3f, sizeof(mincost));
    mincost[1] = 0;
    while(true) {
        int v = -1;
        for(int u=1; u<=V; ++u)
            if(!used[u] && ( v==-1 || mincost[u] < mincost[v])) v = u;
        if(v == -1) break;
        used[v] = true;
        res += mincost[v];
        for(int u=1; u<=V; ++u)
            mincost[u] = min(mincost[u], cost[v][u]);
    }
    return res;
}

void solve() {
    cin.sync_with_stdio(false);
    while(cin >> V && V)  {
        int from, to, dist;
        memset(cost, 0x3f, sizeof(cost));
        for(int i=V*(V-1)/2; i>0; --i) {
            cin >> from >> to >> dist;
            cost[from][to] = cost[to][from] = dist;
        }
        printf("%d\n", prim());
    }

}

int main()
{
#ifdef Oj
    freopen("1233.in", "r", stdin);
#endif
    solve();
    return 0;
}

AC代码(Kruskal $O(E log(V))$)

/*************************************************************************
    > File Name: 1233_2.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: 2015-08-15 Sat 15:29:31 CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;
const int MAX_V = 105;
int V, E;
int par[MAX_V];
struct edge {
    int u, v, cost;
    bool operator<(const edge &b) const {
        return cost < b.cost;
    }
} es[MAX_V*(MAX_V-1)/2];

void init_union_find(int N) {
    for(int i=1; i<=N; ++i)
        par[i] = i;
}

int find(int x) {
    return x==par[x]?x:find(par[x]);
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x != y)
        par[x] = y;
}

int kruskal() {
    sort(es, es+E);
    init_union_find(V);
    int res = 0;
    for(int i=0; i<E; ++i) {
        edge e = es[i];
        if(find(e.u) != find(e.v)) {
            unite(e.u, e.v);
            res+=e.cost;
        }
    }
    return res;
}


void solve() {
    cin.sync_with_stdio(false);
    while(cin >> V && V) {
        E = V*(V-1)/2;

        int from, to, cost;
        for(int i=0; i<E; ++i) {
            cin >> from >> to >>cost;
            es[i].u = from;
            es[i].v = to;
            es[i].cost = cost;
        }
        printf("%d\n", kruskal());
    }

}

int main()
{
#ifdef Oj
    freopen("1233.in", "r", stdin);
#endif
    solve();
    return 0;
}