HDU1102 Constructing Roads

来源:互联网 发布:js查找字符串出现次数 编辑:程序博客网 时间:2024/06/06 01:40

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算法+并查集。

#include<cstdio>#include<algorithm>#include<vector>#include<cstring>using namespace std;const int maxn=105;const int INF=1e6;struct edge{    int from,to,cost;};vector<edge>es;int par[maxn];int n,q,a,b;void init(){    memset(par,-1,sizeof(par));    //注意每次清空es    es.clear();}int find(int x){    while(par[x]>=0){        x=par[x];    }    return x;}void unite(int x,int y){    int x1=find(x);    int y1=find(y);    if(x1==y1) return;    if(par[x1]<=par[y1]){        par[x1]+=par[y1];        while(y>=0){            int t=par[y];            par[y]=x1;            y=t;        }    }    else{        par[y1]+=par[x1];        while(x>=0){            int t=par[x];            par[x]=y1;            x=t;        }    }}void print(){    for(int i=0;i<n;i++){        printf("%d %d\n",find(i),par[i]);    }    for(int i=0;i<es.size();i++){        printf("%d ",es[i].cost);    }    printf("\n");}bool cmp(const edge &a,const edge &b){    return a.cost<b.cost;}bool same(int a,int b){    return find(a)==find(b);}void solve(){    sort(es.begin(),es.end(),cmp);    //print();    int ans=0;    for(int i=0;i<es.size();i++){        if(!same(es[i].from,es[i].to)){            unite(es[i].from,es[i].to);            ans+=es[i].cost;        }    }    printf("%d\n",ans);}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d",&n)==1){        init();        int x;        for(int i=0;i<n;i++){            for(int j=0;j<n;j++){                scanf("%d",&x);                if(j>i){                    edge e1;                    e1.cost=x;                    e1.from=i;                    e1.to=j;                    es.push_back(e1);                }            }        }        scanf("%d",&q);        for(int i=0;i<q;i++){            scanf("%d%d",&a,&b);            a--;b--;            unite(a,b);        }        solve();    }    return 0;}
0 0
原创粉丝点击