zoukankan      html  css  js  c++  java
  • POJ 1287 Networking

    Networking

    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
    最小生成树。
    prim算法:
     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <algorithm>
     5 #define INF 0x3f3f3f3f
     6 using namespace std;
     7 const int N = 55;
     8 int G[N][N], vis[N], d[N];
     9 int n, m;
    10 int prim() {
    11     for(int i = 1; i <= n; i ++) vis[i] = 0, d[i] = INF;
    12     d[1] = 0;
    13     int res = 0;
    14     while(true) {
    15         int v = -1;
    16         for(int i = 1; i <= n; i ++) {
    17             if(!vis[i] && (v == -1 || d[v] > d[i])) v = i;
    18         }
    19         if(v == -1) break;
    20         vis[v] = 1;
    21         res += d[v];
    22         for(int i = 1; i <= n; i ++)
    23             d[i] = min(d[i], G[i][v]);
    24     }
    25     return res;
    26 }
    27 int main() {
    28     while(scanf("%d",&n)&&n) {
    29         scanf("%d",&m);
    30         for(int i = 1; i <= n; i ++) {
    31             for(int j = 1; j <= n; j ++) {
    32                 G[i][j] = (i==j ? 0: INF);
    33             }
    34         }
    35         for(int i = 1; i <= m; i ++) {
    36             int u, v, w;
    37             scanf("%d %d %d",&v, &u, &w);
    38             if(G[u][v] > w) {
    39                 G[u][v] = G[v][u] = w;
    40             }
    41         }
    42         // for(int i = 1; i <= n; i ++) {
    43         //     for(int j = 1; j <= n; j ++)
    44         //         printf("%d ",G[i][j]);
    45         //     printf("
    ");
    46         // }
    47         printf("%d
    ",prim());
    48     }
    49     return 0;
    50 }

    Kruskal算法:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 using namespace std;
     5 const int N = 55;
     6 struct Nod {
     7     int u, v, cost;
     8 }nod[N*N];
     9 int fa[N];
    10 bool cmp(const Nod &a, const Nod &b) {
    11     return a.cost < b.cost;
    12 }
    13 int find(int x) {
    14     return fa[x] = (fa[x] == x ? x: find(fa[x]));
    15 }
    16 int main() {
    17     int n, m;
    18     while(scanf("%d",&n)&&n) {
    19         scanf("%d",&m);
    20         int cnt = 1;
    21         for(int i = 1; i < N; i ++) fa[i] = i;
    22         for(int i = 1; i <= m; i ++) {
    23             int u, v, w;
    24             cin >> u >> v >> w;
    25             nod[cnt].u = u;
    26             nod[cnt].v = v;
    27             nod[cnt++].cost = w;
    28             nod[cnt].u = v;
    29             nod[cnt].v = u;
    30             nod[cnt++].cost = w;
    31         }
    32         sort(nod+1, nod+1+cnt, cmp);
    33         int res = 0;
    34         for(int i = 1; i < cnt; i ++) {
    35             int x = find(nod[i].u), y = find(nod[i].v);
    36             if(x != y) {
    37                 fa[x] = y;
    38                 res += nod[i].cost;
    39             }
    40         }
    41         printf("%d
    ",res);
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    最大回文子串
    求数组中的最大连续子序列和
    如何在10亿数中找出前1000大的数
    给定一个字符串,最多删除一个字符,判断能够构成回文字符串
    HashMap(数组+链表+红黑树)、HashTable、TreeMap
    【转】jmeter如何设置登录接口只调用一次以及遇到的问题:cookie参数放在消息头headers里面
    Kafka命令行操作
    git上无法push代码解决办法
    【转】Jenkins集成Docker镜像实现自动发布
    awk命令总结
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/7275782.html
Copyright © 2011-2022 走看看