POJ 1861 Network (模版kruskal算法)
Network Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12563 Accepted: 4826 Special Judge Description Andrew 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. Input The 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. Output Output 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 Input 4 6 1 2 1 1 3 1 1 4 2 2 3 1 3 4 1 2 4 1 Sample Output 1 4 1 2 1 3 2 3 3 4
代码WA了,待查找原因
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<algorithm> 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 & a, const Kruskal & b) 19 { 20 return a.value < b.value; 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 else 41 { 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",&v,&l)!=EOF) 54 { 55 ltotal = 0; 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", &edge[i].a, &edge[i].b, &edge[i].value); 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++; //边数加1 72 //cout<<edge[i].a<<" "<<edge[i].b<<endl; 73 if(edge[i].value < Min) 74 Min=edge[i].value; 75 } 76 } 77 printf("%d\n%d\n",Min,ltotal); 78 for(int i=0;i<ltotal;i++){ 79 printf("%d %d\n",res_f[i],res_b[i]); 80 } 81 } 82 return 0; 83 }
克鲁斯卡尔(Kruskal)算法(只与边相关)
算法描述:克鲁斯卡尔算法需要对图的边进行访问,所以克鲁斯卡尔算法的时间复杂度只和边又关系,可以证明其时间复杂度为O(eloge)。
算法过程:
1.将图各边按照权值进行排序
2.将图遍历一次,找出权值最小的边,(条件:此次找出的边不能和已加入最小生成树集合的边构成环),若符合条件,则加入最小生成树的集合中。不符合条件则继续遍历图,寻找下一个最小权值的边。
3.递归重复步骤1,直到找出n-1条边为止(设图有n个结点,则最小生成树的边数应为n-1条),算法结束。得到的就是此图的最小生成树。
克鲁斯卡尔(Kruskal)算法因为只与边相关,则适合求稀疏图的最小生成树。而prime算法因为只与顶点有关,所以适合求稠密图的最小生成树。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。