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 ...
随机推荐
- java常见加密方式介绍
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt260 本篇内容简要介绍BASE64.MD5.SHA.HMAC几种加密算法. ...
- Linux-mount命令和umount命令 (8)
mount:用于挂载文件系统,使能访问其它文件系统中的资源 umount:用于卸载已挂载的文件系统 mount: 格式: mount [-参数] [设备名称] [挂载点] 其中常用的参数(参数默认自带 ...
- Linux修改文件permission属性
列出文件属性 ls -al 修改文件属性为可读.可写 sudo chmod -c 777 <your file name>
- CCIE-交换路由复习笔记
交换 考点: 1.trunk link(基础) 2.vtp 3.vlan 4.stp rstp mstp 5.hsrp vrrp glbp 6.ec Trunk link: 修改封装模式 802.1q ...
- 软件工程(GZSD2015)第二次作业进度
贵州师范大学软件工程第二次作业 徐 镇 王铭霞 张 英 涂江枫 张 燕 安 坤 周 娟 杨明颢 杨家堂 罗文豪 娄秀盛 周 娟 李盼 岳庆 张颖 李丽思 邓婷 唐洁 郑倩 尚清丽 陈小丽 毛茸 宋光能 ...
- 团队作业1——团队展示&博客作业查重系统
团队展示: 1.队名:六个核桃 2.队员学号: 王婧(201421123065).柯怡芳(201421123067组长).陈艺菡(201421123068). 钱惠(201421123071).尼玛( ...
- 【集美大学1411_助教博客】团队作业10——项目复审与事后分析(Beta版本)
写在前面的话 软件工程课结束了,大家开心吗?是不是再也不用熬夜写代码了?如果这门课你真的熬夜写代码了,相信你一定有收获,如果这门课结束了你觉得是自己一个全新的开始,那么这门课的意义就实现了.团队作业全 ...
- 201521123100《Java程序设计》第八周学习总结
---恢复内容开始--- 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 ...
- 201521123051《Java程序设计》第十四周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- 201521123022 《Java程序设计》 第十四周学习总结
1. 本章学习总结 2. 书面作业 Q1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自己的学号.姓名),在自己建立的数据库上执行常见SQL语句(截图) ...