Connect them


Time Limit:
1 Second      Memory Limit:32768 KB


You have n computers numbered from 1 ton and you want to connect
them to make a small local area network (LAN). All connections are two-way (that is connecting computersi andj
is the same as connecting computersj andi). The cost of connecting computeri
and computerj iscij. 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 eachcij
, find the cheapest way to connect computers.

Input

There are multiple test cases. The first line of input contains an integerT (T
<= 100), indicating the number of test cases. ThenT test cases follow.

The first line of each test case contains an integern (1 <n <=
100). Thenn lines follow, each of which containsn integers separated by a space. Thej-th
integer of thei-th line in thesen lines iscij,
indicating the cost of connecting computersi andj (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:

i1j1i1j1
......

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 thelexicographically
smallest
one (see hints for the definition of "lexicography small") If you cannot connect them, just output "-1" in the line.

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

 1:每个点都必须加入,而且不可能有环,所以生成一棵树。
 2:最小生成树。
 3:数据小,直接暴力。
 4:反正我暴力之后也想不明白O(n^4)居然只要60ms。600ms我比较好接受,难道暴力出奇迹?


#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<memory.h>
using namespace std;
const int inf=1e9;
int map[101][101];
int ans,n;
int tnum,tfrom,tto;
bool used[101];
int _Sin()
{
char c=getchar();
while(c<'0'||c>'9') c=getchar();
int s=0;
while(c>='0'&&c<='9'){
s=s*10+c-'0';
c=getchar();
}
return s;
}
struct in{
int L,R;
}a[101];
bool cmp(in a,in b){
if(a.L==b.L) return a.R<b.R;
return a.L<b.L;
}
void _update()
{
memset(used,false,sizeof(used));
return ;
}
void _in()
{
n=_Sin();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=_Sin();
}
bool _solve()
{
used[1]=true;
for(int s=1;s<n;s++){
tnum=inf;
for(int i=1;i<=n;i++){
if(used[i]){
for(int j=1;j<=n;j++)
{
if(!used[j]&&map[i][j]!=0&&map[i][j]<tnum)
{
tnum=map[i][j];
tfrom=i;
tto=j;
}
}
}
}
if(tnum==inf) return false;
used[tto]=true;
if(tfrom>tto) swap(tfrom,tto);
a[s].L=tfrom;
a[s].R=tto;
}
sort(a+1,a+n,cmp);
return true;
}
int main()
{
int T;
T=_Sin();
while(T--){
_update();
_in();
if(!_solve()) printf("-1\n");
else {
printf("%d %d",a[1].L,a[1].R);
for(int i=2;i<n;i++)
printf(" %d %d",a[i].L,a[i].R);
printf("\n");
}
}
return 0;
}



												

zoj3204 connect them 最小生成树 暴力的更多相关文章

  1. zoj3204 Connect them 最小生成树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一 ...

  2. POJ 3522 Slim Span 最小生成树,暴力 难度:0

    kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...

  3. ZOJ - 3204 Connect them 最小生成树

    Connect them ZOJ - 3204 You have n computers numbered from 1 to n and you want to connect them to ma ...

  4. poj-3522 最小生成树

    Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...

  5. bzoj1016 [JSOI2008]最小生成树计数

    1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3517  Solved: 1396[Submit][St ...

  6. hdu 3371 Connect the Cities(最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  7. HDU 2489 Minimal Ratio Tree(暴力+最小生成树)(2008 Asia Regional Beijing)

    Description For a tree, which nodes and edges are all weighted, the ratio of it is calculated accord ...

  8. FJoi2017 1月20日模拟赛 直线斯坦纳树(暴力+最小生成树+骗分+人工构造+随机乱搞)

    [题目描述] 给定二维平面上n个整点,求该图的一个直线斯坦纳树,使得树的边长度总和尽量小. 直线斯坦纳树:使所有给定的点连通的树,所有边必须平行于坐标轴,允许在给定点外增加额外的中间节点. 如下图所示 ...

  9. hdu oj 3371 Connect the Cities (最小生成树)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. Mysql介绍和实践总结

    本文首先介绍mysql的安装和基本使用.进阶操作.讲解mysql的导入导出和自动备份,然后介绍安全模式修改密码和mysql的全文本搜索功能,最后记录了个人使用mysql中遇到的问题集. 开始安装: s ...

  2. [js高手之路]Vue2.0基于vue-cli+webpack同级组件之间的通信教程

    我们接着上文继续,本文我们讲解兄弟组件的通信,项目结构还是跟上文一样. 在src/assets目录下建立文件EventHandler.js,该文件的作用在于给同级组件之间传递事件 EventHandl ...

  3. Android 日夜间切换Demo

    这是其中一种实现模式,也是比较麻烦的一种,首先写布局,不多说上代码 activity_main.xml <?xml version="1.0" encoding=" ...

  4. Mac系统的终端显示git当前分支

    当我第一次在mac系统下使用git的时候,发现一个问题,git默认是不显示当前所在的分支名称,然后网上查找资料,找到了解决办法,终于可以显示本地当前分支,现在分享如下. 1 进入你的home目录 cd ...

  5. cobbler部署安装CentOS6.8

    Linux运维:cobbler : 矮哥linux运维群:93324526 学习cobbler的话,必须先搞懂kickstart,原理不是,不懂如何排错. kickstart部署请点击这里 1. Co ...

  6. 教程,Python图片转字符堆叠图

    Python 图片转字符画 一.实验说明 1. 环境登录 无需密码自动登录, 2. 环境介绍 本实验环境采用带桌面的UbuntuLinux环境,实验中会用到桌面上的程序: LX终端(LXTermina ...

  7. MySQL(四)--蠕虫复制、查询

    1 蠕虫复制 蠕虫复制:从已有的数据中去获取数据,然后将数据又进行新增操作,数据成倍增加. 表创建高级操作:从已有创建新表(复制表结构) create table 表名 like 数据库.表名; 蠕虫 ...

  8. css样式实现立方体制作

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 201521123107 《Java程序设计》第14周学习总结

    第14周-数据库 1.本周学习总结 2.书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名) 在自己建立的数据库上执行常见SQL语 ...

  10. 201521123009 《Java程序设计》第6周学习总结

    1. 本周学习总结 2. 书面作业 Q1:clone方法 1.1 Object对象中的clone方法是被protected修饰,在自定义的类中覆盖clone方法时需要注意什么? 用protected修 ...