码迷,mamicode.com
首页 > 其他好文 > 详细

hust 1102 Constructing Roads

时间:2014-05-10 02:28:50      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   java   

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

 很简单的最小生成树,只要把已经有了的边的cost设为0就可以了

bubuko.com,布布扣
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX=1000000;
struct node
{
    int left,right,cost;
}road[MAX];

bool cmp(node x,node y){return x.cost<y.cost;}

int p[MAX],m,n;

int find(int x){return x==p[x]? x:p[x]=find(p[x]);}

int kruskal()
{
    int ans=0;
    for (int i=0;i<=n;i++) p[i]=i;
    for (int i=0;i<m;i++)
    {
        int x=find(road[i].left);
        int y=find(road[i].right);
        if(x!=y)
        {
            ans+=road[i].cost;
            p[x]=y;
        }
    }
    return ans;
}

int main()
{
    int x,y,c,G[101][101],q;
    while(scanf("%d",&n)!=EOF)
    {
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        scanf("%d",&G[i][j]);
        scanf("%d",&q);
        for (int i=0;i<q;i++)
        {
            scanf("%d%d",&x,&y);
            G[x][y]=0;
        }
        m=0;
        for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            if (i!=j)
            {
                road[m].left=i;
                road[m].right=j;
                road[m].cost=G[i][j];
                m++;
            }
        }
        sort(road,road+m,cmp);
        printf("%d\n",kruskal());
    }
    return 0;
}
bubuko.com,布布扣

 

hust 1102 Constructing Roads,布布扣,bubuko.com

hust 1102 Constructing Roads

标签:des   style   blog   class   code   java   

原文地址:http://www.cnblogs.com/chensunrise/p/3719790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!