POJ-1287.Network(Kruskal + Prim + Prim堆优化)

Networking

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19674   Accepted: 10061

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0

Sample Output

0
17
16
26

Source

 
本题思路:最小生成树模版题,所以这就用了三种方法来练习自己的模版能力。
 
Kruskal算法参考代码:
 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int maxp = 50 + 5, maxr = 50 * 50 / 2 + 5;
 6 int p, r, ans, head[maxp], Rank[maxp];
 7 struct Edge {
 8     int u, v, w;
 9 }edge[maxr];
10 
11 void Make_Set() {
12     for(int i = 1; i <= p; i ++) {
13         head[i] = i;
14         Rank[i] = 0;
15     }
16     ans = 0;
17 }
18 
19 int Find(int u) {
20     if(u == head[u]) return u;
21     return head[u] = Find(head[u]);
22 }
23 
24 void Union(int u, int v) {
25     int fu = Find(u), fv = Find(v);
26     if(fu == fv) return;
27     if(Rank[fu] > Rank[fv])
28         head[fv] = fu;
29     else {
30         head[fu] = fv;
31         if(Rank[fu] == Rank[fv]) Rank[fv] += 1;
32     }
33 }
34 
35 bool cmp(Edge a, Edge b) {
36     return a.w < b.w;
37 }
38 
39 bool Is_same(int u, int v) {
40     return Find(u) == Find(v);
41 }
42 
43 void Kruskal() {
44     sort(edge + 1, edge + r + 1, cmp);
45     int cnt = 0;
46     Make_Set();
47     for(int i = 1; i <= r; i ++) {
48         if(!Is_same(edge[i].u, edge[i].v)) {
49             cnt ++;
50             ans += edge[i].w;
51             Union(edge[i].u, edge[i].v);
52         }
53         if(cnt == p - 1) return;
54     }
55 }
56 
57 int main () {
58     while(~scanf("%d", &p) && p) {
59         scanf("%d", &r);
60         for(int i = 1; i <= r; i ++) {
61             scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
62         }
63         Kruskal();
64         printf("%d\n", ans);
65     }
66     return 0;
67 }

 

Prim + 邻接矩阵

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxp = 50 + 5, maxr = 50 * 50 / 2 + 5, INF = 0x3f3f3f3f;
 8 int p, r, ans, dist[maxp], G[maxp][maxp];
 9 bool vis[maxp];
10 
11 void Init() {
12     ans = 0;
13     memset(vis ,false, sizeof vis);
14     for(int i = 1; i <= p; i ++) {
15         for(int j = 1; j <= p; j ++)
16             G[i][j] = INF;
17     }
18 }
19 
20 void prim(int source) {
21     dist[source] = 0;
22     vis[source] = true;//初始状态下只有source为已经安装了network的点
23     for(int i = 1; i <= p; i ++)
24         dist[i] = G[source][i];//初始化所有distance为source到他们的距离
25     for(int i = 1; i <= p; i ++) {
26         int MIN = INF, k = -1;
27         for(int j = 1; j <= p; j ++) {//每次选择那个距离子最小生成树所有结点权值最小的结点,并将其连接Network
28             if(!vis[j] && MIN > dist[j]) {
29                 k = j;
30                 MIN = dist[j];
31             }
32         }
33         if(MIN == INF) return;//没找到就说明该此时已经没有可以探索的结点了
34         ans += MIN;
35         vis[k] = true;
36         for(int j = 1; j <= p; j ++) {
37             if(!vis[j] && dist[j] > G[k][j])
38                 dist[j] = G[k][j];//对于新增的结点k,动态更新最小生成树内结点到他们结点相邻的权值,很显然意思就是每新增一个结点就看是否此时会有一条更进的边可以到达j结点
39         }
40     }
41 }
42 
43 int main () {
44     int a, b, w;
45     while(~scanf("%d", &p) && p) {
46         scanf("%d", &r);
47         Init();
48         for(int i = 1; i <= r; i ++) {
49             scanf("%d %d %d", &a, &b, &w);
50             if(w < G[a][b]) {//选择权值最小的那条边
51                 G[a][b] = G[b][a] = w;
52             }
53         }
54         prim(1);
55         printf("%d\n", ans);
56     }
57     return 0;
58 }

 

Prim + 最小堆优化 + 邻接表

这里堆是用STL优先队列实现的,我比较懒emm...(学了这么多需要堆优化的算法,结果现在连个最基本的堆都不会写,算法导论上说斐波纳挈堆优化的Prim超级快,所以打完国赛我会总结堆 + 斐波纳挈堆

还会更新出他们优化的算法)。

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

typedef pair <int, int> pii;
struct Edge {
    int to, cost;
    friend bool operator < (const Edge &a, const Edge &b) {
    return a.cost > b.cost;
    }
};
const int maxp = 50 + 5, maxr = 50 * 50 / 2 + 5, INF = 0x3f3f3f3f;
int p, r, ans, dist[maxp];
bool vis[maxp];
vector <Edge> edge[maxp];

void addedge(int u, int v, int w) {
    edge[u].push_back({v, w});
}

void Queue_Prim(int source) {
    memset(vis, false, sizeof vis);
    for(int i = 2; i <= p; i ++)    dist[i] = INF;
    dist[1] = ans = 0;
    priority_queue <Edge> Q;
    Q.push({source, dist[source]});
    while(!Q.empty()) {
        Edge now = Q.top();
        Q.pop();
        if(vis[now.to]) continue;
        vis[now.to] = true;
        ans += now.cost;
        for(unsigned int i = 0; i < edge[now.to].size(); i ++) {
            int v = edge[now.to][i].to;
            if(dist[v] > edge[now.to][i].cost) {
                dist[v] = edge[now.to][i].cost;
                Q.push({v, dist[v]});
            }
        }
    }
}

int main () {
    int a, b, c;
    while(~scanf("%d", &p) && p) {
        scanf("%d", &r);
        for(int i = 0; i < r; i ++) {
            scanf("%d %d %d", &a, &b, &c);
            addedge(a, b, c);
            addedge(b, a, c);
        }
        Queue_Prim(1);
        for(int i = 1; i <= p; i ++) edge[i].clear();
        printf("%d\n", ans);
    }
    return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2019-05-04 20:40  Cruel_King  阅读(179)  评论(0编辑  收藏  举报