POJ 1287 Networking
来源:互联网 发布:js tr td 编辑:程序博客网 时间:2024/06/10 20:05
Description:
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.
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.
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
0171626
题目大意:
给定某两点的无项边权关系, 求由这些点构成的最小生成树的权值是多少。
解题思路:
裸最小生成树模板, 这里用并查集实现最小生成树比较容易,没有太大的坑点。
代码:
#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
using namespace std;
/*tools:
*ios::sync_with_stdio(false);
*freopen("input.txt", "r", stdin);
*/
typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = 1000000;
const int Max = (int) 1e6;
struct node {
int l, r;
int w;
}arr[Max];
int Fa[Max], n, m;
void Init() {
for (int i = 1; i <= n; ++i) {
Fa[i] = i;
arr[i].w = inf;
}
}
bool cmp(node a, node b) {
return a.w < b.w;
}
int Pa(int x) {
return (x == Fa[x]) ? x : Pa(Fa[x]);
}
void Merge(int x, int y) {
int fx = Pa(x);
int fy = Pa(y);
Fa[fx] = fy;
}
int main() {
//freopen("input.txt", "r", stdin);
while (scanf("%d", &n) && n) {
Init();
scanf("%d", &m);
for (int i = 0; i < m; ++i) {
int l, r, w;
scanf("%d %d %d", &l, &r, &w);
arr[i].l = l;
arr[i].r = r;
arr[i].w = w;
}
sort(arr, arr + m, cmp);
int rst = n, ans = 0;
for (int i = 0; i < m && rst > 1; ++i) {
if (Pa(arr[i].l) != Pa(arr[i].r)) {
Merge(arr[i].l, arr[i].r);
ans += arr[i].w;
rst--;
}
}
printf("%d\n", ans);
}
return 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
- 浅谈摩尔定律
- 学习uboot前奏之hardware-clock[s3c2440]
- Unity Shader 学习笔记(13) 混合光源、光的衰减
- 伪随机生成之线性同余算法
- [Python高效编程]
- POJ 1287 Networking
- 【JavaScript】如何获取调用该函数的源对象(元素)
- Python 练习实例15
- zookeeper官方下载与启动
- unity游戏引擎基础之编辑器菜单栏扩展(三)
- Nginx 学习笔记(1)
- IntelliJ Idea 常用快捷键列表
- centos安装jdk9
- [LC]121. Best Time to Buy and Sell Stock