博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1287 Networking
阅读量:4944 次
发布时间:2019-06-11

本文共 3178 字,大约阅读时间需要 10 分钟。

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6803   Accepted: 3705

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 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120

Sample Output

0171626 最小生成树 CODE:
#include 
#include
#include
#include
#define REP(i, s, n) for(int i = s; i <= n; i ++)#define REP_(i, s, n) for(int i = n; i >= s; i --)#define MAX_N 50 + 10#define MAX_M 10000 + 10using namespace std;int n, m, u, v, w;struct node{ int u, v, w;}E[MAX_M];int F[MAX_N];int find(int x){ if(x == F[x]) return x; return F[x] = find(F[x]);}void Union(int x,int y){ x = find(x); y = find(y); if(x == y) return; F[y] = x;}bool cmp(node a, node b){ return a.w < b.w;} int main(){ while(scanf("%d%d", &n, &m) != EOF){ if(n == 0) break; if(n == 1 || m == 0) {printf("0\n"); continue;} REP(i, 1, m) scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w); sort(E + 1, E + m + 1, cmp); REP(i, 1, n) F[i] = i; int ans = 0; REP(i, 1, m){ if(find(E[i].u) != find(E[i].v)){ Union(E[i].u, E[i].v); ans += E[i].w; } } printf("%d\n", ans); } return 0;}

 

 

转载于:https://www.cnblogs.com/ALXPCUN/p/4567149.html

你可能感兴趣的文章
webstorm使用说明
查看>>
项目练习计划
查看>>
Xshell远程登录
查看>>
@RequestParam与@PathVariable的区别
查看>>
C语言之break和continue
查看>>
jquery.form.js使用
查看>>
LINQ to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”。
查看>>
回顾2012,展望2013
查看>>
Spring中的ApplicationContextAware使用
查看>>
HDU-2067-小兔的棋盘
查看>>
监听手机录音
查看>>
hadoop的WordCount样例
查看>>
客户化程序完成标准成本成批更新
查看>>
JZOJ 1286. 太空电梯
查看>>
大数据平台组件布置 与 进程查看
查看>>
Hadoop3集群搭建之——hive添加自定义函数UDTF (一行输入,多行输出)
查看>>
【转】去除inline-block元素的间隙
查看>>
JS - Math对象
查看>>
MUI开发指南(二) webview对象
查看>>
HTML5按键打开摄像头和拍照
查看>>