POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)
题目链接:
PKU:http://poj.org/problem?id=1861
ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=542
Description
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 maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one
because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Input
possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot
be connected to itself. There will always be at least one way to connect all hubs.
Output
cable. Separate numbers by spaces and/or line breaks.
Sample Input
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
Sample Output
1
4
1 2
1 3
2 3
3 4
Source
题意:
有n个顶点,m条边,每条边都是双向的,而且有一定的长度。要求使每一个顶点都连通,而且要使总长度最短,
输出最大边、边的总数和所选择的边。
PS:
貌似题目的案例有点问题,卡了好久!
应该输出的是:
1
3
1 3
2 3
2 4
代码例如以下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 15017;
int father[maxn];
struct edge
{
int x,y,v;
};
struct edge ed[maxn],ansa[maxn]; bool cmp(edge a,edge b)
{
return a.v<b.v;
} int find(int x)
{
if(x==father[x])
return x;
return father[x]=find(father[x]);
} void Krusal(int n,int m)
{
int i,fx,fy,cnt;
int ans=0;
for(i = 1; i <= n; i++)
father[i]=i;
sort(ed,ed+m,cmp);//对边的排序
cnt=0;
int max=-1;
for(i=0; i<m; i++)
{
fx=find(ed[i].x);
fy=find(ed[i].y);
if(fx!=fy)
{
ans+=ed[i].v;
father[fx]=fy;
ansa[cnt].x=ed[i].x;
ansa[cnt++].y=ed[i].y;
if(max<ed[i].v)
max=ed[i].v;
}
}
printf("%d\n%d\n",max,cnt);
for(i=0; i<cnt; i++)
printf("%d %d\n",ansa[i].x,ansa[i].y);
} int main()
{
int t;
int n, m;
int a, b, k; while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i = 0; i < m; i++)
{
scanf("%d %d %d",&a,&b,&k);
ed[i].x=a,ed[i].y=b,ed[i].v=k;
}
Krusal(n,m);
}
return 0;
}
POJ 1861 & ZOJ 1542 Network(最小生成树之Krusal)的更多相关文章
- ZOJ 1542 POJ 1861 Network 网络 最小生成树,求最长边,Kruskal算法
题目连接:problemId=542" target="_blank">ZOJ 1542 POJ 1861 Network 网络 Network Time Limi ...
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- POJ 1861 Network (Kruskal算法+输出的最小生成树里最长的边==最后加入生成树的边权 *【模板】)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14021 Accepted: 5484 Specia ...
- POJ 1861:Network(最小生成树&&kruskal)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 13266 Accepted: 5123 Specia ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Subm ...
- POJ 1861 Network (Kruskal求MST模板题)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14103 Accepted: 5528 Specia ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: Accepted: Special Judge Descripti ...
- POJ 2349 Arctic Network(最小生成树+求第k大边)
题目链接:http://poj.org/problem?id=2349 题目大意:有n个前哨,和s个卫星通讯装置,任何两个装了卫星通讯装置的前哨都可以通过卫星进行通信,而不管他们的位置. 否则,只有两 ...
随机推荐
- Ubuntu 10.10安装和使用pythonbrew简介
http://blog.csdn.net/nuccch/article/details/17595161 一.安装Pythonbrew 通过pythonbrew工具实现多版本Python管理,首先安装 ...
- 火车票抢票API 根据乘客的车次与座席要求快速订票出票
火车票抢票API 根据乘客的车次与座席要求快速订票出票:https://www.juhe.cn/docs/api/id/257 1.站站查询 接口地址:http://v.juhe.cn/grabTic ...
- measureChildren的工作原理
无论是在重写View还是ViewGroup的时候,尤其是ViewGrop的时候,往往不可避免的重写onMeasure方法,我们一定会调用setMeasuredDimension()将测量好的宽高值传递 ...
- (转)JavaScript: in, hasOwnProperty, delete, for/in
in 运算符 判断对象是否拥有某一属性只要对象拥有该属性,就会返回true,否则false var point = { x:1, y:1 };alert( 'x' in point ); //tru ...
- 基于python实现的DDoS
目录 一个简单的网络僵尸程序 一个简单的DOS攻击程序 整合网络僵尸和DoS攻击--DDoS 代码地址如下:http://www.demodashi.com/demo/12002.html 本例子包含 ...
- Spark on Yarn 集群运行要点
实验版本:spark-1.6.0-bin-hadoop2.6 本次实验主要是想在已有的Hadoop集群上使用Spark,无需过多配置 1.下载&解压到一台使用spark的机器上即可 2.修改配 ...
- genymotion启动黑屏的原因及临时解决方法
解决办法 好像是要激活网络,具体原因不知道!!! sudo ifconfig vboxnet0 up 下面好像更好使一点 sudo ip link set dev vboxnet0 up
- 【精】iOS GCD 具体解释
一.介绍 1.什么是GCD? Grand Central Dispatch.是苹果公司开发的一套多核编程的底层API. GCD首次公布在Mac OS X 10.6,iOS4及以上也可用.GCD存在于l ...
- springboot学习(七) 使用JdbcTemplate
1.对内嵌数据库的支持 内嵌数据库通常用于开发和测试环境,不推荐用于生产环境.Spring Boot提供自动配置的嵌入式数据库有H2.HSQL.Derby,你不需要提供任何连接配置就能使用. 例子: ...
- java线程不安全类与写法
线程不安全类 1.为什么java里要同时提供stringbuilder和stringbuffer两种字符串拼接类 2.simpledateformate是线程不安全的类,如果把它作为全局变量会有线程安 ...