zoj3204 connect them 最小生成树 暴力
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 Output1 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 最小生成树 暴力的更多相关文章
- zoj3204 Connect them 最小生成树
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367 题目就是简单的最小生成树的模板的应用,不过最小生成树可能不唯一 ...
- POJ 3522 Slim Span 最小生成树,暴力 难度:0
kruskal思想,排序后暴力枚举从任意边开始能够组成的最小生成树 #include <cstdio> #include <algorithm> using namespace ...
- 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 ...
- poj-3522 最小生成树
Description Given an undirected weighted graph G, you should find one of spanning trees specified as ...
- bzoj1016 [JSOI2008]最小生成树计数
1016: [JSOI2008]最小生成树计数 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3517 Solved: 1396[Submit][St ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- 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 ...
- FJoi2017 1月20日模拟赛 直线斯坦纳树(暴力+最小生成树+骗分+人工构造+随机乱搞)
[题目描述] 给定二维平面上n个整点,求该图的一个直线斯坦纳树,使得树的边长度总和尽量小. 直线斯坦纳树:使所有给定的点连通的树,所有边必须平行于坐标轴,允许在给定点外增加额外的中间节点. 如下图所示 ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
随机推荐
- python爬虫-抓取acg12动漫壁纸排行设置为桌面壁纸
ACG-wallpaper 初学python,之前想抓取P站的一些图片来着,然后发现acg12这里有专门的壁纸榜单,就写了个抓取壁纸作为mac桌面壁纸玩玩. 功能:抓取acg12壁纸榜单的动漫壁纸,并 ...
- FormData 上传多种格式的文件
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- FileInputStream 小Demo
要求:设计如下界面 文本框里面可以输入的路径和文件名 单机按钮可以读取在 指定的文件 并把文件内容显示到一个文本域里面来 代码: /** * */ package com.niit.homewo ...
- 【2017集美大学1412软工实践_助教博客】个人作业3——个人总结(Alpha阶段)
题目 个人作业3--个人总结(Aplha阶段) 成绩公示 评分项 alpha过程的总结 5个问题 自我评价表 评论区互动 总分 分值 4 2.5 2.5 1 10 201221123032 1 1 2 ...
- 微信小程序中的微信支付js代码和流程详解
微信支付流程 步骤 (一)获取用户的信息 (二)统一下单(返回的prepay_id用于第(三)步的请求参数) (三)发起支付请求 操作(这边假设你已经获得了用户的openId) (一).获取用户ope ...
- 201521123057 《Java程序设计》 第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容 2. 书面作业 1.List中指定元素的删除(题目4-1) 1.1 实验总结 答:remove中注意应该使用eq ...
- 201521123071 《JAVA程序设计》第二周学习总结
1. 本周学习总结 在本周的学习中,主要学习了书上的String类以及Math类等知识,但是书上学到的东西只是理论,实际打起代码来的时候会学习到更多,比如在PTA上就有学习到StringBuilder ...
- 201521123097《Java程序设计》第九周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己 ...
- 从java的开始,java概述,java配置环境变量
一.java开发入门 java 概述 Java划分为三个技术平台:JavaSE(标准版,含Java基础类库),JavaEE(企业版,技术平台),JavaME(小型版,小型产品.嵌入式设备) Jav ...
- Python学习笔记008_类_对象_继承_组合_类相关的BIF
# 对象 = 属性 + 方法>>> # Python中的类名约定以大写字母开始>>> # tt = Turtle() 这就是创建类实例的方法,其它语言用new ,它 ...