ZOJ 3204 Connect them 继续MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3367
题目大意:
让你求最小生成树,并且按照字典序输出哪些点连接。无解输出-1
这里的字典序定义为:(不翻译啦~,详见我的比较函数)
A solution A is a line of p integers: a1, a2, ...ap.
Another solution B different from A is a line of q integers: b1, b2, ...bq.
A is lexicographically smaller than B if and only if:
(1) there exists a positive integer r (r <= p, r <= q) such that ai = bi for all 0 < i < r and ar < br
OR
(2) p < q and ai = bi for all 0 < i <= p
思路:
进行kruskal之前排一次序,保证算法选择的边字典序小。
然后输出的时候也要排一次序。
什么时候无解?MST需要N-1条边才能连接所有顶点,所以你就看看边的条数呗。.。。
详见代码。
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=120;
const int INF=100000+10;
int fa[MAXN];
int n; struct data
{
int x,y;
int dis;
}a[MAXN*MAXN],ans[MAXN];
bool operator< (const data& c,const data &d)
{
if(c.dis<d.dis)
return true;
else if(c.dis==d.dis)
return c.x<d.x || c.x==d.x && c.y < d.y ;
return false;
} int find(int cur)
{
return cur==fa[cur]? cur:fa[cur]=find(fa[cur]);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
int len=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
scanf("%d",&a[len].dis);
if(a[len].dis==0)
continue;
a[len].x=i;
a[len].y=j;
len++;
} sort(a,a+len); //保证kruskal的选边是按字典序来的 for(int i=1;i<=n;i++)
fa[i]=i; int lena=0;
for(int i=0;i<len;i++)
{
int rootx=find(a[i].x);
int rooty=find(a[i].y);
if(rootx!=rooty)
{
fa[rootx]=rooty;
ans[lena].x=a[i].x;
ans[lena].dis=0; //因为懒得在写比较函数,所以直接设为一样的吧
ans[lena++].y=a[i].y;
}
} if(n-1!=lena) //MST性质,肯定要n-1条边才能连接所有点
{
printf("-1\n");
continue;
} sort(ans,ans+lena); //保证输出边也是按字典序来的
for(int i=0;i<lena;i++)
{
if(i!=0)
printf(" ");
printf("%d %d",ans[i].x,ans[i].y);
}
printf("\n");
} return 0;
}
ZOJ 3204 Connect them 继续MST的更多相关文章
- 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 ...
- ZOJ 3204 Connect them(字典序输出)
主要就是将最小生成树的边按字典序输出. 读取数据时,把较小的端点赋给u,较大的端点号赋值给v. 这里要用两次排序,写两个比较器: 第一次是将所有边从小到大排序,边权相同时按u从小到大,u相同时按v从小 ...
- 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 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(最小生成树)
题意:裸最小生成树,主要是要按照字典序. 思路:模板 prim: #include<iostream> #include<stdio.h> #include<string ...
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- hdu3371 Connect the Cities (MST)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- zoj 3204 最小生成树,输出字典序最小的解
注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...
随机推荐
- 一个Web报表项目的性能分析和优化实践(七):性能监测工具JavaMelody
简介 JavaMelody 能够监测Java或Java EE应用程序服务器,并以图表的方式显示:Java内存和Java CPU使用情况,用户Session数量,JDBC连接数,和http请求.sql请 ...
- 洛谷 P2360 地下城主
P2360 地下城主 题目描述 你参加了一项秘密任务,在任务过程中你被困在了一个3D的地下监狱里面,任务是计时的,你现在需要在最短的时间里面从地牢里面逃出来继续你的任务.地牢由若干层组成,每一层的形状 ...
- 开机显示 invalid partition table
解决方法:进入你的BIOS, 然后设置你装系统的盘(SSD,或者磁盘)为第一启动位置处即可. 具体可参考文章电脑开机出现Invalid Partition Table怎么修复?解决
- Impala架构
Impala是Cloudera在受到Google的Dremel启发下开发的实时交互SQL大数据查询工具,Impala没有再使用缓慢的 Hive+MapReduce批处理,而是通过使用与商用并行关系数据 ...
- ubuntu搭建交叉编译环境makeinfo: command not found
解决办法:sudo apt-get install texinfo
- JS怎么判断数组类型?
1.判断对象的constructor是否指向Array,接着判断特殊的属性length,splice等.[应用的是constructor的定义:返回对象所对应的构造函数.] eg: [].constr ...
- loadrunner--设置代理录制
为了解决loadrunner录制不到脚本或录制经常无响应等情况,可以选用代理录制方式,步骤如下. 打开配置好代理的浏览器,输入要录制的服务器ip,开始操作,录制完成后点击Shutdown
- mycat快速体验(转)
横空出世的MyCat截至到2015年4月,保守估计已经有超过60个项目在使用,主要应用在电信领域.互联网项目,大部分是交易和管理系统,少量是信息系统.比较大的系统中,数据规模单表单月30亿.本人也初步 ...
- Qt学习 之 Socket通信
近期写大作业用到Qt的Socket部分.网上关于这部分的资料都太过复杂,如今总结一下一些简单的应用.有机会能够给大家讲讲用Socket传送文件的代码. 这里主要解说怎样实现TCP和UDP的简单通信. ...
- Android Material风格的应用(四)--FloatActionButton
添加 FloatActionButton和SnackBar Android Material风格的应用(一)--AppBar TabLayoutAndroid Material风格的应用(二)--Re ...