TOJ 2815 Connect them (kruskal+并查集)
描述
You have n computers numbered from 1 to n and you want to connect them to make a small local area network (LAN). All connections are two-way (that is connecting computers i and j is the same as connecting computers j and i). The cost of connecting computer i and computer j is cij. You cannot connect some pairs of computers due to some particular reasons. You want to connect them so that every computer connects to any other one directly or indirectly and you also want to pay as little as possible.
Given n and each cij , find the cheapest way to connect computers.
输入
There are multiple test cases. The first line of input contains an integer T (T <= 100), indicating the number of test cases. Then T test cases follow.
The first line of each test case contains an integer n (1 < n <= 100). Then n lines follow, each of which contains n integers separated by a space. The j-th integer of the i-th line in these n lines is cij, indicating the cost of connecting computers i and j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii = 0, 1 <= i, j <= n.
输出
For each test case, if you can connect the computers together, output the method in in the following fomat:
i1 j1 i1 j1 ......
where ik ik (k >= 1) are the identification numbers of the two computers to be connected. All the integers must be separated by a space and there must be no extra space at the end of the line. If there are multiple solutions, output the lexicographically smallest one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.
样例输入
2
3
0 2 3
2 0 5
3 5 0
2
0 0
0 0
样例输出
1 2 1 3
-1
提示
Another solution B different from A is a line of q integers: b1, b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= p, r <= q) such that ai = bi for all 0 < i < r and ar < br
OR
(2) p < q and ai = bi for all 0 < i <= p
题目来源
主要是选取权值最小的边构成树,kruskal+并查集
选完之后保存起来,还需要二次字典序排。
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN=; int N,cnt;
int lis[MAXN];
struct node{
int u,v,w;
}edge[MAXN*];
struct r{
int a,b;
}ans[MAXN]; int find(int x){
int temp=lis[x];
while(temp!=lis[temp]){
temp=lis[temp];
}
return temp;
}
void merge(int x, int y){
lis[x]=y;
} bool cmp1(node a, node b){
if(a.w!=b.w)return a.w < b.w;
return a.u < b.u;
}
bool cmp2(r a, r b){
if(a.a!=b.a)return a.a < b.a;
return a.b < b.b;
} int kruskal(){
int flag=;
sort(edge,edge+cnt,cmp1);
for(int i=; i<cnt; i++){
int x=find(edge[i].u);
int y=find(edge[i].v);
if(x!=y){
merge(x,y);
ans[flag].a=edge[i].u;
ans[flag].b=edge[i].v;
flag++;
}
}
return flag;
}
int main(int argc, char *argv[])
{
int T,v;
scanf("%d",&T);
while(T--){
cnt=;
scanf("%d",&N);
for(int i=; i<=N; i++){
lis[i]=i;
}
for(int i=; i<=N; i++){
for(int j=; j<=N; j++){
scanf("%d",&v);
if(i<j && v>){
edge[cnt].u=i;
edge[cnt].v=j;
edge[cnt].w=v;
cnt++;
}
}
}
int f=kruskal();
if(f!=N-){
printf("-1\n");
}else{
int flag=;
sort(ans,ans+f,cmp2);
for(int i=; i<f; i++){
if(flag)printf(" ");
printf("%d %d",ans[i].a,ans[i].b);
flag=;
}
printf("\n");
}
}
return ;
}
TOJ 2815 Connect them (kruskal+并查集)的更多相关文章
- Connect the Campus (Uva 10397 Prim || Kruskal + 并查集)
题意:给出n个点的坐标,要把n个点连通,使得总距离最小,可是有m对点已经连接,输入m,和m组a和b,表示a和b两点已经连接. 思路:两种做法.(1)用prim算法时,输入a,b.令mp[a][b]=0 ...
- Minimum Spanning Tree.prim/kruskal(并查集)
开始了最小生成树,以简单应用为例hoj1323,1232(求连通分支数,直接并查集即可) prim(n*n) 一般用于稠密图,而Kruskal(m*log(m))用于系稀疏图 #include< ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- hdu 1863 畅通工程(Kruskal+并查集)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- POJ 3723 Conscription (Kruskal并查集求最小生成树)
Conscription Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14661 Accepted: 5102 Des ...
- [CF891C] Envy - Kruskal,并查集
给出一个 n 个点 m条边的无向图,每条边有边权,共 Q次询问,每次给出 \(k\)条边,问这些边能否同时在一棵最小生成树上. Solution 所有最小生成树中某权值的边的数量是一定的 加完小于某权 ...
- BZOJ3545 [ONTAK2010]Peaks kruskal 并查集 主席树 dfs序
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3545 题意概括 Description 在Bytemountains有N座山峰,每座山峰有他的高度 ...
- BZOJ3551 [ONTAK2010]Peaks加强版 kruskal 并查集 主席树 dfs序
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3551 题意概括 Description 在Bytemountains有N座山峰,每座山峰有他的高度 ...
- 习题:过路费(kruskal+并查集+LCA)
过路费 [问题描述]在某个遥远的国家里,有 n 个城市.编号为 1,2,3,…,n.这个国家的政府修 建了 m 条双向道路,每条道路连接着两个城市.政府规定从城市 S 到城市 T 需 要收取的过路费 ...
随机推荐
- 搭建基于MinGW平台的《OpenGL蓝皮书(OpenGL SuperBibe 5th)》示例代码编译环境
副标题:搭建基于MinGW平台的<OpenGL超级宝典>(OpenGL蓝皮书第5版)GLTools 编译环境.示例代码:Triangle.cpp @ SB5.zip 以下内容以及方法均参考 ...
- 扩展卢卡斯定理(Exlucas)
题目链接 戳我 前置知识 中国剩余定理(crt)或扩展中国剩余定理(excrt) 乘法逆元 组合数的基本运用 扩展欧几里得(exgcd) 说实话Lucas真的和这个没有什么太大的关系,但是Lucas还 ...
- 201621123012 《java程序设计》第2周学习总结
1. 本章学习总结 1.学习了java许多了基本语法,string函数,arraylist函数的各种功能方法和调用. 2.了解了除for循环之外的foreach循环 3.学习了各种数值类的包装 4.熟 ...
- docker概述及基础操作
docker概述 容器技术已经成为应用程序封装和交付的核心技术容器技术的核心有以下几个内核组成CGroups-资源管理NamsSpace-进程管理SElinux-安全 由于是在物理机上实施隔离,启动一 ...
- ceph_osd故障检测
1. 当前monitor可以通过3种途径检测到osd离线 1) Osd自主上报 2) Osd通过投票的方式(满足一下条件之一,mon会将osd标记为down) a) ...
- php中的静态方法实例理解
<?php header("content-type:text/html;charset=utf-8"); class Human{ static public $name ...
- nRF51822外设应用[2]:GPIOTE的应用-按键检测
版权声明:本文为博主原创文章,转载请注明作者和出处. 作者:强光手电[艾克姆科技-无线事业部] 1. nRF51822寄存器类型 nRF51822的寄存器和一般的单片机有所差别,nRF51822 ...
- linux 下PATH环境变量
环境变量简介 什么是环境变量呢?简要的说,就是指定一个目录,运行软件的时候,相关的程序将会按照该目录寻找相关文件. 在linux系统下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现&quo ...
- 在pycharm中使用指定的虚拟环境virtualenv
第一步 建立虚拟环境 cmd下: pip install virtualenv 创建虚拟环境目录 env 第二步:激活虚拟环境 进入虚拟环境下的script目录 第三步:创建一个flask文件夹并打开 ...
- FileReader 方法 实现预览图片
FileReader 方法 方法名 参数 描述 abort none 中断读取 readAsBinaryString file(blob) 将文件读取为二进制码 readAsDataURL file( ...