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

来源:互联网 发布:java redis list 存取 编辑:程序博客网 时间:2024/05/05 07:21

原题

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)



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.


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

题意

输入一个邻接矩阵来表示无向图,再输入一些路径表示已连通,你要再加入一些最短的路径路径使得该图为连通图,输出这些路径的长度和。

涉及知识及算法

求最小生成树的Kruskal算法(加边法),用并查集来判断是否有通路,利用#<algorithm>的sort函数对边进行排序。

代码

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int MAX_N=110;const int MAX_Q=MAX_N*MAX_N;int N,Q;int p[MAX_N];struct node{    int u,v,w;};node edge[MAX_Q];bool cmp(node a,node b){    return a.w<=b.w;}int Find(int x){    return p[x]==x?x:Find(p[x]);}int Kruskal(int m){    int ans=0;    int i,x,y;    //对边进行排序    sort(edge,edge+m,cmp);    for(i=0;i<m;i++)    {        x=edge[i].u;        y=edge[i].v;        x=Find(x);        y=Find(y);        //如果x和y之间没有通路        if(x!=y)        {            p[x]=y;            ans+=edge[i].w;        }    }    return ans;}int main(){    int k,dis,a,b;    while(cin>>N)    {        memset(edge,0,sizeof(edge));        k=0;        for(int i=0;i<N;i++)        {            for(int j=0;j<N;j++)            {                cin>>dis;                //只处理上三角矩阵                if(i>=j)                {                    continue;                }                edge[k].u=i;                edge[k].v=j;                edge[k].w=dis;                k++;            }        }        //并查集的初始化        for(int i=0;i<N;i++)        {            p[i]=i;        }        cin>>Q;        for(int i=0;i<Q;i++)        {            cin>>a>>b;            a--,b--;            //加入为一个集合(一条边为一个集合)            a=Find(a);            b=Find(b);            p[a]=b;        }        cout<<Kruskal(k)<<endl;    }    return 0;}
代码转载自HDUOJ用户csc12,向他表示感谢。

阅读全文
0 0
原创粉丝点击