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;

   }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章