并查集

实现代码:

[cpp]  view plain copy
  1. class UnitFind  
  2. {  
  3.     enum{Max_size = 101};  
  4.       
  5. public:  
  6.     UnitFind()  
  7.     {  
  8.         ans = 0;  
  9.         int i = 0;  
  10.         for(i = 0; i < Max_size; i++)  
  11.         {  
  12.             father[i] = i;  
  13.             rank[i] = 0;            //树的深度  
  14.         }  
  15.     }  
  16.     int FindSet(int x)  
  17.     {  
  18.         if(x != father[x])    //最顶的那个祖先永远满足 x = father[x];  
  19.         {  
  20.             father[x] = FindSet(father[x]);   //路径压缩  
  21.         }  
  22.         return father[x];  
  23.     }  
  24.   
  25.     void unionSet(int x, int y)  
  26.     {  
  27.         link(FindSet(x), FindSet(y));  
  28.         return;  
  29.     }  
  30.   
  31.     void link(int x, int y)   //两个祖先,按秩合并  
  32.     {  
  33.         if(x == y) return;  
  34.         if(rank[x] > rank[y])  
  35.         {  
  36.             father[y] = x;    //将深度小的合并到深度大的那棵树上  
  37.         }  
  38.         else  
  39.         {  
  40.             if(rank[x] == rank[y])  
  41.             {  
  42.                 rank[y]++;  
  43.             }  
  44.             father[x] = y;  
  45.         }  
  46.     }  
  47. private:  
  48.     int father[Max_size];  
  49.     int rank[Max_size];  
  50. };  


练习一道ACM题,解答:2421

http://poj.org/problem?id=2421

Constructing Roads

Description

There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected. 

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.

Input

The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j. 

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.

Output

You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

Sample Input

3
0 990 692
990 0 179
692 179 0
1
1 2
Sample Output

179

题意:找出最短路径,并消除已经建好的路的长度,我利用并查集,建立了合并找到最短路的方案,首先输入所有的路,并将已经建好的路的长度置为0, 之后对要修的路进行qsort快速排序,使其从小到大排列,之后利用并查,现将小的找到其father进行并查,这样就可以使后来的大的长度的father与前面的小的father相同,就直接return即可,这样

就找到了最短路径了。

代码:

[cpp]  view plain copy
  1. #include "iostream"  
  2. using namespace std;  
  3.   
  4. class UnitFind  
  5. {  
  6.     enum{Max_size = 101};  
  7.       
  8. public:  
  9.     UnitFind()  
  10.     {  
  11.         ans = 0;  
  12.         int i = 0;  
  13.         for(i = 0; i < Max_size; i++)  
  14.         {  
  15.             father[i] = i;  
  16.             rank[i] = 0;            //树的深度  
  17.         }  
  18.     }  
  19.     int FindSet(int x)  
  20.     {  
  21.         if(x != father[x])    //最顶的那个祖先永远满足 x = father[x];  
  22.         {  
  23.             father[x] = FindSet(father[x]);   //路径压缩  
  24.         }  
  25.         return father[x];  
  26.     }  
  27.   
  28.     void unionSet(int x, int y, int w)  
  29.     {  
  30.         link(FindSet(x), FindSet(y), w);  
  31.         return;  
  32.     }  
  33.   
  34.     void link(int x, int y, int w)   //两个祖先,按秩合并  
  35.     {  
  36.         if(x == y) return;  
  37.         if(rank[x] > rank[y])  
  38.         {  
  39.             father[y] = x;    //将深度小的合并到深度大的那棵树上  
  40.         }  
  41.         else  
  42.         {  
  43.             if(rank[x] == rank[y])  
  44.             {  
  45.                 rank[y]++;  
  46.             }  
  47.             father[x] = y;  
  48.         }  
  49.         ans += w;  
  50.     }  
  51.     void print()  
  52.     {  
  53.         cout <<  ans;  
  54.     }  
  55. private:  
  56.     int ans;  
  57.     int father[Max_size];  
  58.     int rank[Max_size];  
  59. };  
  60.   
  61. typedef struct route  
  62. {  
  63.     int x_node;  
  64.     int y_node;  
  65.     int weight;  
  66. }route;  
  67.   
  68. int cmp(const void *a, const void *b)  
  69. {  
  70.     return (*(route *)a).weight - (*(route *)b).weight;  
  71. }  
  72.   
  73. #define Max 101  
  74.   
  75. int main()  
  76. {  
  77.     route Route[Max*Max/2];    
  78.     int map[Max][Max];  
  79.     memset(Route, 0, Max*Max/2*sizeof(Route[0]));  
  80.     memset(map, 0, Max*Max*sizeof(map[0][0]));  
  81.     int num;  
  82.     cin >> num;  
  83.     for(int i = 0; i < num; i++)  
  84.     {  
  85.         for(int j = 0; j < num; j++)  
  86.         {  
  87.             cin >> map[i][j];  
  88.         }  
  89.     }  
  90.   
  91.     //if it has created, wo defined it as 0;  
  92.     int Q;  
  93.     cin >> Q;  
  94.     unsigned x, y;  
  95.     for(int i = 0; i < Q; i++)  
  96.     {  
  97.         cin >> x >> y;  
  98.         x--; y--;  
  99.         map[x][y] = 0;  
  100.         map[y][x] = 0;  
  101.     }  
  102.   
  103.     int k = 0;  
  104.     for(int i = 0; i < num; i++)    //create finished  
  105.     {  
  106.         for(int j = i+1; j < num; j++)  
  107.         {  
  108.             Route[k].x_node = i;  
  109.             Route[k].y_node = j;  
  110.             Route[k].weight = map[i][j];   //just record unrepeat  
  111.             k++;  
  112.         }  
  113.     }  
  114.   
  115.     //begin accumulate  
  116.     qsort(Route, k, sizeof(Route[0]), cmp);   //small to big  
  117.   
  118.     UnitFind unitSon;  
  119.     for(int i = 0; i < k; i++)  
  120.     {  
  121.         unitSon.unionSet(Route[i].x_node, Route[i].y_node, Route[i].weight);  //因为首先经过了排序,所以并查时从小到大,前面的并查father时已经出现了,会出现相等的case.  
  122.     }  
  123.   
  124.     unitSon.print();  
  125.   
  126.     return 0;  
  127. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值