POJ 1287 Networking【MST模板题】

Networking


Time Limit: 1000MS


Memory Limit: 10000K


Total Submissions: 8214


Accepted: 4528

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

给你n个点,m条边,输出连接所有点的最小消耗。克里斯卡尔算法秒之。

AC代码:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct path
{
    int x,y,w;
}a[10005];
int f[100005];
int cmp(path a,path b)
{
    return a.w<b.w;
}
int find(int x)
{
    return f[x] == x ? x : (f[x] = find(f[x]));
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==0)break;
        int m;
        for(int i=1;i<=n;i++)f[i]=i;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].w);
        }
        sort(a,a+m,cmp);
        int output=0;
        for(int i=0;i<m;i++)
        {
            if(find(a[i].x)!=find(a[i].y))
            {
                merge(a[i].x,a[i].y);
                output+=a[i].w;
            }
        }
        printf("%d\n",output);
    }
}
时间: 2024-10-04 23:26:23

POJ 1287 Networking【MST模板题】的相关文章

POJ 1287 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

ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

题目链接:ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds      Memory Limit: 65536 KB 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

[2016-04-14][POJ][1287][Networking]

时间:2016-04-14 14:48:44 星期四 题目编号:[2016-04-14][POJ][1287][Networking] 题目大意:求最小生成树 分析:直接prim算法,更新边的时候,重边取最小值 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 50 + 10; const int inf = 0x3f3f3f3f; i

Sliding Window POJ - 2823 单调队列模板题

Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间最小值 就是维护一个单调递增的序列 对于样例 8 3 1 3 -1 -3 5 3 6 7 我们先模拟一遍 1.队列为空 1 进队 队列:1 2.3>队尾元素 3 进队 队列: 1 3 3.-1小于队尾元素,一直从尾部出队知道找到比-1小的元素或者队列为空 队列:-1 当队列中元素大于m的时候从队头删

POJ 1287 Networking (最小生成树)

Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1287 Appoint description:  System Crawler  (2015-06-02) Description You are assigned to design network connections between certain poin

POJ 3041 匈牙利算法模板题

一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分别是行数和列数 每有一个点 就让所处行列连一条边 求最小点覆盖 然后卡住...后来看了增林的博客... 最小点覆盖=最大匹配数 果然是模板题.. 然后wa.. 后来发现是当进行对左边点的遍历的时候 每次都要mem一次vis数组 应该是每次找之前都重新清空啊..不然下次怎么找啊...增光路对点的是否被

POJ - 1113 Wall (凸包模板题)

原题链接 模板题,直接说思路. 思路: 要求一距离凸包为 L 的图形的周长,即为 凸包周长+L为半径的圆周长 ,直接用 Graham 求一次凸包即可. 1 /* 2 * @Author: windystreet 3 * @Date: 2018-08-02 20:41:25 4 * @Last Modified by: windystreet 5 * @Last Modified time: 2018-08-02 22:30:59 6 */ 7 #include <stdio.h> 8 #inc

POJ 3041 Asteroids(二分图模板题)

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortun

POJ 1861 Network (Kruskal求MST模板题)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 14103   Accepted: 5528   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c