Networking

题目链接:

http://acm.hust.edu.cn/vjudge/contest/124434#problem/B

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

##题意:

给出每个字母与哪些字母相连及花费,求一个最小花费的边集使得所有字母联通.


##题解:

裸的最小生成树,读入数据的时候稍微小心一些.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 11000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

struct node{

int left,right,cost;

}road[maxn];

int cmp(node x,node y) {return x.cost<y.cost;}

int p[maxn],m,n;

int find(int x) {return p[x]=(p[x]==x? x:find(p[x]));}

int kruskal()

{

int ans=0;

for(int i=1;i<=n;i++) p[i]=i;

sort(road+1,road+m+1,cmp);

for(int i=1;i<=m;i++)

{

int x=find(road[i].left);

int y=find(road[i].right);

if(x!=y)

{

ans+=road[i].cost;

p[x]=y;

}

}

return ans;

}

int main(int argc, char const *argv[])

{

//IN;

while(scanf("%d", &n) != EOF && n)
{
cin >> m;
memset(road,0,sizeof(road)); for(int i=1; i<=m; i++)
{
scanf("%d %d %d", &road[i].left, &road[i].right, &road[i].cost);
} int ans=kruskal(); printf("%d\n", ans);
} return 0;

}

POJ 1287 Networking (最小生成树)的更多相关文章

  1. POJ 1287 Networking (最小生成树)

    Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S ...

  2. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  3. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  4. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  5. POJ 1287 Networking(最小生成树)

    题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...

  6. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

  7. [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking

    最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...

  8. POJ - 1287 Networking 【最小生成树Kruskal】

    Networking Description You are assigned to design network connections between certain points in a wi ...

  9. POJ 1287 Networking(最小生成树裸题有重边)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

随机推荐

  1. Linux3.4内核的基本配置和编译

    转载自:http://www.embedu.org/Column/Column634.htm 作者:李昕,华清远见研发中心讲师. 了解Linux3.4内核的特性及新增功能,掌握Linux内核的编译过程 ...

  2. ProgressBar 示例及自定义样式

    在layout中使用ProgerssBar,其中使用了自定义的样式 <ProgressBar android:id="@+id/footer_refresh_prgs" st ...

  3. new int[]和new int()的区别

    1. new int[] 是创建一个int型数组,数组大小是在[]中指定,例如:int * p = new int[10]; //p执行一个长度为10的int数组.2. new int()是创建一个i ...

  4. mac更新node

    今天在用 yeoman 的时候,提示对 npm 和 node 的版本有要求,为了决绝以后遇到的一些类似的问题,我决定定期对 node 和 npm 进行更新. npm的更新: $ sudo npm in ...

  5. bzoj2788

    明显是一个差分约束系统 对于第一种限制,其实就是x[a]+1<=x[b] x[b]-1<=x[a] 根据三角不等式很容易建图 但这题他比较奇怪,问的是X最多不同取值的个数 根据这张图的特殊 ...

  6. UVa 1328 (KMP求字符串周期) Period

    当初学KMP的时候也做过这道题,现在看来还是刘汝佳的代码要精简一些,毕竟代码越短越好记,越不容易出错. 而且KMP的递推失配函数的代码风格和后面的Aho-Corasick自动机求失配函数的代码风格也是 ...

  7. UVa 11174 (乘法逆元) Stand in a Line

    题意: 有n个人排队,要求每个人不能排在自己父亲的前面(如果有的话),求所有的排队方案数模1e9+7的值. 分析: <训练指南>上分析得挺清楚的,把公式贴一下吧: 设f(i)为以i为根节点 ...

  8. UVa 10003 (可用四边形不等式优化) Cutting Sticks

    题意: 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用. 分析: d(i, j)表示切割第i个切点到第j个切点这段所需的最小费用.则有d(i, j) = ...

  9. Oracle 跟踪事件 set event

    一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...

  10. Android设计模式源码解析之桥接模式

    模式介绍 模式的定义 将抽象部分与实现部分分离,使它们都可以独立的变化. 模式的使用场景 如果一个系统需要在构件的抽象化角色和具体化角色之间添加更多的灵活性,避免在两个层次之间建立静态的联系. 设计要 ...