NetworkTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 12563 Accepted: 4826 Special JudgeDescriptionAndrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections. You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied. InputThe first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.OutputOutput first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.Sample Input4 61 2 11 3 11 4 22 3 13 4 12 4 1Sample Output141 21 32 33 4View Question
代码WA了,待查找原因
1 #include 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 #define MAX 1000 8 int father[MAX], son[MAX], Min=0x3fffffff; 9 int v, l;10 11 typedef struct Kruskal //存储边的信息12 {13 int a;14 int b;15 int value;16 };17 18 bool cmp(const Kruskal 21 }22 23 int unionsearch(int x) //查找根结点+路径压缩24 {25 return x == father[x] ? x : unionsearch(father[x]);26 }27 28 bool join(int x, int y) //合并29 {30 int root1, root2;31 root1 = unionsearch(x);32 root2 = unionsearch(y);33 if(root1 == root2) //为环34 return false;35 else if(son[root1] >= son[root2])36 {37 father[root2] = root1;38 son[root1] += son[root2];39 }40 else41 {42 father[root1] = root2;43 son[root2] += son[root1];44 }45 return true;46 }47 48 int main()49 {50 int ltotal;51 int res_f[100],res_b[100];52 Kruskal edge[MAX];53 while(scanf("%d%d",56 for(int i = 1; i <= v; ++i) //初始化57 {58 father[i] = i;59 son[i] = 1;60 }61 for(int i = 1; i <= l ; ++i)62 {63 scanf("%d%d%d", 64 }65 sort(edge + 1, edge + 1 + l, cmp); //按权值由小到大排序66 for(int i = 1; i <= l; ++i)67 {68 if(join(edge[i].a, edge[i].b))69 {70 res_f[ltotal]=edge[i].a; res_b[ltotal]=edge[i].b;71 ltotal++; //边数加172 //cout<
算法描述:克鲁斯卡尔算法需要对图的边进行访问,所以克鲁斯卡尔算法的时间复杂度只和边又关系,可以证明其时间复杂度为O(eloge)。
算法过程:
1.将图各边按照权值进行排序
2.将图遍历一次,找出权值最小的边,(条件:此次找出的边不能和已加入最小生成树集合的边构成环),若符合条件,则加入最小生成树的集合中。不符合条件则继续遍历图,寻找下一个最小权值的边。
3.递归重复步骤1,直到找出n-1条边为止(设图有n个结点,则最小生成树的边数应为n-1条),算法结束。得到的就是此图的最小生成树。
克鲁斯卡尔(Kruskal)算法因为只与边相关,则适合求稀疏图的最小生成树。而prime算法因为只与顶点有关,所以适合求稠密图的最小生成树。
摘自http://blog.csdn.net/niushuai666/article/details/6689285
POJ 1861 Network (模版kruskal算法),布布扣,bubuko.com