ZOJ - 3204 Connect them 最小生成树
Connect them ZOJ - 3204
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.
Input
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 iand j (cij = 0 means that you cannot connect them). 0 <= cij <= 60000, cij = cji, cii= 0, 1 <= i, j <= n.
Output
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.
<b< dd="">
Sample Input
2
3
0 2 3
2 0 5
3 5 0
2
0 0
0 0
Sample Output
1 2 1 3
-1 题意:最小生成树,但要求最小字典序的解。
那么变化就是在cmp的函数书写的,不能仅仅的比较value,还要把from,to按字典序的顺序来加入,当然输出也是如此。
所以重写两个cmp函数就可以了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=;
int T,x;
int n,p,q;
int cnt,sum;
struct Node
{
int from,to;
double value;
}node[maxn*maxn],ans[maxn*maxn];
int fa[maxn];
bool cmp(Node a,Node b)
{
if(a.value!=b.value)
return a.value<b.value;
else if(a.from!=b.from)
return a.from<b.from;
else
return a.to<b.to;
}
bool cmpp(Node a,Node b)
{
if(a.from==b.from)
return a.to<b.to;
else
return a.from<b.from;
}
void init()
{
for(int i=;i<maxn;i++)
fa[i]=i;
}
int findd(int x)
{
if(fa[x]==x)
return x;
else
return fa[x]=findd(fa[x]);
}
void Kruskal()
{
sum=;
for(int i=;i<=cnt;i++)
{
int fx=findd(node[i].from);
int fy=findd(node[i].to);
if(fx!=fy)
{
ans[sum++]=node[i];
fa[fx]=fy;
}
}
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cnt=;
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&x);
if(x==||j<=i)
continue;
cnt++;
node[cnt].from=i;node[cnt].to=j;
node[cnt].value=x;
}
}
init();
sort(node+,node+cnt+,cmp);
sum=;
Kruskal();
if(sum!=n-)
{
printf("-1\n");
continue;
}
else
{
sort(ans,ans+sum,cmpp);
for(int i=;i<sum-;i++)
printf("%d %d ",ans[i].from,ans[i].to);
printf("%d %d\n",ans[sum-].from,ans[sum-].to);
}
}
return ;
}
ZOJ - 3204 Connect them 最小生成树的更多相关文章
- ZOJ 3204 Connect them(最小生成树+最小字典序)
Connect them Time Limit: 1 Second Memory Limit: 32768 KB You have n computers numbered from 1 t ...
- zoj 3204 Connect them(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- ZOJ 3204 Connect them MST-Kruscal
这道题目麻烦在输出的时候需要按照字典序输出,不过写了 Compare 函数还是比较简单的 因为是裸的 Kruscal ,所以就直接上代码了- Source Code : //#pragma comme ...
- zoj 3204 Connect them
最小生成树,我用的是并查集+贪心的写法. #include<stdio.h> #include<string.h> #include<math.h> #includ ...
- ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目大意: 让你求最小生成树,并且按照字典序输出哪些点连接.无解输出-1 ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
- zoj3204 connect them 最小生成树 暴力
Connect them Time Limit: 1 Second Memory Limit:32768 KB You have n computers numbered from 1 to ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
随机推荐
- 洛谷 P2763 试题库问题【最大流】
s向所有类别属性连流量为当前类别属性需要的个数的边,所有题目向t连流量为1的边(表示只能选一次),所有属性向含有它的题连容量为1的边.跑一变dinic,结果小于m则无解,否则看每一个类别属性连出去的题 ...
- python面向对象的3个特点
封装 封装是从业务逻辑中抽象对象时,要赋予对象相关数据与操作,将一些数据和操作打包在一起的过程.封装是使用对象的主要魅力之一,它提供了一个简单方法来创建复杂方案,解决了世界是如何工作的这一问题,我们自 ...
- rabbitmq实践笔记(一):安装、配置与使用初探
引言: 对于一个大型的软件系统来说,会有很多的组件.模块及不同的子系统一起协同工作,模块之间的通信需要一个可靠的通信管道来保证 ,通信管道需要解决解决很多问题,比如: 1)信息的发送者和接收者如何维持 ...
- [转]C# 邮箱验证激活
原文链接 /// <summary> /// 发送邮件 发送激活码 /// </summary> /// <param name="address"& ...
- 表达式语言EL简单学习
Jsp2.0最重要的特性就是表达式语言EL.jsp用户可以用它来访问应用程序数据. EL表达式以${开头并以}结束. ${expresion} ${x+y} 它也常用来连接两个表达式,取值将从 ...
- GUI初步和frame&panel
java的话这个GUI其实不是什么重点,但我们也要学习,重点是学习这种图形编程的思路. java里面对于图形的一些类都封装在了AWT和它的一些子包里.AWT(抽象窗口开发包) 当 ...
- D. Winter Is Coming 贪心(好题)
http://codeforces.com/contest/747/problem/D 大概的思路就是找到所有两个负数夹着的线段,优先覆盖最小的长度.使得那时候不用换鞋,是最优的. 但是这里有个坑点, ...
- Hibernate配置(外部配置文件方式)
配置Hibernate有2种方式,本文讲的是通过外部配置文件配置的方式 Hibernate核心配置文件 <?xml version='1.0' encoding='UTF-8'?> < ...
- AJPFX分析Android退出应用最优雅的方式
什么是RS式呢?即Receiver+singleTask .我们知道Activity有四种加载模式,而singleTask就是其中的一种,使用这个模式之后,当startActivity时,它先会在当前 ...
- 平板&Safari 开发tips
css: *{ margin: 0; padding: 0; /* 禁止用户点选网页内容 */ -webkit-touch-callout:none; -webkit-user-select:non ...