Step6.1.1 1102Constructing Roads(克鲁斯卡尔)

Step6.1.1 1102Constructing Roads

 

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

Total Submission(s): 12150    Accepted Submission(s): 4613

 

 

Problem Description

There are N villages, which are numberedfrom 1 to N, and you should build some roads such that every two villages canconnect to each other. We say two village A and B are connected, if and only ifthere is a road between A and B, or there exists a village C such that there isa road between A and C, and C and B are connected.

 

We know that there are already some roadsbetween some villages and your job is the build some roads such that all thevillages 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 ofwhich 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 villagej.

 

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 bhas been built.

 

 

Output

You should output a line contains aninteger, which is the length of all the roads to be built such that all thevillages 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

 

 

Source

Kicc

题解:

这道题有一个很神奇的题意,就是给你修路的各个城市的花费,但有些路已经修好了,求剩下修的路让所有的城市连接起来的最小花费。用克鲁斯卡尔求最小生成树,就可以了,先修的路不管花费如何,都标记该这两个城市之间有路,用克鲁斯卡尔算法能计算出有这些城市之间有多少是已经连接到一起了。然后是记录那些没修过的路,排序后继续生成最小生成树。

源代码:

#include <iostream>

#include <algorithm>

 

using namespace std;

#define MAX 999999;

 

typedef struct road

{

   intc1,c2,value;

}Road;

 

int n;

Road rd[10050];

int a[105][105];

int node[105];

 

void init()

{

   memset(a,0,sizeof(a));

   memset(node,-1,sizeof(node));

}

bool mycmp(const Road &x,const Road &y)

{

   if(x.value< y.value)

     return1;

   else

     return0;

}

 

int find_set(int x)

{

   if(node[x]== -1)

     returnx;

   returnnode[x] = find_set(node[x]);

}

 

bool merge(int s1,int s2)

{

   intr1 = find_set(s1);

   intr2 = find_set(s2);

   if(r1== r2)

     return0;

   if(r1< r2)

     node[r2] = r1;

   else

     node[r1] = r2;

   return1;

}

 

int main()

{

   while(cin>> n)

   {

     init();

     for(int i = 1;i <= n;i++)

        for(int k = 1;k <= n;k++)

        {

          cin >> a[i][k];

        }

 

     intm;

     cin >> m;

     intcount = 0;

     for(int i = 1;i <= m;i++)

     {

        intp1,p2;

        cin >> p1 >> p2;

        if(merge(p1,p2))

          count++;

        a[p1][p2] = 0;

        a[p2][p1] = 0;

       

     }

 

     intnum = 0;

     for(int  i = 1;i<= n;i++)

     {

        for(int j = i;j <= n;j++)

        {

          if(a[i][j])

          {

             rd[num].c1 = i;

             rd[num].c2 = j;

             rd[num++].value =a[i][j];

          }

        }

     }

 

     sort(rd,rd+num,mycmp);

 

     intsum = 0;

     for(int i = 0;i < num;i++)

     {

        if(count== n-1)

          break;

        if(merge(rd[i].c1,rd[i].c2))

        {

          sum+=rd[i].value;

          count++;

        }

     }

     cout<< sum << endl;

   }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值