POJ 1287 Networking(最小生成树)
题意 给你n个点 m条边 求最小生成树的权
这是最裸的最小生成树了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 55, M = 3000;
int par[N], n, m, ans;
struct edge{int u, v, w;} e[M];
bool cmp(edge a, edge b){return a.w < b.w;} int Find(int x)
{
int r = x, tmp;
while(par[r] >= 0) r = par[r];
while(x != r)
{
tmp = par[x];
par[x] = r;
x = tmp;
}
return r;
} void Union (int u, int v)
{
int ru = Find(u), rv = Find(v), tmp = par[ru] + par[rv];
if(par[ru] < par[rv])
par[rv] = ru, par[ru] = tmp;
else
par[ru] = rv, par[rv] = tmp;
} void kruskal()
{
memset(par, -1, sizeof(par));
int cnt = 0;
for(int i = 1; i <= m; ++i)
{
int u = e[i].u, v = e[i].v;
if(Find(u) != Find(v))
{
++cnt;
ans += e[i].w;
Union(u, v);
}
if(cnt >= n - 1) break;
}
} int main()
{
int u, v, w;
while(scanf("%d", &n), n)
{
scanf("%d", &m);
for(int i = 1; i <= m; ++i)
{
scanf("%d%d%d", &u, &v, &w);
e[i].u = u, e[i].v = v, e[i].w = w;
}
sort(e + 1, e + m + 1, cmp);
ans = 0;
kruskal();
printf("%d\n", ans);
}
return 0;
}
Networking
Time Limit: 2 Seconds Memory Limit: 65536 KB
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
POJ 1287 Networking(最小生成树)的更多相关文章
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法
题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds ...
- POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...
- POJ 1287 Networking (最小生成树)
Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...
- poj 1287 Networking【最小生成树prime】
Networking Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7321 Accepted: 3977 Descri ...
- [kuangbin带你飞]专题六 最小生成树 POJ 1287 Networking
最小生成树模板题 跑一次kruskal就可以了 /* *********************************************** Author :Sun Yuefeng Creat ...
- POJ - 1287 Networking 【最小生成树Kruskal】
Networking Description You are assigned to design network connections between certain points in a wi ...
- POJ 1287 Networking(最小生成树裸题有重边)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
随机推荐
- 使用Curl进行抓取远程内容时url中文编码问题
PHP中对于URL进行编码,可以使用 urlencode() 或者 rawurlencode(),二者的区别是前者把空格编码为 '+',而后者把空格编码为 '%20',不过应该注意的是,在编码时应该只 ...
- android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用
android中,如果使用imageButton可以在drawable 中设置一个selector,但是imageView设置不起作用,只要把Imageview的src给去掉就成了,src捕获了bac ...
- 求刷Kindle Fire HD的方法
前几天入手了台Amazon Kindle Fire HD 其系统是经过Amazon尝试改造过的Android,用起来很不爽,想刷个CM10之类的,求教程和工具.
- UVA 11054 Wine trading in Gergovia 葡萄酒交易 贪心+模拟
题意:一题街道上很多酒店,交易葡萄酒,正数为卖出葡萄酒,负数为需要葡萄酒,总需求量和总售出量是相等的,从一家店到另外一家店需要路费(路费=距离×运算量),假设每家店线性排列且相邻两店之间距离都是1,求 ...
- innerHTML与appendChild(newnodeText)的区别
innerHTML和createTextNode都可以把一段内容添加到一个节点中,区别是如果这段内容中有html标签时表现就不同了,在createTextNode中会当作文本处理,不会被浏览器解析,但 ...
- 01-编写CMS注意事项
原文:01-编写CMS注意事项 1.将ThinkPHP核心文件放在项目目录,将下载的扩展包放在在ThinkPHP目录下的Extend文件夹中 2.设置整个项目的编码为utf-8 3.创建Public公 ...
- 更好的自动ssh登录
更好的自动ssh登录 解决~/.ssh/known_hosts 过期问题. bash + expect bash:ssh.sh #!/bin/bash help(){ echo "usage ...
- 基于Chrome开源提取的界面开发框架开篇--转
初衷 一直希望VC开发者能够方便的开发出细腻高品质的用户界面.我喜欢C++,选择的平台是Windows,所以大部分时间用VC.我自身不排斥其他技术或者开发语言或者开发工具,都去了解,了解的目的是想吸取 ...
- [Python] heapq简介
[Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...
- ios 正則表達式替换
1. 不可变字符串 (content 是不可变) NSRegularExpression *regularExpression = [NSRegularExpression regularExpr ...