题目链接:

https://vjudge.net/problem/POJ-1287

题目大意:

模板

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + ;
const int INF = << ;
int dir[][] = {,,,,-,,,-};
int T, n, m, x;
struct edge
{
int u, v, w;
bool operator <(const edge& a)const
{
return w < a.w;
}
};
edge a[maxn];
int par[], high[];
//初始化n个元素
void init(int n)
{
for(int i = ; i < n; i++)
{
par[i] = i;
high[i] = ;
}
}
//查询树的根
int Find(int x)
{
return par[x] == x ? x : par[x] = Find(par[x]);//路径压缩
}
void unite(int x, int y)
{
x = Find(x);
y = Find(y);
if(x == y)return;
if(high[x] < high[y])par[x] = y;//y的高度高,将x的父节点设置成y
else
{
par[y] = x;
if(high[x] == high[y])high[x]++;
}
}
bool same(int x, int y)
{
return Find(x) == Find(y);
}
void kruskal(int n, int m)//点数n,边数m
{
int sum_mst = ;//mst权值
int num= ;//已经选择的边的边数
sort(a, a + m);//边进行排序
init(n);//初始化并查集
for(int i = ; i < m; i++)
{
int u = a[i].u;
int v = a[i].v;
if(Find(u - ) != Find(v - ))//图最开始的下标是1,并查集是0
{
//printf("%d %d %d\n", u, v, a[i].w);
sum_mst += a[i].w;
num++;
unite(u - , v - );
}
if(num >= n - )break;
}
//printf("weight of mst is %d\n", sum_mst);
cout<<sum_mst<<endl;
}
int main()
{
while(cin >> n && n)
{
cin >> m;
for(int i = ; i < m; i++)
{
cin >> a[i].u >> a[i].v >> a[i].w;
}
kruskal(n, m);
}
return ;
}

POJ-1287 Networking---裸的不能再裸的MST的更多相关文章

  1. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  2. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  3. POJ 1287 Networking (最小生成树)

    Networking Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit S ...

  4. POJ 1287 Networking(最小生成树裸题有重边)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  5. POJ 1287 Networking (最小生成树)

    Networking 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/B Description You are assigned ...

  6. POJ 1287 Networking(最小生成树)

    题意  给你n个点 m条边  求最小生成树的权 这是最裸的最小生成树了 #include<cstdio> #include<cstring> #include<algor ...

  7. POJ 1287 Networking

    题目链接: poj.org/problem?id=1287 题目大意: 你被分派到去设计一个区域的连接点,给出你每个点对之间的路线,你需要算出连接所有点路线的总长度. 题目输入: 一个数字n  代表有 ...

  8. POJ 1287 Networking (ZOJ 1372) MST

    http://poj.org/problem?id=1287 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=372 和上次那题差 ...

  9. POJ 1287 Networking【kruskal模板题】

    传送门:http://poj.org/problem?id=1287 题意:给出n个点 m条边 ,求最小生成树的权 思路:最小生树的模板题,直接跑一遍kruskal即可 代码: #include< ...

  10. poj 1287 Networking【最小生成树prime】

    Networking Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7321   Accepted: 3977 Descri ...

随机推荐

  1. c++编码及读写文件

    写文件 #include <fstream> #include <iostream> using namespace std; int main() { ofstream ou ...

  2. 第六周PTA作业

    第一题: #include<stdio.h> int main () { float a,b; scanf("%f %f\n",&a,&b); floa ...

  3. alpha-咸鱼冲刺day9-紫仪

    总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图   三,项目进展 大概差不多了.不过提交似乎又出了问题正在修改ing 四,问题困难 页面整合啥的凑合一下.因为后面跟着学长速成的PH ...

  4. C语言博客作业--数组

    一.PTA实验作业 题目1.求整数序列中出现次数最多的数 1.本题PTA提交列表 2.设计思路 定义整形变量n,max,count分别表示整数个数,出现次数最大值,出现次数.定义循环变量i,j. 输入 ...

  5. 2017 清北济南考前刷题Day 3 afternoon

    期望得分:100+40+100=240 实际得分:100+40+100=240 将每个联通块的贡献乘起来就是答案 如果一个联通块的边数>点数 ,那么无解 如果边数=点数,那么贡献是 2 如果边数 ...

  6. maven(二)创建工程

    创建动态Web工程打war包 ​ File→new→Maven Project→勾上create a simple project→然后next> ​ 然后会报一下的错 ​ 解决 ​ 创建jav ...

  7. Junit 4 测试中使用定时任务操作

    难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...

  8. NFC驱动调试

    1.NFC基本概念: NFC 又称为近场通信,是一种新兴技术,可以在彼此靠近的情况下进行数据交换,是由非接触式射频识别(RFID) 及互连互通技术整合演变而来,通过单一芯片集成感应式读卡器: NFC有 ...

  9. 《javascript设计模式与开发实践》阅读笔记(15)—— 装饰者模式

    装饰者模式 可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.在程序开发中,许多时候都并不希望某个类天生就非常庞大,一次性包含许多职责.那么我们就可以使用装饰者模式. 代码例 ...

  10. 【编程开发】PHP---面向对象

    面向对象编程 类:在现实世界中,任何事物都有种类的概念:车 类是由特征和行为构成的. 特征:都是不动的,从出厂的时候就已经内置好了(属性) 行为:一种动的状态.(方法(函数)) 行为依赖于这些特征,而 ...