POJ 1287
来源:互联网 发布:汽车编程器什么好 编辑:程序博客网 时间:2024/05/03 06:37
题意是给你N个点,m条边,求最小的造价连起来,直接上模板就可以
#include<iostream>#include<cstdio>#include<vector>#include<algorithm>#define N 100struct clca{int x, y, v;clca(int xx, int yy, int vv){x = xx, y = yy, v = vv;}};using namespace std;vector<clca>eg;int a[N];int n, m,aa,bb,va;bool cmp(const clca& x1, const clca& x2){return x1.v < x2.v;}int find(int x){if (x != a[x]) a[x] = find(a[x]);return a[x];}void unio(int x1, int x2){int y1 = find(x1);int y2 = find(x2);if (y1 != y2)a[y1] = y2;}bool judge_same(int x1, int x2){if (find(x1) == find(x2))return true;return false;}int kruskal(){for (int i = 1; i <= n; i++)a[i] = i;sort(eg.begin(), eg.end(), cmp);int res = 0;for (int i = 0; i < eg.size(); i++){if (!judge_same(eg[i].x, eg[i].y)){res += eg[i].v;unio(eg[i].x, eg[i].y);}}return res;}int main(){#ifdef CDZSCfreopen("i.txt", "r",stdin);#endifwhile (~scanf("%d%d", &n, &m)){eg.clear();if (n == 0)break;while (m--){scanf("%d%d%d", &aa, &bb, &va);eg.push_back(clca(aa, bb, va));}printf("%d\n",kruskal());}return 0;}
Networking
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7531 Accepted: 4118
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
0 0
- POJ 1287
- poj 1287
- POJ 1287
- POJ 1287
- POJ 1287
- poj 1287 kruskal
- poj 1287 Networking
- POJ-1287-Networking
- poj 1287 networking
- POJ 1287 Networking
- POJ:1287 Networking
- POJ 1287 Networking
- poj 1287 MST(kruskal)
- poj 1287 Networking
- POJ-1287 Networking
- poj 1287 Networking
- POJ 1287 Networking
- poj 1287 Networking
- hdu 1075 What Are You Talking About map
- 唤醒 -- try_to_wake_up() http://blog.csdn.net/linux__kernel/article/details/1476795
- (一)jQuery概述
- 外观模式
- codeforces 358D D. Dima and Hares(dp)
- POJ 1287
- codeforce 583D Once Again... - 最长上升子序列
- 抽象类(abstract)与接口(interface)
- 对称加密(Des)
- MYSQL查询语句大全集锦
- 应用Master定理求解递归方程
- leetcode | Remove Duplicates from Sorted List
- MD5加密
- MySQL学习笔记(六)--选择合适的数据类型