首页 > 编程语言 >最小生成树 Kruskal算法 HDU 1102 Constructing Roads

最小生成树 Kruskal算法 HDU 1102 Constructing Roads

时间:2023-02-07 13:03:48浏览次数:38  
标签:HDU Constructing r2 int Kruskal fa roads find r1


Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 13   Accepted Submission(s) : 5
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. <br><br>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.<br>

 


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.<br><br>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.<br>

 


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. <br>

 


Sample Input


3 0 990 692 990 0 179 692 179 0 1 1 2

 


Sample Output


179


算法分析:

很简单的最小生成树,但接触到快排,很不错。

代码实现:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y,v;
}a[100010];
int fa[100005];
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
int cmp(const void *c,const void *b)//快排比较函数
{
return (*(struct node *)c).v-(*(struct node *)b).v;
}
int main()
{

int n;
while(scanf("%d",&n)!=EOF)
{
int m=0;//一开始没想这m循环输出,所以m在外面清0,教训
for(int i=0;i<=n;i++)
fa[i]=i;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)//转化
{
int x;
scanf("%d",&x);
if(x!=0)
{
a[m].x=i;
a[m].y=j;
a[m].v=x;m++;
}
}
int q;
scanf("%d",&q);
int k=0;
for(int i=1;i<=q;i++)//已经修好的路。
{
int x,y;
scanf("%d%d",&x,&y);

int r1=find(x);
int r2=find(y);
if(r1!=r2)
{
fa[r1]=r2;
k++;
}
}
qsort(a,m,sizeof(a[0]),cmp);//必须要快排,不然会挂掉
int tot=0;
for(int i=0;i<m;i++)
{
int r1=find(a[i].x);
int r2=find(a[i].y);
if(r1!=r2)
{
fa[r1]=r2;
tot+=a[i].v;
k++;
}
if(k==n-1) break;
}
printf("%d\n",tot);
}
return 0;
}


标签:HDU,Constructing,r2,int,Kruskal,fa,roads,find,r1
From: https://blog.51cto.com/u_14932227/6041999

相关文章

  • HDU/HDOJ 2067 小兔的棋盘 DP/卡特兰数
    HDU/HDOJ2067小兔的棋盘小兔的棋盘TimeLimit:1000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):12782    Acce......
  • iOS7中UIView的animateKeyframesWithDuration方法讲解
    在iOS7中,给UIView添加了一个方法用来直接使用关键帧动画而不用借助CoreAnimation来实现,那就是animateKeyframesWithDuration以下是使用源码:////Vi......
  • hdu1427-速算24点
    速算24点TimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):4374    AcceptedSubmission(s):1079......
  • hdu-1874-畅通工程续(dijkstra + SPFA )
    畅通工程续TimeLimit:3000/1000MS(Java/Others)    MemoryLimit:32768/32768K(Java/Others)TotalSubmission(s):36928    AcceptedSubmission(s):13......
  • hdu-1513-Palindrome
    PalindromeTimeLimit:4000/2000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):4052    AcceptedSubmission(s):1377......
  • hdu-1312-Red and Black
    RedandBlackTimeLimit:2000/1000MS(Java/Others)    MemoryLimit:65536/32768K(Java/Others)TotalSubmission(s):13464    AcceptedSubmission(s):......
  • HDU 6441 Find Integer (费马大定理)
    Description:peopleinUSSSlovemathverymuch,andthereisafamousmathproblemgiveyoutwointegersn,a,youarerequiredtofind......
  • HDU 5223 GCD
    Description:Inmathematics,thegreatestcommondivisor(gcd......
  • HDU1098 Ignatius's puzzle (数学归纳法)
    Description:Ignatiusispooratmath,hefallsacrossapuzzleproblem,sohehasnochoicebuttoappealtoEddy.thisproblemdescribesthat:......
  • HDU6198 number number number(打表 矩阵快速幂)
    题意就是找到用K个斐波那契数组不成的最小的数字是谁。先打表找规律1421233348852326609可以发现递推规律:F[n]=4*(F[n-1]-F[n-2])+F[n-3]如果直接递推打......