摘要: 做最小生成树的时候,用kruskal做稠密图。。怎么都是超时,等等试一下Prim看看能不能过。。期间优化下并查集的部分,看的杭电上的文档,文档上讲的很好,讲了两种方式。 1.把小树合并到大树上去。 2.通过查找时,把树给压缩了。 看文档上讲的比较好。。。 关键代码: 1 int find(int x)//通过查找压缩路径 2 { 3 int i,r,j; 4 r = x; 5 while(r != o[r])//找到根 6 { 7 r = o[r]; 8 } 9 i = x;10 while (r != i)//把这条路... 阅读全文
posted @ 2012-06-13 17:31 Naix_x 阅读(348) 评论(4) 推荐(1) 编辑
摘要: 题目链接裸最小生成树。 还好数据小,0ms。 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <math.h> 5 int o[101],num; 6 double sum; 7 struct edge 8 { 9 int sv,ev;10 double w;11 } p[10000];12 int cmp(const void *a,const void *b)13 {14 return (*(struct edge *)a).w>(* 阅读全文
posted @ 2012-06-13 16:02 Naix_x 阅读(223) 评论(0) 推荐(0) 编辑
摘要: 题目链接真郁闷。。。模版题,由于自己SB错误,错了5 6 次。。。不过也有好处,加深对模版的理解了。。 kruskal 模版 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 int o[101],sum,num; 5 struct edge//图的结构体 6 { 7 int sv,ev,w;//起始边,终边,权值。 8 }; 9 struct edge p[10000];//必须开到大于(n*n-1)/2,否则必然RE。。。10 int comp(const void *a 阅读全文
posted @ 2012-06-13 15:05 Naix_x 阅读(216) 评论(0) 推荐(0) 编辑