POJ 1287 Networking【kruskal模板题】
传送门:http://poj.org/problem?id=1287
题意:给出n个点 m条边 ,求最小生成树的权
思路:最小生树的模板题,直接跑一遍kruskal即可
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 5005;
struct node
{
int u;
int v;
int w;
bool operator< (const node &a)
{
return w < a.w;
}
} edges[maxn];
int f[maxn];
int find(int x)
{
return f[x] = f[x] == x ? x : find(f[x]);
}
int kruskal(int n, int m)
{
for(int i = 1; i <= n; i++)
f[i] = i;
int cnt = 0;
int ans = 0;
for(int i = 1; i <= m; i++)
{
int u = edges[i].u;
int v = edges[i].v;
int w = edges[i].w;
int t1 = find(u);
int t2 = find(v);
if(t1 != t2)
{
ans += w;
f[t1] = t2;
cnt++;
}
if(cnt == n - 1)
break;
}
if(cnt < n - 1)
return -1;
else
return ans;
}
int main()
{
int n;
while(scanf("%d", &n), n)
{
int m;
scanf("%d", &m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &edges[i].u, &edges[i].v, &edges[i].w);
}
sort(edges + 1, edges + m + 1);
int ans = kruskal(n, m);
cout << ans << endl;
}
}
POJ 1287 Networking【kruskal模板题】的更多相关文章
- 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 ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- POJ 1287 Networking (最小生成树)
Networking Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Submit S ...
- 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 1258 + POJ 1287 【最小生成树裸题/矩阵建图】
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
随机推荐
- Vulkan SDK之Vertex Buffer
A vertex buffer is a CPU-visible and GPU-visible buffer that contains the vertex data that describes ...
- [题解] LuoguP4827 [国家集训队] Crash 的文明世界
传送门 这个题......我谔谔 首先可以考虑换根\(dp\),但到后来发现二项式定理展开过后需要维护\(k\)个值,同时每个值也要\(O(k)\)的时间按二项式定理算 当然fft优化过后就是k lo ...
- springboot - 返回JSON error 从自定义的 ErrorController
使用AbstractErrorController(是ErrorController的实现),返回json error. 1.概览 2.基于<springboot - 映射 /error 到自定 ...
- ERROR in Cannot find module 'node-sass'
windows下,通过淘宝的npm镜像安装 npm install node-sass --registry=https://registry.npm.taobao.org (之前安装好过,一段时间没 ...
- [NCTF2019]Fake XML cookbook
0x00 知识点 XXE攻击 附上链接: https://xz.aliyun.com/t/6887 XXE(XML External Entity Injection)全称为XML外部实体注入 XML ...
- Linux-课后练习(第二章命令)20200217-2
- GitHub Token for composer
a2248520cdd2b1d27c2c70741003b9078530d81c
- Swift 3 :基于 AVAudioPlayer 的简单音乐播放器
2017.05.22 17:46* 字数 1585 阅读 5095评论 0喜欢 8赞赏 2 https://www.jianshu.com/p/4d5c257428a1 学习ios以来差不多接近两个月 ...
- 简单的说一下react路由(逆战班)
现代前端大多数都是SPA(单页面程序),也就是只有一个HTML页面的应用程序,因为它的用户体验更好,对服务器压力更小,所以更受欢迎,为了有效的使用单个页面来管理原来多页面的功能,前端路由应运而生. 前 ...
- 计划任务常用在线工具-微服务信息整-seafile网盘-亿图操作-正则工具
正则工具 https://regex101.com/ http://www.regexp.cn/Regex 身份证匹配 ^(\\d{}|\d{})(\\d|[xX])$ \d{}[-9Xx]|\d{} ...