hdu1102 很好的最小生成树克鲁斯卡尔算法
来源:互联网 发布:算法第四版中文版 编辑:程序博客网 时间:2024/05/16 17:14
Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8074 Accepted Submission(s): 3022
Problem 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.
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.
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
30 990 692990 0 179692 179 011 2
Sample Output
179
/*有些点城镇已经连通好了 但是我们在做的时候是求剩下的连通起来要花多少钱我们可以依然把这些已经连通好的边放进我们将要连通的所有边中 不过花费是0这样依旧求出的是最短路 注意找的是最短路*/#include<stdio.h>#include<stdlib.h>int map[105][105];struct haha{int view1;int view2;int value;}e[30000+5];int parent[10005];int cmp(const void *a,const void *b){return (*(struct haha *)a).value-(*(struct haha *)b).value;}int get_root(int x){return x==parent[x]?x:get_root(parent[x]);}int join(int x,int y){int root1,root2;root1=get_root(x);root2=get_root(y);// printf("root1=%d root2=%d\n",root1,root2);if(root1==root2) return 0;else parent[root1]=root2;return 1;}int main(){int i,j,n,k,x,y,c,ans;while(scanf("%d",&n)!=EOF){c=1;ans=0;for(i=1;i<=n;i++)for(j=1;j<=n;j++)scanf("%d",&map[i][j]);for(i=1;i<=n;i++)for(j=1;j<=n;j++){if(map[i][j]){e[c].view1=i;e[c].view2=j;e[c].value=map[i][j];c++;}}for(i=0;i<=102;i++)parent[i]=i;scanf("%d",&k);i=c;for(c;c<i+k;c++){scanf("%d %d",&x,&y);e[c].view1=x;e[c].view2=y;e[c].value=0;}// printf("%d",c);qsort(e+1,c-1,sizeof(e[0]),cmp);// for(i=1;i<c;i++)// printf("value=%d\n",e[i].value);// printf("c=%d\n",c);for(i=1;i<c;i++){// printf("i=%d:",i);if(join(e[i].view1,e[i].view2)){ans=ans+e[i].value;}}printf("%d\n",ans);}return 0;}
- hdu1102 很好的最小生成树克鲁斯卡尔算法
- 最小生成树 克鲁斯卡尔算法
- 最小生成树--克鲁斯卡尔算法
- 克鲁斯卡尔最小生成树算法
- 最小生成树(克鲁斯卡尔算法)
- 最小生成树--克鲁斯卡尔算法
- 最小生成树---克鲁斯卡尔算法
- 最小生成树的 克鲁斯卡尔算法
- 第十三周--最小生成树的克鲁斯卡尔算法
- 项目2 -- 最小生成树的克鲁斯卡尔算法
- 图的最小生成树---克鲁斯卡尔(Kruskal)算法
- 最小代价生成树的克鲁斯卡尔算法
- 求最小生成树的克鲁斯卡尔(Kruskal)算法
- 克鲁斯卡尔算法(Kruskal)图的最小生成树
- 克鲁斯卡尔算法生成最小生成树
- 最小生成树(普利姆算法、克鲁斯卡尔算法)
- 最小生成树(普利姆算法、克鲁斯卡尔算法) .
- 最小生成树(普利姆算法、克鲁斯卡尔算法)
- hdu1258 确定比赛名次 邻接表法拓扑排序
- LIST
- hdu1811 链表的最重要 非常很好的模板式链表拓扑排序
- 0727
- hdu1879继续畅通工程 最小生成树prim算法
- hdu1102 很好的最小生成树克鲁斯卡尔算法
- HDOJ1002
- JQuery Ajax WebService传递参数
- volatile 详解
- 彩虹的七种颜色CMYK和RGB的值是什么
- poj1410 Intersection 线段与矩形相交
- 【语录分享】
- 琉球群岛(zhuan)
- poj1696 Space Ant 点积,叉积,夹角