POJ-1287 Networking
来源:互联网 发布:微缩景观模型淘宝 编辑:程序博客网 时间:2024/05/16 11:00
Networking
Time Limit: 1000MS Memory Limit: 10000K
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.
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.
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————————————————————集训8.5的分割线————————————————————
思路:Ctrl+C Ctrl+V (POJ-1861 Network)Then revise
代码如下:
/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <iostream>using namespace std;/****************************************/const int N = 1005, M = 15005;int n, m, sum, fa[N];struct Node{int x, y, dis;}edge[M];bool cmp(Node a, Node b){return a.dis < b.dis;}int Find(int x){if(x != fa[x]) fa[x] = Find(fa[x]);return fa[x];}void Union(int x, int y, int d){int fx = Find(x), fy = Find(y);if(fx != fy) {sum += d;fa[fy] = fx;}}int main(){#ifdef J_Surefreopen("1287.in", "r", stdin);//freopen(".out", "w", stdout);#endifwhile(scanf("%d%d", &n, &m)!=EOF&&n) {for(int i = 0; i < m; i++) {scanf("%d%d%d", &edge[i].x, &edge[i].y, &edge[i].dis);}sort(edge, edge+m, cmp);sum = 0;for(int i = 1; i <= n; i++)fa[i] = i;for(int i = 0; i < m; i++) {Union(edge[i].x, edge[i].y, edge[i].dis);}printf("%d\n", sum);}return 0;}
0 0
- poj 1287 Networking
- POJ-1287-Networking
- poj 1287 networking
- POJ 1287 Networking
- POJ:1287 Networking
- POJ 1287 Networking
- poj 1287 Networking
- POJ-1287 Networking
- poj 1287 Networking
- POJ 1287 Networking
- poj 1287 Networking
- poj 1287 networking kruskal
- POJ 1287 Networking
- poj 1287 Networking
- POJ 1287 Networking
- POJ 1287 Networking
- Poj 1287 Networking
- POJ 1287 Networking
- 【MFC】查找指定路径下的指格式,并放到vector中
- hdu 3724 字典树
- jsp 传值jsp 数据库 乱码解决
- JavaScript学习 jquery学习6 替换,删除,复制
- UIPickerView中插入图片
- POJ-1287 Networking
- Hbase 的几点学习体会
- yugowu微信营销是什么
- apache kafka系列之性能优化架构分析
- oracle rownum 使用详解
- 嵌入式系统的结构
- IF OBJECT_ID('UP_GET_XXX_SEQNO') IS NOT NULL
- 显示系统中所有的定时任务
- ELF重定向对象文件 by wangdb