POJ 2421 Constructing Roads

Posted zlrrrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2421 Constructing Roads相关的知识,希望对你有一定的参考价值。

http://poj.org/problem?id=2421

 

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

题解:建好的边记为 0

代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;

#define inf 0x3f3f3f3f
int N, P;
int mp[110][110];
bool vis[110];
int dis[110];

int prim() {
    memset(dis, inf, sizeof(dis));
    memset(vis, false, sizeof(vis));

    dis[1] = 0;
    int temp;
    int res = 0;
    for(int i = 1; i <= N; i ++)
        dis[i] = mp[1][i];

    for(int i = 1; i <= N; i ++) {
        int minn = inf;
        for(int j = 1; j <= N; j ++) {
            if(!vis[j] && dis[j] < minn) {
                temp = j;
                minn = dis[j];
            }
        }
        vis[temp] = true;
        for(int j = 1; j <= N; j ++)
            if(!vis[j])
                dis[j] = min(dis[j], mp[temp][j]);
    }

    for(int i = 1; i <= N; i ++)
        res += dis[i];

    return res;
}

int main() {
    scanf("%d", &N);
    memset(mp, inf, sizeof(mp));
    for(int i = 1; i <= N; i ++) {
        for(int j = 1; j <= N; j ++) {
            int s;
            scanf("%d", &s);
            if(s < mp[i][j]) {
                mp[i][j] = s;
                mp[j][i] = mp[i][j];
            }
        }
    }

    int sum = 0;
    scanf("%d", &P);
    for(int i = 1; i <= P; i ++) {
        int a, b;
        scanf("%d%d", &a, &b);
        mp[a][b] = mp[b][a] = 0;
    }

    int ans = prim();
    printf("%d
", ans);
    return 0;
}

  





以上是关于POJ 2421 Constructing Roads的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2421 Constructing Roads

POJ 2421 Constructing Roads

poj 2421 Constructing Roads

POJ 2421 Constructing Roads

POJ - 2421 Constructing Roads (最小生成树)

Constructing Roads POJ - 2421