Hub Connection plan

Time Limit:1000MS Memory Limit:65536KB
Total Submit:743 Accepted:180

Description

Partychen is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the cost is minimal. partychen will provide you all necessary information about possible hub connections. You are to help partychen to find the way to connect hubs so that all above conditions are satisfied.

Input

The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable cost required to connect them. cost is a positive integer number that does not exceed 106. There will always be at least one way to connect all hubs.

Output

Output the minimize cost of your hub connection plan.

Sample Input

4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1

Sample Output

3
Hint
We can build net from 1 to 2 to 3 to 4,then we get the cost is 3.Of course you can get 3 by other way.

Source

解题:最小生成树模板

 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc{
int u,v,w;
bool operator<(const arc &t)const{
return w < t.w;
}
}e[maxn];
int uf[maxn],n,m;
int Find(int x){
if(x != uf[x]) uf[x] = Find(uf[x]);
return uf[x];
}
int main(){
while(~scanf("%d %d",&n,&m)){
for(int i = ; i < m; ++i)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e,e+m);
int ret = ;
for(int i = ; i <= n; ++i) uf[i] = i;
for(int i = ; i < m; ++i){
int x = Find(e[i].u);
int y = Find(e[i].v);
if(x == y) continue;
ret += e[i].w;
uf[x] = y;
}
printf("%d\n",ret);
}
return ;
}

ECNUOJ 2573 Hub Connection plan的更多相关文章

  1. Network 分类: POJ 图论 2015-07-27 17:18 17人阅读 评论(0) 收藏

    Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14721 Accepted: 5777 Special Judg ...

  2. URAL 1160 Network(最小生成树)

    Network Time limit: 1.0 secondMemory limit: 64 MB Andrew is working as system administrator and is p ...

  3. Network()

    Network Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) Total Submi ...

  4. POJ 1861 Network (模版kruskal算法)

    Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...

  5. ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法

    题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...

  6. POJ-1861-NETWORK 解题报告

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16628   Accepted: 6597   Specia ...

  7. POJ 1861:Network(最小生成树&amp;&amp;kruskal)

    Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13266   Accepted: 5123   Specia ...

  8. poj1681 Network

    题目链接 https://cn.vjudge.net/problem/17712/origin Andrew is working as system administrator and is pla ...

  9. OJ题解记录计划

    容错声明: ①题目选自https://acm.ecnu.edu.cn/,不再检查题目删改情况 ②所有代码仅代表个人AC提交,不保证解法无误 E0001  A+B Problem First AC: 2 ...

随机推荐

  1. LeetCode hard 668. Kth Smallest Number in Multiplication Table(二分答案,一次过了,好开心,哈哈哈哈)

    题目:https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/description/ 668. Kth S ...

  2. http扩展请求头中的x-Forwarded-For

    X-Forwarded-For格式: X-Forwarded-For: client-ip, proxy1-ip, proxy2-ip 客户端请求服务器的过程中没经过一个代理层(代理中使用了xff)那 ...

  3. CDQ分治笔记

    以前一直不会CDQ……然后经常听到dalao们说“这题直接CDQ啊”“CDQ不就秒了吗”的时候我只能瑟瑟发抖QAQ CDQ分治 其实CDQ分治就是二分分治,每次将$[l,r]$的问题划分为$[l,mi ...

  4. Manacher 求最长回文子串算法

    Manacher算法,是由一个叫Manacher的人在1975年发明的,可以在$O(n)$的时间复杂度里求出一个字符串中的最长回文子串. 例如这两个回文串“level”.“noon”,Manacher ...

  5. 计数排序(counting-sort)

    计数排序是一种稳定的排序算法,它不是比较排序.计数排序是有条件限制的:排序的数必须是n个0到k的数,所以计数排序不适合给字母排序.计数排序时间复杂度:O(n+k),空间复杂度:O(k),当k=n时,时 ...

  6. C#-CLR各版本特点

    来自为知笔记(Wiz) 附件列表 CLR与NET版本关系.png NET框架工作流程.png VS与CLR关系.png

  7. Unix发展史

    简述 了解过去,我们才能知其然,更知所以然.总结过去,我们才会知道明天该何去何从.在时间的滚轮中,许许多多的东西就像流星一样一闪而逝,而有些东西却能经受着时间的考验散发着经久的魅力,让人津津乐道.流传 ...

  8. asp.net 缓存公共类

    using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...

  9. unity3D常见问题

    总结自己在学习中遇到的问题. 记录问题,帮助他人,有什么不正确的地方欢迎指正 没有发生碰撞 两个物体(Plane和Cube)都加入了collider,当中一个加入了rigidbody,应该会产生碰撞, ...

  10. Python: PS 图像调整--颜色梯度

    本文用 Python 实现 PS 中的色彩图,可以看到颜色的各种渐变,具体的效果可以参考以前的博客: http://blog.csdn.net/matrix_space/article/details ...