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. 聊聊并发-Java中的Copy-On-Write容器

    详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp78   聊聊并发-Java中的Copy-On-Write容器   Cop ...

  2. 【C++小白成长撸】--(续)单偶数N阶魔方矩阵

    1 /*程序的版权和版本声明部分: **Copyright(c) 2016,电子科技大学本科生 **All rights reserved. **文件名:单偶数N阶魔方矩阵 **程序作用:单偶数N阶魔 ...

  3. java基于注解的redis自动缓存实现

    目的: 对于查询接口所得到的数据,只需要配置注解,就自动存入redis!此后一定时间内,都从redis中获取数据,从而减轻数据库压力. 示例: package com.itliucheng.biz; ...

  4. java初阶

    java的开发工具分成 IDE(integrated developmentenvironment )和JDk(Java Development Kit) 一个.java中只能有一个public类且至 ...

  5. Mybatis第六篇【配置文件和映射文件再解读、占位符、主键生成与获取、Mapper代理】

    配置文件和映射文件再解读 映射文件 在mapper.xml文件中配置很多的sql语句,执行每个sql语句时,封装为MappedStatement对象,mapper.xml以statement为单位管理 ...

  6. showfm练习小项目总结

    Showfm 项目总结: 有一个主页面, 有一个service,启动和结束一般在主页面里面完成. OnCreate启动service OnDestroy关闭service EventBus 信息传递 ...

  7. windows 下面安装gcc

    0.环境说明: win7 家庭版64位 1.下载编译器 https://sourceforge.net/projects/mingw/?source=typ_redirect 如图所示: 注意,安装的 ...

  8. EGit使用教程:第一篇 添加工程到版本控制

    配置 确定身份 当每次提交的时候,Git需要跟踪这次提交,确认是哪个用户提交的.用户由 user.name 和 user.email 组成,这个信息包含在 ~/.gitconfig 文件中. ~ 代表 ...

  9. Pagination(分页) 从前台到后端总结

    一:效果图 下面我先上网页前台和管理端的部分分页效果图,他们用的是一套代码.                                   回到顶部(go to top) 二:上代码前的一些知识 ...

  10. java 如何将方法作为传参--多态

    在前段时研究智能算法时,发现如果使用java进行实现的话,往往具体实现过程差不多,但是适应值函数却根据 研究对象的不同发生很大的改变,这样对代码的维护产生很大的阻碍,于是产生的一个疑问:可不可以将适 ...