• Networking POJ


    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

    题意:有不超过50个网点,用光缆连接,点与点之间的距离不超过100,问在保证所有网点都连接的情况下花费最少的光缆
    把他们连接在一起,问最小花费是多少。输入样例以0结束,每个样例第一行两个数n,m,表示点数和边数,然后有m行每行三个数
    a,b,c表示a和b连接,花费的光缆为c.

    思路:最小生成树的思想,先对所有的边进行从小到大的排序,然后从小开始排查每个边,如果相连的两个点不在同一个集合中,则连接这两个点,并保留这个边。
    依此类推

    代码:
      1 #include <cstdio>
      2 #include <fstream>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <deque>
      6 #include <vector>
      7 #include <queue>
      8 #include <string>
      9 #include <cstring>
     10 #include <map>
     11 #include <stack>
     12 #include <set>
     13 #include <sstream>
     14 #include <iostream>
     15 #define mod 998244353
     16 #define eps 1e-6
     17 #define ll long long
     18 #define INF 0x3f3f3f3f
     19 using namespace std;
     20 
     21 //u表示起点,v表示终点,cost表示花费
     22 struct node
     23 {
     24     int u,v,cost;
     25 };
     26 //排序,从小到大
     27 bool cmp(node a,node b)
     28 {
     29     return a.cost<b.cost;
     30 }
     31 //存放边的信息
     32 vector<node> ve;
     33 
     34 //fa表示当前i的最远祖先
     35 //因为最多有50个点所以定义为50
     36 int fa[55];
     37 //初始化fa,开始时自己是自己的祖先
     38 void init(int qwq)
     39 {
     40     for(int i=0;i<=qwq;i++)
     41     {
     42         fa[i]=i;
     43     }
     44 }
     45 //查找最远祖先,同时进行路径压缩
     46 int find(int x)
     47 {
     48     if(fa[x]==x)
     49     {
     50         return x;
     51     }
     52     return fa[x]=find(fa[x]);
     53 }
     54 //判断最远祖先是否相同
     55 bool che(int x,int y)
     56 {
     57     return find(x)==find(y);
     58 }
     59 //合并x,y,把他们放到同一个家族中
     60 void mer(int x,int y)
     61 {
     62     if(!che(x,y)) 
     63     {
     64         fa[fa[x]]=fa[y];
     65     }
     66     return ;
     67 }
     68 //n表示点数m表示边数
     69 int n,m;
     70 int main()
     71 {
     72     //当n等于0时退出
     73     while(scanf("%d",&n)&&n!=0)
     74     {
     75         //初始化
     76         int ans=0;
     77         init(n);
     78         scanf("%d",&m);
     79         node no;
     80         for(int i=0;i<m;i++)
     81         {
     82             scanf("%d %d %d",&no.u,&no.v,&no.cost);
     83             ve.push_back(no);
     84         }
     85         //排序
     86         sort(ve.begin(),ve.end(),cmp);
     87         
     88         for(int i=0;i<ve.size();i++)
     89         {
     90             //如果这两个点不在同一集合
     91             if(!che(ve[i].u,ve[i].v))
     92             {
     93                 //合并这两个点
     94                 mer(ve[i].u,ve[i].v);
     95                 ans+=ve[i].cost;
     96             }
     97         }
     98         printf("%d
    ",ans);
     99         //清空vector容器的数据
    100         ve.clear();
    101     }
    102 }
  • 相关阅读:
    Python匿名函数详解
    python开发 -- 经常浏览的网站
    Python -- 变量指向
    我的Python开发之路---微信网页授权(扫码登陆)
    信息收集篇:玩转信息收集(一)
    TodoList:适合初学者的vue+node小项目
    节流和防抖的实现
    javascript的this问题
    bind、call、apply的区别与实现
    杜教筛
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11722521.html
Copyright © 2020-2023  润新知