现在的位置: 首页 > 综合 > 正文

POJ1258 &&TOJ3197 Agri-Net

2013年09月15日 ⁄ 综合 ⁄ 共 2188字 ⁄ 字号 评论关闭
Agri-Net
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 33782   Accepted: 13565

Description

Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
The distance between any two farms will not exceed 100,000. 

Input

The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines
of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output

For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int maxm = 110000;
const int maxn = 1100;
//wangwenhao	1258	Accepted	184K	16MS	C++	1574B	2013-08-27 13:12:26
struct edge {
    int x, y, w;
}e[maxm];
int gn;
int fa[maxn];

bool cmp(const edge& a, const edge& b) {
    if(a.w <= b.w) return true;
    return false;
}

int getfather(int x) {
    if(x == fa[x]) return x;
    else return fa[x] = getfather(fa[x]);
}

__int64 kruscal(int edgeSize) {
    int i;
    int cnt = gn;
    sort(e, e+edgeSize, cmp);
    for(i = 1; i <= gn; i++) fa[i] = i;
    __int64 ans = 0;
    for(i = 0; i < edgeSize; i++) {
        int t1 = getfather(e[i].x);
        int t2 = getfather(e[i].y);
        if(t1 != t2) {
            fa[t1] = t2;
            ans += e[i].w;
          //  printf("ans = %d\n", ans);
            cnt--;
        }
        if(cnt == 1) {
            break;
        }
    }
    return ans;
}

int main()
{
    int i, j, w;
    int index;
    while(scanf("%d", &gn) != EOF) {
        index = 0;
        for(i = 1; i <= gn; i++) {
            for(j = 1; j <= gn; j++){
                scanf("%d", &w);
                if(i >= j) continue;
                else {
                    e[index].x = i;
                    e[index].y = j;
                    e[index].w = w;
                    index++;
                }
            }
        }
        __int64 res = kruscal(index);
        printf("%I64d\n", res);
    }
    return 0;
}

抱歉!评论已关闭.